Skip to content

Commit

Permalink
Merge pull request aws#11820 from jkenn99/patch-1
Browse files Browse the repository at this point in the history
fix(ecs): remove Fargate environment files restriction
  • Loading branch information
SoManyHs authored Dec 10, 2020
2 parents 9cd76cd + bd9366b commit 2f55502
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 109 deletions.
4 changes: 0 additions & 4 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ export class ContainerDefinition extends cdk.Construct {
}
}

if (this.taskDefinition.isFargateCompatible && props.environmentFiles) {
throw new Error(`Cannot specify environment files for a task using the FARGATE launch type in container '${this.node.id}'.`);
}

if (props.environmentFiles) {
this.environmentFiles = [];

Expand Down
303 changes: 198 additions & 105 deletions packages/@aws-cdk/aws-ecs/test/test.container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,122 +703,215 @@ export = {
},

'Environment Files': {
'can add asset environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
'with EC2 task definitions': {
'can add asset environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromAsset(path.join(__dirname, 'demo-envfiles/test-envfile.env'))],
});
// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromAsset(path.join(__dirname, 'demo-envfiles/test-envfile.env'))],
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3Bucket7B2069B7',
},
'/',
{
'Fn::Select': [
0,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
{
'Fn::Select': [
1,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3Bucket7B2069B7',
},
'/',
{
'Fn::Select': [
0,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
{
'Fn::Select': [
1,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
],
],
],
},
}],
},
],
}));
},
}],
},
],
}));

test.done();
},
'can add s3 bucket environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Bucket', {
bucketName: 'test-bucket',
});
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
test.done();
},
'can add s3 bucket environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Bucket', {
bucketName: 'test-bucket',
});
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef');

// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromBucket(bucket, 'test-key')],
});
// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromBucket(bucket, 'test-key')],
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'Bucket83908E77',
},
'/test-key',
// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'Bucket83908E77',
},
'/test-key',
],
],
],
},
}],
},
],
}));
},
}],
},
],
}));

test.done();
test.done();
},
},
'throws when using environment files for a Fargate task'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');
'with Fargate task definitions': {
'can add asset environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');

// THEN
test.throws(() => taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromAsset(path.join(__dirname, 'demo-envfiles/test-envfile.env'))],
}), /Cannot specify environment files for a task using the FARGATE launch type in container/);
// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromAsset(path.join(__dirname, 'demo-envfiles/test-envfile.env'))],
});

test.done();
// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3Bucket7B2069B7',
},
'/',
{
'Fn::Select': [
0,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
{
'Fn::Select': [
1,
{
'Fn::Split': [
'||',
{
Ref: 'AssetParameters872561bf078edd1685d50c9ff821cdd60d2b2ddfb0013c4087e79bf2bb50724dS3VersionKey40E12C15',
},
],
},
],
},
],
],
},
}],
},
],
}));

test.done();
},
'can add s3 bucket environment file to the container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Bucket', {
bucketName: 'test-bucket',
});
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');

// WHEN
taskDefinition.addContainer('cont', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
environmentFiles: [ecs.EnvironmentFile.fromBucket(bucket, 'test-key')],
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
EnvironmentFiles: [{
Type: 's3',
Value: {
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'Bucket83908E77',
},
'/test-key',
],
],
},
}],
},
],
}));

test.done();
},
},
},

Expand Down

0 comments on commit 2f55502

Please sign in to comment.