Skip to content

Commit ae03ddb

Browse files
SoManyHsrix0rrr
authored andcommitted
feat: add a new construct library for ECS (#1058)
Add a new construct library to start services on ECS, both on EC2 and Fargate-based clusters. Containers can be started from images that are publicly available on DockerHub, images in ECR repositories, and images built directly from sources that are stored in source code next to the CDK app. ECS services can be used as load balancer targets, and there are higher-level constructs available to make it easy to start a service behind a load balancer. BREAKING CHANGE: the ec2.Connections object has been changed to be able to manage multiple security groups. The relevant property has been changed from `securityGroup` to `securityGroups` (an array of security group objects).
1 parent 0e3b217 commit ae03ddb

File tree

86 files changed

+9392
-246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+9392
-246
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pack
1010
coverage
1111
.nyc_output
1212
.LAST_BUILD
13+
*.swp
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.LAST_BUILD
2-
*.snk
2+
hello-cdk-ecs/cdk.json
3+
*.snk
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "fargate-service.yml"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# applet is loaded from the local ./test-applet.js file
2+
applets:
3+
LoadBalancedFargateService:
4+
type: @aws-cdk/aws-ecs:LoadBalancedFargateServiceApplet
5+
properties:
6+
image: 'amazon/amazon-ecs-sample'
7+
cpu: "2048"
8+
memoryMiB: "1024"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ec2 = require('@aws-cdk/aws-ec2');
2+
import { InstanceType } from '@aws-cdk/aws-ec2';
3+
import ecs = require('@aws-cdk/aws-ecs');
4+
import cdk = require('@aws-cdk/cdk');
5+
6+
class BonjourECS extends cdk.Stack {
7+
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
8+
super(parent, name, props);
9+
10+
// For better iteration speed, it might make sense to put this VPC into
11+
// a separate stack and import it here. We then have two stacks to
12+
// deploy, but VPC creation is slow so we'll only have to do that once
13+
// and can iterate quickly on consuming stacks. Not doing that for now.
14+
const vpc = new ec2.VpcNetwork(this, 'MyVpc', { maxAZs: 2 });
15+
const cluster = new ecs.Cluster(this, 'Ec2Cluster', { vpc });
16+
cluster.addDefaultAutoScalingGroupCapacity({
17+
instanceType: new InstanceType("t2.xlarge"),
18+
instanceCount: 3,
19+
});
20+
21+
// Instantiate ECS Service with just cluster and image
22+
const ecsService = new ecs.LoadBalancedEc2Service(this, "Ec2Service", {
23+
cluster,
24+
memoryLimitMiB: 512,
25+
image: ecs.DockerHub.image("amazon/amazon-ecs-sample"),
26+
});
27+
28+
// Output the DNS where you can access your service
29+
new cdk.Output(this, 'LoadBalancerDNS', { value: ecsService.loadBalancer.dnsName });
30+
}
31+
}
32+
33+
const app = new cdk.App();
34+
35+
new BonjourECS(app, 'Bonjour');
36+
37+
app.run();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ec2 = require('@aws-cdk/aws-ec2');
2+
import ecs = require('@aws-cdk/aws-ecs');
3+
import cdk = require('@aws-cdk/cdk');
4+
5+
class BonjourFargate extends cdk.Stack {
6+
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
7+
super(parent, name, props);
8+
9+
// Create VPC and Fargate Cluster
10+
// NOTE: Limit AZs to avoid reaching resource quotas
11+
const vpc = new ec2.VpcNetwork(this, 'MyVpc', { maxAZs: 2 });
12+
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
13+
14+
// Instantiate Fargate Service with just cluster and image
15+
const fargateService = new ecs.LoadBalancedFargateService(this, "FargateService", {
16+
cluster,
17+
image: ecs.DockerHub.image("amazon/amazon-ecs-sample"),
18+
});
19+
20+
// Output the DNS where you can access your service
21+
new cdk.Output(this, 'LoadBalancerDNS', { value: fargateService.loadBalancer.dnsName });
22+
}
23+
}
24+
25+
const app = new cdk.App();
26+
27+
new BonjourFargate(app, 'Bonjour');
28+
29+
app.run();

examples/cdk-examples-typescript/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"@aws-cdk/aws-cognito": "^0.14.1",
2929
"@aws-cdk/aws-dynamodb": "^0.14.1",
3030
"@aws-cdk/aws-ec2": "^0.14.1",
31+
"@aws-cdk/aws-ecs": "^0.14.1",
3132
"@aws-cdk/aws-elasticloadbalancing": "^0.14.1",
33+
"@aws-cdk/aws-elasticloadbalancingv2": "^0.14.1",
3234
"@aws-cdk/aws-iam": "^0.14.1",
3335
"@aws-cdk/aws-lambda": "^0.14.1",
3436
"@aws-cdk/aws-neptune": "^0.14.1",

packages/@aws-cdk/assets/lib/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class Asset extends cdk.Construct {
124124
// for tooling to be able to package and upload a directory to the
125125
// s3 bucket and plug in the bucket name and key in the correct
126126
// parameters.
127-
const asset: cxapi.AssetMetadataEntry = {
127+
const asset: cxapi.FileAssetMetadataEntry = {
128128
path: this.assetPath,
129129
id: this.uniqueId,
130130
packaging: props.packaging,

packages/@aws-cdk/aws-applicationautoscaling/lib/base-scalable-attribute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ export abstract class BaseScalableAttribute extends cdk.Construct {
6262
/**
6363
* Scale out or in based on time
6464
*/
65-
protected scaleOnSchedule(id: string, props: ScalingSchedule) {
65+
protected doScaleOnSchedule(id: string, props: ScalingSchedule) {
6666
this.target.scaleOnSchedule(id, props);
6767
}
6868

6969
/**
7070
* Scale out or in based on a metric value
7171
*/
72-
protected scaleOnMetric(id: string, props: BasicStepScalingPolicyProps) {
72+
protected doScaleOnMetric(id: string, props: BasicStepScalingPolicyProps) {
7373
this.target.scaleOnMetric(id, props);
7474
}
7575

7676
/**
7777
* Scale out or in in order to keep a metric around a target value
7878
*/
79-
protected scaleToTrackMetric(id: string, props: BasicTargetTrackingScalingPolicyProps) {
79+
protected doScaleToTrackMetric(id: string, props: BasicTargetTrackingScalingPolicyProps) {
8080
this.target.scaleToTrackMetric(id, props);
8181
}
8282
}

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class AutoScalingGroup extends cdk.Construct implements cdk.ITaggable, el
183183
vpc: props.vpc,
184184
allowAllOutbound: props.allowAllOutbound !== false
185185
});
186-
this.connections = new ec2.Connections({ securityGroup: this.securityGroup });
186+
this.connections = new ec2.Connections({ securityGroups: [this.securityGroup] });
187187
this.securityGroups.push(this.securityGroup);
188188
this.tags = new TagManager(this, {initialTags: props.tags});
189189
this.tags.setTag(NAME_TAG, this.path, { overwrite: false });

0 commit comments

Comments
 (0)