Skip to content

Commit

Permalink
update integrations tests and fix linting as required
Browse files Browse the repository at this point in the history
  • Loading branch information
juinquok committed Dec 20, 2023
1 parent df24694 commit be56ba6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 34 deletions.
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 @@ -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
39 changes: 20 additions & 19 deletions packages/aws-cdk-lib/aws-ecs/lib/ec2/ec2-task-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PidMode,
TaskDefinition,
} from '../base/task-definition';
import { ContainerDefinition, ContainerDefinitionOptions } from '../container-definition'
import { ContainerDefinition, ContainerDefinitionOptions } from '../container-definition';
import { PlacementConstraint } from '../placement';

/**
Expand Down Expand Up @@ -131,24 +131,6 @@ export class Ec2TaskDefinition extends TaskDefinition implements IEc2TaskDefinit
}
}

/**
* 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});
}

/**
* Constructs a new instance of the Ec2TaskDefinition class.
*/
Expand All @@ -165,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 });
}
}
30 changes: 15 additions & 15 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 @@ -1121,9 +1121,9 @@ describe('ec2 task definition', () => {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
environment: {
SOME_VARIABLE: 'some-value'
}
})
SOME_VARIABLE: 'some-value',
},
});

// THEN it should include the AWS_REGION env variable
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
Expand All @@ -1134,14 +1134,14 @@ describe('ec2 task definition', () => {
Memory: 512,
Environment: [{
Name: 'SOME_VARIABLE',
Value: 'some-value'
Value: 'some-value',
}, {
Name: 'AWS_REGION',
Value: {
Ref: 'AWS::Region'
}
}]
}]
Ref: 'AWS::Region',
},
}],
}],
});

// GIVEN HOST network mode
Expand All @@ -1153,9 +1153,9 @@ describe('ec2 task definition', () => {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
environment: {
SOME_VARIABLE: 'some-value'
}
})
SOME_VARIABLE: 'some-value',
},
});

// THEN it should add in any env variables
Template.fromStack(anotherStack).hasResourceProperties('AWS::ECS::TaskDefinition', {
Expand All @@ -1166,11 +1166,11 @@ describe('ec2 task definition', () => {
Memory: 512,
Environment: [{
Name: 'SOME_VARIABLE',
Value: 'some-value'
}]
}]
Value: 'some-value',
}],
}],
});
})
});
});

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

0 comments on commit be56ba6

Please sign in to comment.