Skip to content

Instantly share code, notes, and snippets.

@hirokihello
Last active December 19, 2019 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokihello/9df1248c7152b8c7bdce93e4578a2356 to your computer and use it in GitHub Desktop.
Save hirokihello/9df1248c7152b8c7bdce93e4578a2356 to your computer and use it in GitHub Desktop.
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import ecr = require('@aws-cdk/aws-ecr');
import certmgr = require('@aws-cdk/aws-certificatemanager')
import route53 = require('@aws-cdk/aws-route53');
import targets = require('@aws-cdk/aws-route53-targets');
import ecs_patterns = require('@aws-cdk/aws-ecs-patterns');
import cdk = require('@aws-cdk/core');
export class CdkSampleStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2 });
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "fargateService", {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
taskImageOptions: {
image: ecs.ContainerImage.fromEcrRepository(repository),
},
});
const zone = new route53.PublicHostedZone(this, 'HostedZone', {
zoneName: 'hirokihello.com'
});
new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new targets.LoadBalancerTarget(fargateService.loadBalancer)),
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomainName(domainName)),
});
const hostedZone = route53.HostedZone.fromLookup(this, 'CreatedHostedZone', {
domainName: 'hirokihello.com',
privateZone: false
});
const certificate = new certmgr.DnsValidatedCertificate(this, 'TestCertificate', {
domainName: 'hirokihello.com',
hostedZone,
});
const listener = fargateService.loadBalancer.addListener('Listener',
{
port: 443,
}
);
listener.addTargetGroups(
fargateService.loadBalancer.loadBalancerArn,
{
targetGroups: [fargateService.targetGroup],
});
listener.addCertificateArns(
fargateService.loadBalancer.loadBalancerArn,
[certificate.certificateArn],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment