|
| 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(); |
0 commit comments