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(aws-ecs): expose environment from containerDefinition #17889

Merged
merged 15 commits into from
Dec 13, 2021
Merged
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ declare const parameter: ssm.StringParameter;
declare const taskDefinition: ecs.TaskDefinition;
declare const s3Bucket: s3.Bucket;

taskDefinition.addContainer('container', {
const newContainer = taskDefinition.addContainer('container', {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
memoryLimitMiB: 1024,
environment: { // clear text, not for sensitive data
Expand All @@ -421,6 +421,7 @@ taskDefinition.addContainer('container', {
PARAMETER: ecs.Secret.fromSsmParameter(parameter),
},
});
newContainer.addEnvironment('QUEUE_NAME', 'MyQueue');
```

The task execution role is automatically granted read permissions on the secrets/parameters. Support for environment
Expand Down
17 changes: 16 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ export class ContainerDefinition extends CoreConstruct {

private readonly secrets?: CfnTaskDefinition.SecretProperty[];

private readonly environment: { [key: string]: string };

/**
* Constructs a new instance of the ContainerDefinition class.
*/
Expand Down Expand Up @@ -457,6 +459,12 @@ export class ContainerDefinition extends CoreConstruct {
}
}

if (props.environment) {
this.environment = { ...props.environment };
} else {
this.environment = {};
}

if (props.environmentFiles) {
this.environmentFiles = [];

Expand Down Expand Up @@ -547,6 +555,13 @@ export class ContainerDefinition extends CoreConstruct {
}));
}

/**
* This method adds an environment variable to the container.
*/
public addEnvironment(name: string, value: string) {
this.environment[name] = value;
}

/**
* This method adds one or more resources to the container.
*/
Expand Down Expand Up @@ -669,7 +684,7 @@ export class ContainerDefinition extends CoreConstruct {
volumesFrom: cdk.Lazy.any({ produce: () => this.volumesFrom.map(renderVolumeFrom) }, { omitEmptyArray: true }),
workingDirectory: this.props.workingDirectory,
logConfiguration: this.logDriverConfig,
environment: this.props.environment && renderKV(this.props.environment, 'name', 'value'),
environment: this.environment && Object.keys(this.environment).length ? renderKV(this.environment, 'name', 'value') : undefined,
environmentFiles: this.environmentFiles && renderEnvironmentFiles(this.environmentFiles),
secrets: this.secrets,
extraHosts: this.props.extraHosts && renderKV(this.props.extraHosts, 'hostname', 'ipAddress'),
Expand Down
34 changes: 33 additions & 1 deletion packages/@aws-cdk/aws-ecs/test/container-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,14 @@ describe('container definition', () => {
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
taskDefinition.addContainer('cont', {
const container = taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environment: {
TEST_ENVIRONMENT_VARIABLE: 'test environment variable value',
},
});
container.addEnvironment('SECOND_ENVIRONEMENT_VARIABLE', 'second test value');

// THEN
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
Expand All @@ -705,6 +706,37 @@ describe('container definition', () => {
Environment: [{
Name: 'TEST_ENVIRONMENT_VARIABLE',
Value: 'test environment variable value',
},
{
Name: 'SECOND_ENVIRONEMENT_VARIABLE',
Value: 'second test value',
}],
},
],
});


});

test('can add environment variables to container definition with no environment', () => {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
const container = taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
});
container.addEnvironment('SECOND_ENVIRONEMENT_VARIABLE', 'second test value');

// THEN
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [{
Name: 'SECOND_ENVIRONEMENT_VARIABLE',
Value: 'second test value',
}],
},
],
Expand Down
Loading