Last active
December 7, 2018 06:43
-
-
Save taketo957/71820412fb491ab5e0373a5aa64fb1ff 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
import { VpcNetwork, VpcNetworkRef, VpcNetworkRefProps } from '@aws-cdk/aws-ec2' | |
import { ImportRepositoryProps, Repository } from '@aws-cdk/aws-ecr' | |
import { Cluster, ContainerImage, FargateService, FargateTaskDefinition, ImportedClusterProps } from '@aws-cdk/aws-ecs' | |
import { | |
ApplicationListener, | |
ApplicationListenerRefProps, | |
ApplicationLoadBalancer, | |
ApplicationLoadBalancerRefProps, | |
ApplicationProtocol, | |
ApplicationTargetGroup, | |
} from '@aws-cdk/aws-elasticloadbalancingv2' | |
import { cloudformation as route53, PublicHostedZone } from '@aws-cdk/aws-route53' | |
import cdk = require('@aws-cdk/cdk') | |
import { StackProps } from '@aws-cdk/cdk' | |
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', | |
}, | |
}) | |
} | |
} | |
class EcrStack extends cdk.Stack { | |
public readonly repositoryRefProps: ImportRepositoryProps | |
constructor(parent: cdk.App, name: string, props?: StackProps) { | |
super(parent, name, props) | |
const repository = new Repository(this, 'Repository') | |
this.repositoryRefProps = repository.export() | |
} | |
} | |
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 defaultTargetGroup = new ApplicationTargetGroup(this, 'DefaultTargetGroup', { | |
port: 8000, | |
protocol: ApplicationProtocol.Http, | |
vpc, | |
}) | |
const listener = alb.addListener('PublicLoadBalancerListener', { | |
port: 80, | |
open: false, | |
defaultTargetGroups: [defaultTargetGroup], | |
}) | |
this.ecsClusterProps = ecsCluster.export() | |
this.listenerProps = listener.export() | |
} | |
} | |
interface EcsServiceStackProps extends cdk.StackProps { | |
vpcNetworkRefProps: VpcNetworkRefProps | |
ecsClusterProps: ImportedClusterProps | |
listenerProps: ApplicationListenerRefProps | |
containerImageRepositoryRefProps: ImportRepositoryProps | |
} | |
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 containerImageRepository = Repository.import( | |
this, | |
'ContainerImageRepository', | |
props.containerImageRepositoryRefProps | |
) | |
const branchname = this.getContext('devhost:branchname') | |
const hostname = `${branchname}.example.com` | |
const serverTaskDefinition = new FargateTaskDefinition(this, 'ServerTaskDefinition') | |
const container = serverTaskDefinition.addContainer('ServerTaskContainer', { | |
image: ContainerImage.fromEcrRepository(containerImageRepository, branchname), | |
}) | |
container.addPortMappings({ | |
containerPort: 8000, | |
}) | |
const service = new FargateService(this, 'EcsService', { | |
cluster: ecsCluster, | |
serviceName: 'example-service', | |
taskDefinition: serverTaskDefinition, | |
}) | |
const target = new ApplicationTargetGroup(this, 'TargetGroup', { | |
port: 8000, | |
protocol: ApplicationProtocol.Http, | |
targets: [service], | |
vpc, | |
}) | |
listener.addTargetGroups('TargetGroup', { | |
targetGroups: [target], | |
hostHeader: hostname, | |
pathPattern: '*', | |
priority: '被らないように動的に決める', | |
}) | |
} | |
} | |
const app = new cdk.App() | |
const infra = new InfraStack(app, 'InfraStack') | |
const ecr = new EcrStack(app, 'EcrStack') | |
const cluster = new EcsClusterStack(app, 'EcsClusterStack', { | |
vpcNetworkRefProps: infra.vpcRef, | |
}) | |
new EcsServiceStack(app, 'EcsService', { | |
vpcNetworkRefProps: infra.vpcRef, | |
ecsClusterProps: cluster.ecsClusterProps, | |
listenerProps: cluster.listenerProps, | |
containerImageRepositoryRefProps: ecr.repositoryRefProps, | |
}) | |
new Route53Stack(app, 'Route53Stack') | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment