Created
December 7, 2018 04:21
-
-
Save taketo957/61f094fb0ee6bb97c9b6b600a270c4ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import cdk = require('@aws-cdk/cdk'); | |
import { StackProps } from '@aws-cdk/cdk'; | |
import { VpcNetworkRefProps, VpcNetwork, VpcNetworkRef } from '@aws-cdk/aws-ec2'; | |
import { Cluster, ImportedClusterProps, FargateTaskDefinition, FargateService } from '@aws-cdk/aws-ecs'; | |
import { cloudformation as route53, PublicHostedZone } from '@aws-cdk/aws-route53'; | |
import { ApplicationLoadBalancer, ApplicationListenerRefProps, ApplicationLoadBalancerRefProps, ApplicationListener, ApplicationTargetGroup, ApplicationProtocol } from '@aws-cdk/aws-elasticloadbalancingv2'; | |
class InfraStack extends cdk.Stack { | |
public vpcRef: VpcNetworkRefProps | |
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) { | |
super(parent, name, props); | |
const vpc = new VpcNetwork(this, 'VPC') | |
this.vpcRef = vpc.export() | |
} | |
} | |
class Route53Stack extends cdk.Stack { | |
constructor(parent: cdk.App, name: string, props?: StackProps) { | |
super(parent, name, props); | |
const exampleZone = new PublicHostedZone(this, 'ExampleHostedZone', { | |
zoneName: 'example.com', | |
}) | |
new route53.RecordSetResource(this, 'AliasRecord', { | |
hostedZoneId: exampleZone.hostedZoneId, | |
name: '*.example.com.', | |
type: 'A', | |
aliasTarget: { | |
hostedZoneId: '', | |
dnsName: 'ALB DNS Name', | |
}, | |
}) | |
} | |
} | |
interface EcsClusterStackProps extends cdk.StackProps { | |
vpcNetworkRefProps: VpcNetworkRefProps | |
} | |
class EcsClusterStack extends cdk.Stack { | |
public ecsClusterProps: ImportedClusterProps | |
public listenerProps: ApplicationListenerRefProps | |
public albProps: ApplicationLoadBalancerRefProps | |
constructor(parent: cdk.App, name: string, props: EcsClusterStackProps) { | |
super(parent, name, props); | |
const vpc = VpcNetworkRef.import(this, 'VPC', props.vpcNetworkRefProps) | |
const ecsCluster = new Cluster(this, 'EcsCluster', { | |
vpc, | |
clusterName: 'example-cluster', | |
}) | |
const alb = new ApplicationLoadBalancer(this, 'PublicLoadBalancer', { | |
vpc, | |
internetFacing: true, | |
}) | |
const listener = alb.addListener('PublicLoadBalancerListener', { | |
port: 80, | |
open: false, | |
}) | |
this.ecsClusterProps = ecsCluster.export() | |
this.listenerProps = listener.export() | |
} | |
} | |
interface EcsServiceStackProps extends cdk.StackProps { | |
vpcNetworkRefProps: VpcNetworkRefProps | |
ecsClusterProps: ImportedClusterProps | |
listenerProps: ApplicationListenerRefProps | |
hostName: string | |
branchName: string | |
} | |
class EcsServiceStack extends cdk.Stack { | |
constructor(parent: cdk.App, name: string, props: EcsServiceStackProps) { | |
super(parent, name, props); | |
const vpc = VpcNetworkRef.import(this, 'VPC', props.vpcNetworkRefProps) | |
const ecsCluster = Cluster.import(this, 'EcsCluster', props.ecsClusterProps) | |
const listener = ApplicationListener.import(this, 'ApplicationLoadBalancerListener', props.listenerProps) | |
const serverTaskDefinition = new FargateTaskDefinition(this, 'ServerTaskDefinition') | |
// container定義は省略 | |
const service = new FargateService(this, 'EcsService', { | |
cluster: ecsCluster, | |
serviceName: 'example-service', | |
taskDefinition: serverTaskDefinition, | |
}) | |
const target = new ApplicationTargetGroup(this, 'TargetGroup', { | |
protocol: ApplicationProtocol.Http, | |
targets: [service], | |
vpc, | |
}) | |
listener.addTargetGroups('TargetGroup', { | |
targetGroups: [target], | |
hostHeader: props.hostName, | |
pathPattern: '*', | |
priority: '被らないように動的に決める', | |
}) | |
} | |
} | |
const app = new cdk.App(); | |
const infra = new InfraStack(app, 'InfraStack'); | |
const cluster = new EcsClusterStack (app, 'EcsClusterStack', { | |
vpcNetworkRefProps: infra.vpcRef, | |
}) | |
new EcsServiceStack(app, 'EcsService', { | |
vpcNetworkRefProps: infra.vpcRef, | |
ecsClusterProps: cluster.ecsClusterProps, | |
listenerProps: cluster.listenerProps, | |
hostName: 'hostname', | |
branchName: 'branchname' | |
}) | |
new Route53Stack(app, 'Route53Stack'); | |
app.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment