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-patterns): add CloudMapOptions to load balanced services #4369

Merged
merged 7 commits into from
Oct 3, 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
44 changes: 27 additions & 17 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ To define an Amazon ECS service that is behind an application load balancer, ins
const loadBalancedEcsService = new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
image: ecs.ContainerImage.fromRegistry('test'),
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
},
desiredCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});
```

Expand All @@ -48,7 +50,9 @@ const loadBalancedFargateService = new ecsPatterns.ApplicationLoadBalancedFargat
cluster,
memoryLimitMiB: 1024,
cpu: 512,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
});
```

Expand All @@ -67,12 +71,14 @@ To define an Amazon ECS service that is behind a network load balancer, instanti
const loadBalancedEcsService = new ecsPatterns.NetworkLoadBalancedEc2Service(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
image: ecs.ContainerImage.fromRegistry('test'),
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
},
desiredCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
}
});
```

Expand All @@ -83,7 +89,9 @@ const loadBalancedFargateService = new ecsPatterns.NetworkLoadBalancedFargateSer
cluster,
memoryLimitMiB: 1024,
cpu: 512,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
});
```

Expand Down Expand Up @@ -139,11 +147,13 @@ To define a task that runs periodically, instantiate an `ScheduledEc2Task`:

```ts
// Instantiate an Amazon EC2 Task to run at a scheduled interval
const ecsScheduledTask = new ScheduledEc2Task(this, 'ScheduledTask', {
const ecsScheduledTask = new ScheduledEc2Task(stack, 'ScheduledTask', {
cluster,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
scheduleExpression: 'rate(1 minute)',
environment: [{ name: 'TRIGGER', value: 'CloudWatch Events' }],
memoryLimitMiB: 256
scheduledEc2TaskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 256,
environment: { name: 'TRIGGER', value: 'CloudWatch Events' },
},
schedule: events.Schedule.expression('rate(1 minute)')
});
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DnsValidatedCertificate, ICertificate } from '@aws-cdk/aws-certificatemanager';
import { IVpc } from '@aws-cdk/aws-ec2';
import { AwsLogDriver, BaseService, Cluster, ContainerImage, ICluster, LogDriver, PropagatedTagSource, Secret } from '@aws-cdk/aws-ecs';
import { AwsLogDriver, BaseService, CloudMapOptions, Cluster, ContainerImage, ICluster, LogDriver, PropagatedTagSource, Secret } from '@aws-cdk/aws-ecs';
import { ApplicationListener, ApplicationLoadBalancer, ApplicationProtocol, ApplicationTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
import { IRole } from '@aws-cdk/aws-iam';
import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53';
Expand Down Expand Up @@ -122,6 +122,13 @@ export interface ApplicationLoadBalancedServiceBaseProps {
* @default false
*/
readonly enableECSManagedTags?: boolean;

/**
* The options for configuring an Amazon ECS service to use service discovery.
*
* @default - AWS Cloud Map service discovery is not enabled.
*/
readonly cloudMapOptions?: CloudMapOptions;
}

export interface ApplicationLoadBalancedTaskImageOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IVpc } from '@aws-cdk/aws-ec2';
import { AwsLogDriver, BaseService, Cluster, ContainerImage, ICluster, LogDriver, PropagatedTagSource, Secret } from '@aws-cdk/aws-ecs';
import { AwsLogDriver, BaseService, CloudMapOptions, Cluster, ContainerImage, ICluster, LogDriver, PropagatedTagSource, Secret } from '@aws-cdk/aws-ecs';
import { NetworkListener, NetworkLoadBalancer, NetworkTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
import { IRole } from '@aws-cdk/aws-iam';
import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53';
Expand Down Expand Up @@ -100,6 +100,13 @@ export interface NetworkLoadBalancedServiceBaseProps {
* @default false
*/
readonly enableECSManagedTags?: boolean;

/**
* The options for configuring an Amazon ECS service to use service discovery.
*
* @default - AWS Cloud Map service discovery is not enabled.
*/
readonly cloudMapOptions?: CloudMapOptions;
}

export interface NetworkLoadBalancedTaskImageOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe
healthCheckGracePeriod: props.healthCheckGracePeriod,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
});
this.addServiceAsTarget(this.service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas
healthCheckGracePeriod: props.healthCheckGracePeriod,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
});
this.addServiceAsTarget(this.service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
healthCheckGracePeriod: props.healthCheckGracePeriod,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
});
this.addServiceAsTarget(this.service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
healthCheckGracePeriod: props.healthCheckGracePeriod,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
});
this.addServiceAsTarget(this.service);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@aws-cdk/aws-iam": "^1.11.0",
"@aws-cdk/aws-route53": "^1.11.0",
"@aws-cdk/aws-route53-targets": "^1.11.0",
"@aws-cdk/aws-servicediscovery": "^1.11.0",
"@aws-cdk/aws-sqs": "^1.11.0",
"@aws-cdk/core": "^1.11.0"
},
Expand All @@ -94,6 +95,7 @@
"@aws-cdk/aws-iam": "^1.11.0",
"@aws-cdk/aws-route53": "^1.11.0",
"@aws-cdk/aws-route53-targets": "^1.11.0",
"@aws-cdk/aws-servicediscovery": "^1.11.0",
"@aws-cdk/aws-sqs": "^1.11.0",
"@aws-cdk/core": "^1.11.0"
},
Expand Down
143 changes: 143 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ecs = require('@aws-cdk/aws-ecs');
import { AwsLogDriver } from '@aws-cdk/aws-ecs';
import { ApplicationProtocol } from '@aws-cdk/aws-elasticloadbalancingv2';
import { PublicHostedZone } from '@aws-cdk/aws-route53';
import cloudmap = require('@aws-cdk/aws-servicediscovery');
import cdk = require('@aws-cdk/core');
import { Test } from 'nodeunit';
import ecsPatterns = require('../../lib');
Expand Down Expand Up @@ -135,6 +136,148 @@ export = {
test.done();
},

'creates AWS Cloud Map service for Private DNS namespace with application load balanced ec2 service'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
cluster.addDefaultCloudMapNamespace({
name: 'foo.com',
type: cloudmap.NamespaceType.DNS_PRIVATE
});

new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', {
cluster,
taskImageOptions: {
containerPort: 8000,
image: ecs.ContainerImage.fromRegistry('hello'),
},
cloudMapOptions: {
name: 'myApp',
},
memoryLimitMiB: 512,
});

// THEN
expect(stack).to(haveResource("AWS::ECS::Service", {
ServiceRegistries: [
{
ContainerName: "web",
ContainerPort: 8000,
RegistryArn: {
"Fn::GetAtt": [
"ServiceCloudmapServiceDE76B29D",
"Arn"
]
}
}
]
}));

expect(stack).to(haveResource('AWS::ServiceDiscovery::Service', {
DnsConfig: {
DnsRecords: [
{
TTL: 60,
Type: "SRV"
}
],
NamespaceId: {
'Fn::GetAtt': [
'EcsClusterDefaultServiceDiscoveryNamespaceB0971B2F',
'Id'
]
},
RoutingPolicy: 'MULTIVALUE'
},
HealthCheckCustomConfig: {
FailureThreshold: 1
},
Name: "myApp",
NamespaceId: {
'Fn::GetAtt': [
'EcsClusterDefaultServiceDiscoveryNamespaceB0971B2F',
'Id'
]
}
}));

test.done();
},

'creates AWS Cloud Map service for Private DNS namespace with network load balanced fargate service'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
cluster.addDefaultCloudMapNamespace({
name: 'foo.com',
type: cloudmap.NamespaceType.DNS_PRIVATE
});

new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
containerPort: 8000,
image: ecs.ContainerImage.fromRegistry('hello'),
},
cloudMapOptions: {
name: 'myApp',
},
memoryLimitMiB: 512,
});

// THEN
expect(stack).to(haveResource("AWS::ECS::Service", {
ServiceRegistries: [
{
RegistryArn: {
"Fn::GetAtt": [
"ServiceCloudmapServiceDE76B29D",
"Arn"
]
}
}
]
}));

expect(stack).to(haveResource('AWS::ServiceDiscovery::Service', {
DnsConfig: {
DnsRecords: [
{
TTL: 60,
Type: "A"
}
],
NamespaceId: {
'Fn::GetAtt': [
'EcsClusterDefaultServiceDiscoveryNamespaceB0971B2F',
'Id'
]
},
RoutingPolicy: 'MULTIVALUE'
},
HealthCheckCustomConfig: {
FailureThreshold: 1
},
Name: "myApp",
NamespaceId: {
'Fn::GetAtt': [
'EcsClusterDefaultServiceDiscoveryNamespaceB0971B2F',
'Id'
]
}
}));

test.done();
},

'test Fargate loadbalanced construct'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down