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

fix(ecs): EC2 metadata access is blocked when using EC2 capacity provider for autoscaling #28437

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,28 @@
"Properties": {
"ContainerDefinitions": [
{
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Memory": 256,
"Name": "web"
},
{
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "envoyproxy/envoy:v1.16.2",
"Memory": 256,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,14 @@
"Name": "log_router"
},
{
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "nginx",
"LogConfiguration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,18 @@
"Properties": {
"ContainerDefinitions": [
{
"Environment": [
{
"Name": "SOME_VARIABLE",
"Value": "value"
},
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Memory": 256,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef', {
const container = taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 256,
environment: {
SOME_VARIABLE: 'value',
},
});

container.addPortMappings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,14 @@
"Properties": {
"ContainerDefinitions": [
{
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Memory": 256,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,14 @@
"Properties": {
"ContainerDefinitions": [
{
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
}
],
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Memory": 256,
Expand Down
24 changes: 22 additions & 2 deletions packages/aws-cdk-lib/aws-ecs/lib/ec2/ec2-task-definition.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Construct } from 'constructs';
import { Stack } from '../../../core';
import { ImportedTaskDefinition } from '../base/_imported-task-definition';
import {
CommonTaskDefinitionAttributes,
CommonTaskDefinitionProps,
Compatibility,
InferenceAccelerator,
IpcMode,
ITaskDefinition,
NetworkMode,
PidMode,
TaskDefinition,
InferenceAccelerator,
} from '../base/task-definition';
import { ContainerDefinition, ContainerDefinitionOptions } from '../container-definition';
import { PlacementConstraint } from '../placement';

/**
Expand Down Expand Up @@ -83,7 +85,6 @@ export interface Ec2TaskDefinitionAttributes extends CommonTaskDefinitionAttribu
* @resource AWS::ECS::TaskDefinition
*/
export class Ec2TaskDefinition extends TaskDefinition implements IEc2TaskDefinition {

/**
* Imports a task definition from the specified task definition ARN.
*/
Expand Down Expand Up @@ -146,4 +147,23 @@ export class Ec2TaskDefinition extends TaskDefinition implements IEc2TaskDefinit
// Validate the placement constraints
Ec2TaskDefinition.validatePlacementConstraints(props.placementConstraints ?? []);
}

/**
* Tasks running in AWSVPC networking mode requires an additional environment variable for the region to be sourced.
* This override adds in the additional environment variable as required
*/
override addContainer(id: string, props: ContainerDefinitionOptions): ContainerDefinition {
if (this.networkMode === NetworkMode.AWS_VPC) {
return new ContainerDefinition(this, id, {
taskDefinition: this,
...props,
environment: {
...props.environment,
AWS_REGION: Stack.of(this).region,
},
});
}
// If network mode is not AWSVPC, then just add the container as normal
return new ContainerDefinition(this, id, { taskDefinition: this, ...props });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (this.networkMode === NetworkMode.AWS_VPC) {
return new ContainerDefinition(this, id, {
taskDefinition: this,
...props,
environment: {
...props.environment,
AWS_REGION: Stack.of(this).region,
},
});
}
// If network mode is not AWSVPC, then just add the container as normal
return new ContainerDefinition(this, id, { taskDefinition: this, ...props });
if (this.networkMode === NetworkMode.AWS_VPC) {
return super.addContainer(id, {
...props,
environment: {
...props.environment,
AWS_REGION: Stack.of(this).region,
},
});
}
// If network mode is not AWSVPC, then just add the container as normal
return super.addContainer(id, props);

Let's reuse the parent's class method

}
}
61 changes: 61 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/ec2/ec2-task-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,67 @@ describe('ec2 task definition', () => {
}],
});
});

test('correctly sets env variables when using EC2 capacity provider with AWSVPC mode', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add another test with awsvpc network mode and no added environment variables? (will set only AWS_REGION)

// GIVEN AWS-VPC network mode
const stack = new cdk.Stack();
const taskDefiniton = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', {
networkMode: ecs.NetworkMode.AWS_VPC,
});
taskDefiniton.addContainer('some-container', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
environment: {
SOME_VARIABLE: 'some-value',
},
});

// THEN it should include the AWS_REGION env variable
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
NetworkMode: ecs.NetworkMode.AWS_VPC,
ContainerDefinitions: [{
Name: 'some-container',
Image: 'amazon/amazon-ecs-sample',
Memory: 512,
Environment: [{
Name: 'SOME_VARIABLE',
Value: 'some-value',
}, {
Name: 'AWS_REGION',
Value: {
Ref: 'AWS::Region',
},
}],
}],
});

// GIVEN HOST network mode
const anotherStack = new cdk.Stack();
Copy link
Contributor

Choose a reason for hiding this comment

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

This case should be separated into another test.

const anotherTaskDefiniton = new ecs.Ec2TaskDefinition(anotherStack, 'Ec2TaskDef', {
networkMode: ecs.NetworkMode.HOST,
});
anotherTaskDefiniton.addContainer('some-container', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
environment: {
SOME_VARIABLE: 'some-value',
},
});

// THEN it should add in any env variables
Template.fromStack(anotherStack).hasResourceProperties('AWS::ECS::TaskDefinition', {
NetworkMode: ecs.NetworkMode.HOST,
ContainerDefinitions: [{
Name: 'some-container',
Image: 'amazon/amazon-ecs-sample',
Memory: 512,
Environment: [{
Name: 'SOME_VARIABLE',
Value: 'some-value',
}],
}],
});
});
});

describe('setting inferenceAccelerators', () => {
Expand Down
Loading