-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVmApp.lw
335 lines (319 loc) · 9.9 KB
/
VmApp.lw
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
export
type VMApp
type VMAppSecurityGroups
new
import Fugue.AWS.AutoScaling as AutoScaling
import Fugue.AWS.EC2 as EC2
import Fugue.AWS.ELB as ELB
import Fugue.AWS.IAM as IAM
import Fugue.AWS.RDS as RDS
import Fugue.AWS.DynamoDB as DynamoDB
import Fugue.AWS as AWS
import Fugue.Core.Vars as Vars
type VMApp:
| VMApp
elb: ELB.LoadBalancer
asg: AutoScaling.AutoScalingGroup
sgs: VMAppSecurityGroups
rds: Optional<RDS.DBInstance>
ddb: Optional<DynamoDB.Table>
type VMAppSecurityGroups:
elbSg: EC2.SecurityGroup
asgSg: EC2.SecurityGroup
rdsSg: Optional<EC2.SecurityGroup>
clientSg: EC2.SecurityGroup
fun amiForRegion(region: AWS.Region) -> String:
case region of
| AWS.Us-east-1 -> "ami-a60c23b0"
| AWS.Us-west-1 -> "ami-96f1dcf6"
| AWS.Us-west-2 -> "ami-7646530f"
| AWS.Eu-west-1 -> "ami-af455dc9"
| _ -> AWS.Region.toString(region) ++ " is not supported."
fun makeVMAppSgs {
name: String,
vpc: EC2.Vpc,
withRDS: Bool,
extraTags: List<AWS.Tag>
} -> VMAppSecurityGroups:
let elbSg: EC2.SecurityGroup.new {
description: name ++ "-elb-sg",
ipPermissions: [
EC2.IpPermission.http(EC2.IpPermission.Target.all),
EC2.IpPermission.https(EC2.IpPermission.Target.all),
],
tags: List.concat([AWS.tag("Name", name ++ "-elb-sg")], extraTags),
vpc: vpc,
}
let asgSg: EC2.SecurityGroup.new {
description: name ++ "-asg-sg",
ipPermissions: [
EC2.IpPermission.http(EC2.IpPermission.Target.securityGroups([elbSg])),
EC2.IpPermission.https(EC2.IpPermission.Target.securityGroups([elbSg])),
EC2.IpPermission.ssh(EC2.IpPermission.Target.securityGroups([clientSg]))
],
tags: List.concat([AWS.tag("Name", name ++ "-asg-sg")], extraTags),
vpc: vpc,
}
let rdsSg:
if withRDS then
Optional(EC2.SecurityGroup.new {
description: name ++ "-rds-sg",
ipPermissions: [
EC2.IpPermission.mysql(EC2.IpPermission.Target.securityGroups([asgSg])),
EC2.IpPermission.mysql(EC2.IpPermission.Target.securityGroups([clientSg]))
],
tags: List.concat([AWS.tag("Name", name ++ "-rds-sg")], extraTags),
vpc: vpc,
})
else
None
let clientSg: EC2.SecurityGroup.new {
description: name ++ "-client-sg",
ipPermissions: [],
tags: List.concat([AWS.tag("Name", name ++ "-client-sg")], extraTags),
vpc: vpc,
}
{
elbSg: elbSg,
asgSg: asgSg,
rdsSg: rdsSg,
clientSg: clientSg,
}
fun ec2Policy(name: String, region: String) -> IAM.Policy:
IAM.Policy.new {
policyName: name ++ "-ec2-policy",
policyDocument:
Template.render {
data: {name: name, region: region},
template:
'{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ec2:*"],
"Resource": "*"
}
]
}'
},
}
fun ddbPolicy(name: String, region: String) -> IAM.Policy:
IAM.Policy.new {
policyName: name ++ "-ddb-policy",
policyDocument:
Template.render {
data: {name: name, region: region},
template:
'{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["dynamodb:*"],
"Resource": "arn:aws:dynamodb:{{region}}:*:table/{{name}}-table"
}
]
}'
},
}
fun rdsPolicy(name: String, region: String) -> IAM.Policy:
IAM.Policy.new {
policyName: name ++ "-rds-policy",
policyDocument:
Template.render {
data: {name: name, region: region},
template:
'{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["rds-db:connect*"],
"Resource": "arn:aws:rds:{{region}}:*:db:{{name}}-dbInstance"
}
]
}'
},
}
fun makeVMAppIamProfile {
name: String,
region: String,
withDDB: Bool,
withRDS: Bool,
} -> IAM.InstanceProfile:
let webAppPolicies: [
policy for (policy, enable) in [
(ec2Policy(name,region), True),
(ddbPolicy(name,region), withDDB),
(rdsPolicy(name,region), withRDS)
] if enable
]
let webAppRole:
IAM.Role.new {
roleName: name ++ "-role",
rolePolicies: webAppPolicies,
assumeRolePolicyDocument: IAM.Policy.AssumeRole.ec2,
}
IAM.InstanceProfile.new {
instanceProfileName: name ++ "-profile",
roles: [webAppRole],
}
fun new {
name: String,
subnets: List<EC2.Subnet>,
vpc: Optional<EC2.Vpc>,
region: Optional<AWS.Region>,
keyName: Optional<String>,
image: Optional<String>,
userData: Optional<String>,
instanceType: Optional<EC2.InstanceType>,
managementSgs: Optional<List<EC2.SecurityGroup>>,
withRDS: Optional<Bool>,
dbEngine: Optional<RDS.Engine>,
dbInstanceClass: Optional<RDS.DBInstanceClass>,
dbStorageType: Optional<EC2.VolumeType>,
dbAllocatedStorage: Optional<Int>,
dbMultiAZ: Optional<Bool>,
dbName: Optional<String>,
dbMasterUsername: Optional<String>,
dbMasterUserPassword: Optional<Vars.Password>,
withDDB: Optional<Bool>,
ddbProvisionedReadThroughput: Optional<Int>,
ddbProvisionedWriteThroughput: Optional<Int>,
dbAllocatedStorage: Optional<Int>,
tags: Optional<List<AWS.Tag>>
} -> VMApp:
let _vpc: Optional.unpack(List.index(subnets,0).(EC2.Subnet).vpc, vpc)
let _region: Optional.unpack(_vpc.(EC2.Vpc).region, region)
let _keyName: Optional.unpack(name, keyName)
let _image: Optional.unpack(amiForRegion(_region), image)
let _userData: Optional.unpack(" ", userData)
let _instanceType: Optional.unpack(EC2.T2_micro, instanceType)
let _withRDS: Optional.unpack(False, withRDS)
let _dbEngine: Optional.unpack(RDS.MySQL, dbEngine)
let _dbInstanceClass: Optional.unpack(RDS.DB_T2_MICRO, dbInstanceClass)
let _dbStorageType: Optional.unpack(EC2.Standard, dbStorageType)
let _dbAllocatedStorage: Optional.unpack(10, dbAllocatedStorage)
let _dbMultiAZ: Optional.unpack(False, dbMultiAZ)
let _dbName: Optional.unpack(name, dbName)
let _dbMasterUsername: Optional.unpack("root", dbMasterUsername)
let _dbMasterUserPassword: Optional.unpack(Vars.VarsKey({key: "/rds/password"}), dbMasterUserPassword)
let _managementSgs: Optional.unpack([], managementSgs)
let _withDDB: Optional.unpack(False, withDDB)
let _ddbProvisionedReadThroughput: Optional.unpack(10, ddbProvisionedReadThroughput)
let _ddbProvisionedWriteThroughput: Optional.unpack(10, ddbProvisionedWriteThroughput)
let _extraTags: Optional.unpack([], tags)
let sgs: makeVMAppSgs {
name: name,
vpc: _vpc,
withRDS: _withRDS,
extraTags: _extraTags,
}
let profile: makeVMAppIamProfile {
name: name,
region: AWS.Region.toString(_region),
withDDB: _withDDB,
withRDS: _withRDS,
}
let elb: ELB.LoadBalancer.new {
loadBalancerName: name ++ "-elb",
listeners: [
ELB.Listener.new {
protocol: ELB.TCP,
loadBalancerPort: 80,
instancePort: 80,
instanceProtocol: ELB.TCP,
},
ELB.Listener.new {
protocol: ELB.TCP,
loadBalancerPort: 443,
instancePort: 443,
instanceProtocol: ELB.TCP,
}
],
healthCheck: ELB.HealthCheck.tcp {
port: 80,
interval: 5,
timeout: 2,
unhealthyThreshold: 2,
healthyThreshold: 2,
},
subnets: subnets,
securityGroups: [sgs.elbSg],
tags: List.concat([AWS.tag("Name", name)], _extraTags),
}
let lc: AutoScaling.LaunchConfiguration.new {
image: _image,
securityGroups: List.concat([sgs.asgSg], _managementSgs),
keyName: _keyName,
instanceType: _instanceType,
associatePublicIpAddress: True,
iamInstanceProfile: profile,
userData: _userData,
}
let asg: AutoScaling.AutoScalingGroup.new {
subnets: subnets,
loadBalancers: [elb],
minSize: 1,
maxSize: 2,
defaultCooldown: 300,
healthCheckType: AutoScaling.Ec2,
launchConfiguration: lc,
tags: List.concat([AWS.tag("Name", name)], _extraTags),
terminationPolicies: [AutoScaling.ClosestToNextInstanceHour],
enabledMetrics: [
AutoScaling.GroupInServiceInstances,
AutoScaling.GroupTotalInstances,
]
}
let rds:
if _withRDS then
let subnetGroup: RDS.DBSubnetGroup.new {
name: name,
description: name ++ "-subnet-group",
subnets: subnets,
tags: List.concat([AWS.tag("Name", name)], _extraTags),
}
let securityGroups: case sgs.rdsSg of
| None -> []
| Optional sg -> [sg]
Optional(RDS.DBInstance.new {
dbName: _dbName,
dbInstanceIdentifier: name ++ "-dbInstance",
dbInstanceClass: _dbInstanceClass,
engine: _dbEngine,
masterUsername: _dbMasterUsername,
allocatedStorage: _dbAllocatedStorage,
securityGroups: securityGroups,
availabilityZone: None,
dbSubnetGroup: subnetGroup,
multiAZ: _dbMultiAZ,
publiclyAccessible: True,
storageType: _dbStorageType,
masterUserPassword: _dbMasterUserPassword,
tags: List.concat([AWS.tag("Name", name)], _extraTags),
})
else
None
let ddb:
if _withDDB then
Optional(DynamoDB.Table.new {
name: name ++ "-table",
attributes: {"PropertyName": DynamoDB.S},
schema: {"PropertyName": DynamoDB.HASH},
provisionedThroughput: {
read: _ddbProvisionedReadThroughput,
write: _ddbProvisionedWriteThroughput ,
},
region: _region,
})
else
None
VMApp {
sgs: sgs,
elb: elb,
asg: asg,
rds: rds,
ddb: ddb,
}