Skip to content

Commit

Permalink
feat(aws-ecs): expose environment from containerDefinition (#17889)
Browse files Browse the repository at this point in the history
Closes #17867

* Assigned props.environment to a public readonly member
* Added integration test that confirms the environment can be appended after the task is instantiated

Made 2 cosmetic, but no obvious changes. Environment values are specified:

name: value
name2: value

But in the test and the README.md files the sample values were:

name: something
value: something else

This is using the string 'value" as a key - which, as someone reading the code for the first time, was confusing. So I changed the sample values to more clearly display what's a key and what's a value.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
biffgaut authored Dec 13, 2021
1 parent 4e7a275 commit 4937cd0
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 3 deletions.
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

0 comments on commit 4937cd0

Please sign in to comment.