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

feat(ecs): support container environment variables #1537

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 11 additions & 11 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface ContainerDefinitionProps {
*
* @default No labels
*/
dockerLabels?: {[key: string]: string };
dockerLabels?: { [key: string]: string };

/**
* A list of custom labels for SELinux and AppArmor multi-level security systems.
Expand All @@ -81,7 +81,7 @@ export interface ContainerDefinitionProps {
*
* @default No environment variables
*/
environment?: {[key: string]: string};
environment?: { [key: string]: string };

/**
* Indicates whether the task stops if this container fails.
Expand All @@ -101,7 +101,7 @@ export interface ContainerDefinitionProps {
*
* @default No extra hosts
*/
extraHosts?: {[name: string]: string};
extraHosts?: { [name: string]: string };

/**
* Container health check.
Expand Down Expand Up @@ -434,7 +434,7 @@ export interface HealthCheck {
timeout?: number;
}

function renderKV(env: {[key: string]: string}, keyName: string, valueName: string): any {
function renderKV(env: { [key: string]: string }, keyName: string, valueName: string): any {
const ret = [];
for (const [key, value] of Object.entries(env)) {
ret.push({ [keyName]: key, [valueName]: value });
Expand Down Expand Up @@ -577,16 +577,16 @@ function renderPortMapping(pm: PortMapping): CfnTaskDefinition.PortMappingProper
}

export interface ScratchSpace {
containerPath: string,
readOnly: boolean,
sourcePath: string
name: string,
containerPath: string,
readOnly: boolean,
sourcePath: string
name: string,
}

export interface MountPoint {
containerPath: string,
readOnly: boolean,
sourceVolume: string,
containerPath: string,
readOnly: boolean,
sourceVolume: string,
}

function renderMountPoint(mp: MountPoint): CfnTaskDefinition.MountPointProperty {
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export interface LoadBalancedEc2ServiceProps {
* @default 1
*/
desiredCount?: number;

/**
* Environment variables to pass to the container
*
* @default No environment variables
*/
environment?: { [key: string]: string };
}

/**
Expand All @@ -80,6 +87,7 @@ export class LoadBalancedEc2Service extends cdk.Construct {
const container = taskDefinition.addContainer('web', {
image: props.image,
memoryLimitMiB: props.memoryLimitMiB,
environment: props.environment,
});

container.addPortMappings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export interface LoadBalancedFargateServiceAppletProps extends cdk.StackProps {
* Setting this option will set the load balancer port to 443.
*/
certificate?: string;

/**
* Environment variables to pass to the container
*
* @default No environment variables
*/
environment?: { [key: string]: string };
}

/**
Expand Down Expand Up @@ -127,6 +134,7 @@ export class LoadBalancedFargateServiceApplet extends cdk.Stack {
publicTasks: props.publicTasks,
image: ContainerImage.fromDockerHub(props.image),
desiredCount: props.desiredCount,
environment: props.environment,
certificate,
domainName: props.domainName,
domainZone
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export interface LoadBalancedFargateServiceProps {
* @default true
*/
createLogs?: boolean;

/**
* Environment variables to pass to the container
*
* @default No environment variables
*/
environment?: { [key: string]: string };

}

/**
Expand All @@ -132,7 +140,8 @@ export class LoadBalancedFargateService extends cdk.Construct {

const container = taskDefinition.addContainer('web', {
image: props.image,
logging: optIn ? this.createAWSLogDriver(this.node.id) : undefined
logging: optIn ? this.createAWSLogDriver(this.node.id) : undefined,
environment: props.environment
});

container.addPortMappings({
Expand Down
35 changes: 33 additions & 2 deletions packages/@aws-cdk/aws-ecs/test/test.container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,37 @@ export = {
},
},

'can add environment variables 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.fromDockerHub('test'),
memoryLimitMiB: 1024,
environment: {
TEST_ENVIRONMENT_VARIABLE: "test environment variable value"
}
});

// THEN
// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [{
Name: "TEST_ENVIRONMENT_VARIABLE",
Value: "test environment variable value"
}]
}
]
}));

test.done();

},

'can add AWS logging to container definition'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -285,9 +316,9 @@ export = {
PolicyDocument: {
Statement: [
{
Action: [ "logs:CreateLogStream", "logs:PutLogEvents" ],
Action: ["logs:CreateLogStream", "logs:PutLogEvents"],
Effect: "Allow",
Resource: { "Fn::GetAtt": [ "LoggingLogGroupC6B8E20B", "Arn" ] }
Resource: { "Fn::GetAtt": ["LoggingLogGroupC6B8E20B", "Arn"] }
}
],
Version: "2012-10-17"
Expand Down
86 changes: 78 additions & 8 deletions packages/@aws-cdk/aws-ecs/test/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export = {
cluster,
memoryLimitMiB: 1024,
image: ecs.ContainerImage.fromDockerHub('test'),
desiredCount: 2
desiredCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});

// THEN - stack containers a load balancer and a service
Expand All @@ -30,6 +34,23 @@ export = {
LaunchType: "EC2",
}));

expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "TEST_ENVIRONMENT_VARIABLE1",
Value: "test environment variable 1 value"
},
{
Name: "TEST_ENVIRONMENT_VARIABLE2",
Value: "test environment variable 2 value"
}
],
}
]
}));

test.done();
},

Expand All @@ -43,14 +64,28 @@ export = {
new ecs.LoadBalancedFargateService(stack, 'Service', {
cluster,
image: ecs.ContainerImage.fromDockerHub('test'),
desiredCount: 2
desiredCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});

// THEN - stack contains a load balancer and a service
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "TEST_ENVIRONMENT_VARIABLE1",
Value: "test environment variable 1 value"
},
{
Name: "TEST_ENVIRONMENT_VARIABLE2",
Value: "test environment variable 2 value"
}
],
LogConfiguration: {
LogDriver: "awslogs",
Options: {
Expand Down Expand Up @@ -86,13 +121,27 @@ export = {
cluster,
image: ecs.ContainerImage.fromDockerHub('test'),
desiredCount: 2,
createLogs: false
createLogs: false,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});

// THEN - stack contains a load balancer and a service
expect(stack).notTo(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "TEST_ENVIRONMENT_VARIABLE1",
Value: "test environment variable 1 value"
},
{
Name: "TEST_ENVIRONMENT_VARIABLE2",
Value: "test environment variable 2 value"
}
],
LogConfiguration: {
LogDriver: "awslogs",
Options: {
Expand Down Expand Up @@ -145,8 +194,8 @@ export = {
},
Type: 'A',
AliasTarget: {
HostedZoneId: { 'Fn::GetAtt': [ 'ServiceLBE9A1ADBC', 'CanonicalHostedZoneID' ] },
DNSName: { 'Fn::GetAtt': [ 'ServiceLBE9A1ADBC', 'DNSName' ] },
HostedZoneId: { 'Fn::GetAtt': ['ServiceLBE9A1ADBC', 'CanonicalHostedZoneID'] },
DNSName: { 'Fn::GetAtt': ['ServiceLBE9A1ADBC', 'DNSName'] },
}
}));

Expand Down Expand Up @@ -176,7 +225,11 @@ export = {
const app = new cdk.App();
const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', {
image: 'test',
desiredCount: 2
desiredCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});

// THEN - stack contains a load balancer and a service
Expand All @@ -187,6 +240,23 @@ export = {
LaunchType: "FARGATE",
}));

expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Environment: [
{
Name: "TEST_ENVIRONMENT_VARIABLE1",
Value: "test environment variable 1 value"
},
{
Name: "TEST_ENVIRONMENT_VARIABLE2",
Value: "test environment variable 2 value"
}
],
}
]
}));

test.done();
},

Expand Down Expand Up @@ -221,8 +291,8 @@ export = {
HostedZoneId: "/hostedzone/DUMMY",
Type: 'A',
AliasTarget: {
HostedZoneId: { 'Fn::GetAtt': [ 'FargateServiceLBB353E155', 'CanonicalHostedZoneID' ] },
DNSName: { 'Fn::GetAtt': [ 'FargateServiceLBB353E155', 'DNSName' ] },
HostedZoneId: { 'Fn::GetAtt': ['FargateServiceLBB353E155', 'CanonicalHostedZoneID'] },
DNSName: { 'Fn::GetAtt': ['FargateServiceLBB353E155', 'DNSName'] },
}
}));

Expand Down