From 7f8dd2e61a1efe6557b69ab465d021f3f505e74c Mon Sep 17 00:00:00 2001 From: Philip McMahon Date: Mon, 22 Nov 2021 15:40:19 +0000 Subject: [PATCH 1/3] feat: Add environmentOverrides prop to for ecs tasks --- src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap | 2 +- src/constructs/ecs/ecs-task.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap b/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap index 6dbd7a2551..79aba352df 100644 --- a/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap +++ b/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap @@ -142,7 +142,7 @@ Object { "Arn", ], }, - "\\",\\"TaskDefinition\\":\\"test-TEST-ecs-test\\",\\"NetworkConfiguration\\":{\\"AwsvpcConfiguration\\":{\\"Subnets\\":[\\"abc-123\\"],\\"SecurityGroups\\":[\\"id-123\\"]}},\\"LaunchType\\":\\"FARGATE\\",\\"PlatformVersion\\":\\"LATEST\\"}}}}", + "\\",\\"TaskDefinition\\":\\"test-TEST-ecs-test\\",\\"NetworkConfiguration\\":{\\"AwsvpcConfiguration\\":{\\"Subnets\\":[\\"abc-123\\"],\\"SecurityGroups\\":[\\"id-123\\"]}},\\"Overrides\\":{\\"ContainerOverrides\\":[{\\"Name\\":\\"TaskContainerEcstest\\"}]},\\"LaunchType\\":\\"FARGATE\\",\\"PlatformVersion\\":\\"LATEST\\"}}}}", ], ], }, diff --git a/src/constructs/ecs/ecs-task.ts b/src/constructs/ecs/ecs-task.ts index 661cf8266e..98785b91c9 100644 --- a/src/constructs/ecs/ecs-task.ts +++ b/src/constructs/ecs/ecs-task.ts @@ -13,6 +13,7 @@ import { import type { PolicyStatement } from "@aws-cdk/aws-iam"; import { Topic } from "@aws-cdk/aws-sns"; import { IntegrationPattern, StateMachine } from "@aws-cdk/aws-stepfunctions"; +import type { TaskEnvironmentVariable } from "@aws-cdk/aws-stepfunctions-tasks"; import { EcsFargateLaunchTarget, EcsRunTask } from "@aws-cdk/aws-stepfunctions-tasks"; import { CfnOutput, Duration } from "@aws-cdk/core"; import type { NoMonitoring } from "../cloudwatch"; @@ -95,6 +96,7 @@ export interface GuEcsTaskProps extends Identity { monitoringConfiguration: NoMonitoring | GuEcsTaskMonitoringProps; securityGroups?: ISecurityGroup[]; customTaskPolicies?: PolicyStatement[]; + environmentOverrides?: TaskEnvironmentVariable[]; } /** @@ -145,7 +147,7 @@ export class GuEcsTask { family: `${props.stack}-${props.stage}-${props.app}`, }); - taskDefinition.addContainer(`${id}-TaskContainer`, { + const containerDefinition = taskDefinition.addContainer(`${id}-TaskContainer`, { image: getContainer(props.containerConfiguration), entryPoint: props.taskCommand ? ["/bin/sh"] : undefined, command: props.taskCommand ? ["-c", `${props.taskCommand}`] : undefined, // if unset, falls back to CMD in docker file, or no command will be run @@ -172,6 +174,12 @@ export class GuEcsTask { resultPath: "DISCARD", timeout: Duration.minutes(timeout), securityGroups: props.securityGroups ?? [], + containerOverrides: [ + { + containerDefinition: containerDefinition, + environment: props.environmentOverrides, + }, + ], }); this.stateMachine = new StateMachine(scope, `${id}-StateMachine`, { From 09926f9afa9128d679212a168703567eaae1783f Mon Sep 17 00:00:00 2001 From: Philip McMahon Date: Tue, 23 Nov 2021 12:12:21 +0000 Subject: [PATCH 2/3] chore: Add docs for environmentOverrides param --- src/constructs/ecs/ecs-task.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/constructs/ecs/ecs-task.ts b/src/constructs/ecs/ecs-task.ts index 98785b91c9..30872290c5 100644 --- a/src/constructs/ecs/ecs-task.ts +++ b/src/constructs/ecs/ecs-task.ts @@ -85,6 +85,20 @@ export type GuEcsTaskMonitoringProps = { snsTopicArn: string; noMonitoring: fals * You can also set the memory and cpu units for your task. See https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size * for further details. * + * If you want to pass input from the step function into your EcsTask you can do so via the `environmentOverrides` prop, which allows you to wire up + * step function input JSON to environment variables set on the container. For example, + * const props = { + * ...otherProps + * environmentOverrides: [ + { + name: "VERSION", + value: JsonPath.stringAt("$.version"), + }, + } + * With the above override, your task will attempt to find a `version` property in the JSON input passed to the step function, and apply it to the + * VERSION environment variable. Alternatively, you could hard code a value for the variable in CDK. + * See https://docs.aws.amazon.com/step-functions/latest/dg/connect-ecs.html for further detail and other override options - this construct currently + * only supports environment variables. */ export interface GuEcsTaskProps extends Identity { vpc: IVpc; From 17681ca9cffee2666ff8781003bd08e9e64f998d Mon Sep 17 00:00:00 2001 From: Philip McMahon Date: Wed, 24 Nov 2021 10:54:08 +0000 Subject: [PATCH 3/3] chore: update snapshots --- src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap b/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap index 79aba352df..afea1940c6 100644 --- a/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap +++ b/src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap @@ -142,7 +142,7 @@ Object { "Arn", ], }, - "\\",\\"TaskDefinition\\":\\"test-TEST-ecs-test\\",\\"NetworkConfiguration\\":{\\"AwsvpcConfiguration\\":{\\"Subnets\\":[\\"abc-123\\"],\\"SecurityGroups\\":[\\"id-123\\"]}},\\"Overrides\\":{\\"ContainerOverrides\\":[{\\"Name\\":\\"TaskContainerEcstest\\"}]},\\"LaunchType\\":\\"FARGATE\\",\\"PlatformVersion\\":\\"LATEST\\"}}}}", + "\\",\\"TaskDefinition\\":\\"test-TEST-ecs-test\\",\\"NetworkConfiguration\\":{\\"AwsvpcConfiguration\\":{\\"Subnets\\":[\\"abc-123\\"],\\"SecurityGroups\\":[\\"id-123\\"]}},\\"Overrides\\":{\\"ContainerOverrides\\":[{\\"Name\\":\\"test-ecs-task-ecs-test-TaskContainer\\"}]},\\"LaunchType\\":\\"FARGATE\\",\\"PlatformVersion\\":\\"LATEST\\"}}}}", ], ], },