forked from wolfeidau/exitus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexitus.ts
156 lines (133 loc) · 5.47 KB
/
exitus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { resolve } from 'path';
import { Stack, App, StackProps, CfnOutput, Duration } from '@aws-cdk/core'
import { DatabaseInstance, DatabaseInstanceEngine } from '@aws-cdk/aws-rds'
import { ManagedPolicy, ServicePrincipal, Role } from '@aws-cdk/aws-iam'
import { Vpc, InstanceType, InstanceClass, SubnetType, InstanceSize, Port } from '@aws-cdk/aws-ec2';
import { Cluster, ContainerImage, Secret } from '@aws-cdk/aws-ecs';
import { ApplicationLoadBalancedFargateService } from '@aws-cdk/aws-ecs-patterns';
import { HostedZone, ARecord, AddressRecordTarget } from '@aws-cdk/aws-route53';
import { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets';
import { Certificate } from '@aws-cdk/aws-certificatemanager';
const appName = "Exitus"
const stage = process.env.STAGE || 'dev';
const branch = process.env.BRANCH || 'master';
const oauthClientID = process.env.OAUTH_CLIENT_ID || '';
const oauthClientSecret = process.env.OAUTH_CLIENT_SECRET || '';
const openidProviderURL = process.env.OPENID_PROVIDER_URL || '';
const dbPort = 5432;
const domainName = process.env.DOMAIN_NAME || ''
const hostedZoneID = process.env.HOSTED_ZONE_ID || ''
const acmCertificateArn = process.env.ACM_CERTIFICATE_ARN || ''
interface RDSStackProps extends StackProps {
vpc: Vpc
}
interface ServiceStackProps extends StackProps {
vpc: Vpc
db: DatabaseInstance;
}
class VPCStack extends Stack {
vpc: Vpc;
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
// Network to run everything in
this.vpc = new Vpc(this, `${appName}Vpc`, {
cidr: "10.0.0.0/16",
maxAzs: 2,
natGateways: 1,
subnetConfiguration: [
{
name: `${appName.toLowerCase()}-${stage}-public`,
subnetType: SubnetType.PUBLIC,
},
{
name: `${appName.toLowerCase()}-${stage}-private`,
subnetType: SubnetType.PRIVATE,
},
{
name: `${appName.toLowerCase()}-${stage}-isolated`,
subnetType: SubnetType.ISOLATED,
},
]
});
}
}
class RDSStack extends Stack {
db: DatabaseInstance;
constructor(scope: App, id: string, props?: RDSStackProps) {
super(scope, id, props);
this.db = new DatabaseInstance(this, `${appName}DB`, {
databaseName: appName.toLowerCase(),
masterUsername: appName.toLowerCase(),
engine: DatabaseInstanceEngine.POSTGRES,
instanceClass: InstanceType.of(
InstanceClass.BURSTABLE3, InstanceSize.SMALL
),
vpc: props!.vpc,
vpcPlacement: {
subnetType: SubnetType.PRIVATE
},
port: dbPort,
})
}
}
class ServiceStack extends Stack {
constructor(scope: App, id: string, props?: ServiceStackProps) {
super(scope, id, props);
const cluster = new Cluster(this, `${appName}Cluster`, { vpc: props!.vpc });
const executionRole = new Role(this, `${appName}ExecutionRole`, {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonECSTaskExecutionRolePolicy')
]
})
const taskRole = new Role(this, `${appName}TaskRole`, {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
})
const certificate = Certificate.fromCertificateArn(this,`${appName}Cert`, acmCertificateArn)
const containerSvc = new ApplicationLoadBalancedFargateService(this, `${appName}FargateService`, {
cluster,
image: ContainerImage.fromAsset(resolve(__dirname, './deploy')),
environment: {
OAUTH_CLIENT_ID: oauthClientID,
OAUTH_CLIENT_SECRET: oauthClientSecret,
OPENID_PROVIDER_URL: openidProviderURL,
BRANCH: branch,
STAGE: stage,
ADDR: ":80",
},
secrets : {
DB_SECRET: Secret.fromSecretsManager(props!.db.secret!),
},
certificate,
enableLogging: true,
containerPort: 80,
executionRole,
taskRole,
});
containerSvc.targetGroup.healthCheck = {
interval: Duration.seconds(10),
path: "/healthz",
timeout: Duration.seconds(5),
}
const zone = HostedZone.fromHostedZoneId(this, 'MyZone', hostedZoneID);
const siteDomain = `${appName.toLowerCase()}-${stage}-${branch}` + '.' + domainName;
new ARecord(this, `${appName}Record`, {
zone,
target: AddressRecordTarget.fromAlias(new LoadBalancerTarget(containerSvc.loadBalancer)),
recordName: siteDomain,
})
containerSvc.service.connections.allowTo(props!.db!, Port.tcp(dbPort))
new CfnOutput(this, 'LoadBalancerDNS', { value: containerSvc.loadBalancer.loadBalancerDnsName });
new CfnOutput(this, 'SiteDomain', { value: siteDomain });
}
}
const app = new App();
const vpcStack = new VPCStack(app, `${appName.toLowerCase()}-vpc-${stage}`, {});
const rdsStack = new RDSStack(app, `${appName.toLowerCase()}-rds-${stage}`, {
vpc: vpcStack.vpc,
});
new ServiceStack(app, `${appName.toLowerCase()}-service-${stage}`, {
vpc: vpcStack.vpc,
db: rdsStack.db,
});
app.synth();