Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(aws-ecs): expose environment from containerDefinition #17889

Merged
merged 15 commits into from
Dec 13, 2021
Merged
3 changes: 1 addition & 2 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ rule.addTarget(new targets.EcsTask({
containerOverrides: [{
containerName: 'TheContainer',
environment: [{
name: 'I_WAS_TRIGGERED',
value: 'From CloudWatch Events'
I_WAS_TRIGGERED: 'From CloudWatch Events'
}],
}],
}));
Expand Down
8 changes: 7 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ export class ContainerDefinition extends CoreConstruct {
*/
public readonly taskDefinition: TaskDefinition;

/**
* The environment variables for this container
*/
public readonly environment?: { [key: string]: string };

/**
* The environment files for this container
*/
Expand Down Expand Up @@ -437,6 +442,7 @@ export class ContainerDefinition extends CoreConstruct {
this.memoryLimitSpecified = props.memoryLimitMiB !== undefined || props.memoryReservationMiB !== undefined;
this.linuxParameters = props.linuxParameters;
this.containerName = props.containerName ?? this.node.id;
this.environment = props.environment ?? undefined;

this.imageConfig = props.image.bind(this, this);
if (props.logging) {
Expand Down Expand Up @@ -669,7 +675,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 && renderKV(this.environment, 'name', 'value'),
environmentFiles: this.environmentFiles && renderEnvironmentFiles(this.environmentFiles),
secrets: this.secrets,
extraHosts: this.props.extraHosts && renderKV(this.props.extraHosts, 'hostname', 'ipAddress'),
Expand Down
21 changes: 12 additions & 9 deletions packages/@aws-cdk/aws-ecs/test/container-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('container definition', () => {
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
const secret = new secretsmanager.Secret(stack, 'Secret');
new ecs.ContainerDefinition(stack, 'Container', {
const environment = { keyOne: 'valueOne', keyTwo: 'valueTwo' };
const containerDefinition = new ecs.ContainerDefinition(stack, 'Container', {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
taskDefinition,
memoryLimitMiB: 1024,
Expand All @@ -60,10 +61,7 @@ describe('container definition', () => {
},
dockerSecurityOptions: ['ECS_SELINUX_CAPABLE=true'],
entryPoint: ['top', '-b'],
environment: {
key: 'foo',
value: 'bar',
},
environment,
environmentFiles: [ecs.EnvironmentFile.fromAsset(path.join(__dirname, 'demo-envfiles/test-envfile.env'))],
essential: true,
extraHosts: {
Expand All @@ -89,6 +87,7 @@ describe('container definition', () => {
{ namespace: 'SomeNamespace', value: 'SomeValue' },
],
});
containerDefinition.environment!.keyThree = 'valueThree';

// THEN
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
Expand Down Expand Up @@ -118,12 +117,16 @@ describe('container definition', () => {
],
Environment: [
{
Name: 'key',
Value: 'foo',
Name: 'keyOne',
Value: 'valueOne',
},
{
Name: 'keyTwo',
Value: 'valueTwo',
},
{
Name: 'value',
Value: 'bar',
Name: 'keyThree',
Value: 'valueThree',
},
],
EnvironmentFiles: [{
Expand Down
Loading