Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(batch): add secrets props to job definition #19506

Closed
wants to merge 10 commits into from
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ new batch.JobDefinition(this, 'job-def', {
});
```

### Using the secret on secrets manager

You can set the environment variables from secrets manager.

```ts
const dbSecret = new secretsmanager.Secret(this, 'secret');

new batch.JobDefinition(this, 'batch-job-def-secrets', {
container: {
image: ecs.EcrImage.fromRegistry('docker/whalesay'),
secrets: {
PASSWORD: ecs.Secret.fromSecretsManager(dbSecret, 'password'),
}
},
});
```

### Importing an existing Job Definition

#### From ARN
Expand Down
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-batch/lib/job-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export interface JobDefinitionContainer {
*/
readonly environment?: { [key: string]: string };

/**
* The environment variables from secrets manager or ssm parameter store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ecs.Secret will not support SSM parameter store though.

Don't you want core.SecretValue ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I guess not if you need the ARN. But in any case, the SSM comment here is inaccurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my understanding, ecs.Secret supports SSM parameter store through fromSsmParameter method.
Is it correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was thinking of ssm.Secret.

*
* @default none
*/
readonly secrets?: { [key: string]: ecs.Secret };

/**
* The image used to start a container.
*/
Expand Down Expand Up @@ -453,6 +460,14 @@ export class JobDefinition extends Resource implements IJobDefinition {
platformCapabilities: props.platformCapabilities ?? [PlatformCapabilities.EC2],
});

// add read secrets permission to execution role
if ( props.container.secrets && props.container.executionRole ) {
const executionRole = props.container.executionRole;
Object.values(props.container.secrets).forEach((secret) => {
secret.grantRead(executionRole);
});
}

this.jobDefinitionArn = this.getResourceArnAttribute(jobDef.ref, {
service: 'batch',
resource: 'job-definition',
Expand Down Expand Up @@ -507,6 +522,14 @@ export class JobDefinition extends Resource implements IJobDefinition {
return {
command: container.command,
environment: this.deserializeEnvVariables(container.environment),
secrets: container.secrets
? Object.entries(container.secrets).map(([key, value]) => {
return {
name: key,
valueFrom: value.arn,
};
})
: undefined,
image: this.imageConfig.imageName,
instanceType: container.instanceType && container.instanceType.toString(),
jobRoleArn: container.jobRole && container.jobRole.roleArn,
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-batch/rosetta/default.ts-fixture
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Stack } from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as batch from '@aws-cdk/aws-batch';
import * as ecs from '@aws-cdk/aws-ecs';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';

class Fixture extends Stack {
constructor(scope: Construct, id: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,9 +1362,22 @@
},
"batchjobrepo4C508C51": {
"Type": "AWS::ECR::Repository",
"Properties": {
"ImageScanningConfiguration": {
"ScanOnPush": false
}
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"batchsecret7CD5E4C6": {
"Type": "AWS::SecretsManager::Secret",
"Properties": {
"GenerateSecretString": {}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"batchjobdeffromecrE0E30DAD": {
"Type": "AWS::Batch::JobDefinition",
"Properties": {
Expand Down Expand Up @@ -1486,6 +1499,32 @@
}
}
},
"executionroleDefaultPolicy497F11A3": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret"
],
"Effect": "Allow",
"Resource": {
"Ref": "batchsecret7CD5E4C6"
}
}
],
"Version": "2012-10-17"
},
"PolicyName": "executionroleDefaultPolicy497F11A3",
"Roles": [
{
"Ref": "executionroleD9A39BE6"
}
]
}
},
"batchjobdeffargate7FE30059": {
"Type": "AWS::Batch::JobDefinition",
"Properties": {
Expand All @@ -1509,6 +1548,14 @@
"Type": "MEMORY",
"Value": "512"
}
],
"Secrets": [
{
"Name": "SECRET",
"ValueFrom": {
"Ref": "batchsecret7CD5E4C6"
}
}
]
},
"PlatformCapabilities": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"18.0.0"}
14 changes: 0 additions & 14 deletions packages/@aws-cdk/aws-batch/test/batch.integ.snapshot/integ.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"version": "17.0.0",
"version": "18.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"metadata": {}
},
"batch-stack": {
"type": "aws:cloudformation:stack",
Expand Down Expand Up @@ -285,6 +286,12 @@
"data": "batchjobrepo4C508C51"
}
],
"/batch-stack/batch-secret/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "batchsecret7CD5E4C6"
}
],
"/batch-stack/batch-job-def-from-ecr/Resource": [
{
"type": "aws:cdk:logicalId",
Expand All @@ -303,6 +310,12 @@
"data": "executionroleD9A39BE6"
}
],
"/batch-stack/execution-role/DefaultPolicy/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "executionroleDefaultPolicy497F11A3"
}
],
"/batch-stack/batch-job-def-fargate/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
82 changes: 81 additions & 1 deletion packages/@aws-cdk/aws-batch/test/batch.integ.snapshot/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,11 @@
"path": "batch-stack/batch-job-repo/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::ECR::Repository",
"aws:cdk:cloudformation:props": {}
"aws:cdk:cloudformation:props": {
"imageScanningConfiguration": {
"scanOnPush": false
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-ecr.CfnRepository",
Expand All @@ -1614,6 +1618,30 @@
"version": "0.0.0"
}
},
"batch-secret": {
"id": "batch-secret",
"path": "batch-stack/batch-secret",
"children": {
"Resource": {
"id": "Resource",
"path": "batch-stack/batch-secret/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::SecretsManager::Secret",
"aws:cdk:cloudformation:props": {
"generateSecretString": {}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-secretsmanager.CfnSecret",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-secretsmanager.Secret",
"version": "0.0.0"
}
},
"batch-job-def-from-ecr": {
"id": "batch-job-def-from-ecr",
"path": "batch-stack/batch-job-def-from-ecr",
Expand Down Expand Up @@ -1814,6 +1842,50 @@
"fqn": "@aws-cdk/aws-iam.CfnRole",
"version": "0.0.0"
}
},
"DefaultPolicy": {
"id": "DefaultPolicy",
"path": "batch-stack/execution-role/DefaultPolicy",
"children": {
"Resource": {
"id": "Resource",
"path": "batch-stack/execution-role/DefaultPolicy/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::IAM::Policy",
"aws:cdk:cloudformation:props": {
"policyDocument": {
"Statement": [
{
"Action": [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret"
],
"Effect": "Allow",
"Resource": {
"Ref": "batchsecret7CD5E4C6"
}
}
],
"Version": "2012-10-17"
},
"policyName": "executionroleDefaultPolicy497F11A3",
"roles": [
{
"Ref": "executionroleD9A39BE6"
}
]
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-iam.CfnPolicy",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-iam.Policy",
"version": "0.0.0"
}
}
},
"constructInfo": {
Expand Down Expand Up @@ -1849,6 +1921,14 @@
"aws:cdk:cloudformation:props": {
"type": "container",
"containerProperties": {
"secrets": [
{
"name": "SECRET",
"valueFrom": {
"Ref": "batchsecret7CD5E4C6"
}
}
],
"image": "docker/whalesay",
"executionRoleArn": {
"Fn::GetAtt": [
Expand Down
Loading