Skip to content

Instantly share code, notes, and snippets.

@huksley
Last active July 8, 2022 12:13
Show Gist options
  • Save huksley/01b3d43194d1057a129befcd09766f9f to your computer and use it in GitHub Desktop.
Save huksley/01b3d43194d1057a129befcd09766f9f to your computer and use it in GitHub Desktop.
Create AWS ECS cluster with CDK (short)
class CdkStack extends Stack {
constructor(scope, id, hostname) {
super(scope, id, props);
const vpc = ec2.Vpc.fromLookup(this, "vpc", { vpcId: process.env.VPC_ID });
const cluster = new ecs.Cluster(this, "cluster", { vpc, enableFargateCapacityProviders: true });
const repository = ecr.Repository.fromRepositoryName(this, "images", image);
const lb = new patterns.ApplicationLoadBalancedFargateService(this, "service", {
cluster,
cpu: 1024,
desiredCount: 1,
memoryLimitMiB: 2048,
domainName: hostname,
domainZone: route53.HostedZone.fromHostedZoneAttributes(this, "zone", {
hostedZoneId: process.env.ZONE_ID,
zoneName: process.env.ZONE_NAME
}),
listenerPort: 443,
protocol: "HTTPS",
taskImageOptions: {
image: ecs.ContainerImage.fromEcrRepository(repository),
containerPort: 3000,
executionRole: taskRole,
taskRole,
logDriver: ecs.LogDrivers.awsLogs({
mode: ecs.AwsLogDriverMode.NON_BLOCKING
})
}
});
lb.loadBalancer.addRedirect({
sourcePort: 80,
sourceProtocol: "HTTP",
targetPort: 443,
targetProtocol: "HTTPS"
});
}
}
@huksley
Copy link
Author

huksley commented Jul 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment