-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
333 lines (277 loc) · 9.01 KB
/
__main__.py
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
"""An AWS Python Pulumi program"""
import pulumi
import pulumi_aws as aws
NAME = 'pulumi-aws-example'
# VPC Resources
zones = aws.get_availability_zones(state="available")
## VPC
shared_vpc = aws.ec2.Vpc(
resource_name=f'vpc-{NAME}',
cidr_block="10.0.0.0/16",
enable_dns_hostnames=True,
enable_dns_support=True
)
## Subnet
### public subnet
subnet_public1 = aws.ec2.Subnet(
resource_name=f'subnet-public-1-{NAME}',
availability_zone=zones.names[0],
cidr_block="10.0.10.0/24",
vpc_id=shared_vpc.id,
map_public_ip_on_launch=True,
tags={"Name":"public_1"}
)
subnet_public2 = aws.ec2.Subnet(
resource_name=f'subnet-gateway-2-{NAME}',
availability_zone=zones.names[2],
cidr_block="10.0.11.0/24",
vpc_id=shared_vpc.id,
map_public_ip_on_launch=True,
tags={"Name":"public_2"}
)
## Gateway
internet_gateway = aws.ec2.InternetGateway(
resource_name=f'internet-gateway-{NAME}',
vpc_id=shared_vpc.id
)
# NAT-Gateway에 붙이는 Elastic IP
gateway_eip = aws.ec2.Eip(
resource_name=f'gateway-eip-{NAME}',
vpc=True
)
nat_gateway = aws.ec2.NatGateway(
resource_name=f'nat-gateway-{NAME}',
allocation_id=gateway_eip.id, # eip 할당
subnet_id=subnet_public1.id # public subnet 중 하나에 설치
)
### private subnet
subnet_app1 = aws.ec2.Subnet(
resource_name=f'subnet-app1-{NAME}',
availability_zone=zones.names[0], # AZ-a
cidr_block="10.0.12.0/24",
vpc_id=shared_vpc.id,
tags={"Name":"app_1"}
)
subnet_app2 = aws.ec2.Subnet(
resource_name=f'subnet-app2-{NAME}',
availability_zone=zones.names[2], # AZ-c
cidr_block="10.0.13.0/24",
vpc_id=shared_vpc.id,
tags = {"Name": "app_2"}
)
subnet_rds1 = aws.ec2.Subnet(
resource_name=f'subnet-rds1-{NAME}',
availability_zone=zones.names[0], # AZ-a
cidr_block="10.0.14.0/24",
vpc_id=shared_vpc.id,
tags = {"Name": "rds_1"}
)
subnet_rds2 = aws.ec2.Subnet(
resource_name=f'subnet-rds2-{NAME}',
availability_zone=zones.names[2], # AZ-c
cidr_block="10.0.15.0/24",
vpc_id=shared_vpc.id,
tags={"Name": "rds_2"}
)
### route
routetable_gateway = aws.ec2.RouteTable(
resource_name=f'routetable-gateway-{NAME}',
vpc_id=shared_vpc.id,
routes=[
{
"cidrBlock": "0.0.0.0/0", # 모든 트래픽에 대하여 인터넷 게이트웨이로 향할 것
"gatewayId": internet_gateway.id
}
],
tags = {"Name": "public-route-table"}
)
routetable_app = aws.ec2.RouteTable(
resource_name=f'routetable-app-{NAME}',
vpc_id=shared_vpc.id,
routes=[
{
"cidrBlock": "0.0.0.0/0",
"gatewayId": nat_gateway.id
}
],
tags = {"Name": "private-app-route-table"}
)
routetable_rds = aws.ec2.RouteTable(
resource_name=f'routetable-rds-{NAME}',
vpc_id=shared_vpc.id,
# DB는 외부와 통신할 일이 없으므로 route 설정을 하지 않습니다.
tags = {"Name": "private-rds-route-table"}
)
table_association_public1 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-public1-{NAME}',
subnet_id=subnet_public1.id,
route_table_id=routetable_gateway)
table_association_public2 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-public2-{NAME}',
subnet_id=subnet_public2.id,
route_table_id=routetable_gateway)
table_association_app1 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-app1-{NAME}',
subnet_id=subnet_app1.id,
route_table_id=routetable_app)
table_association_app2 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-app2-{NAME}',
subnet_id=subnet_app2.id,
route_table_id=routetable_app)
table_association_rds1 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-rds1-{NAME}',
subnet_id=subnet_rds1.id,
route_table_id=routetable_rds)
table_association_rds2 = aws.ec2.RouteTableAssociation(
resource_name=f'table-association-rds2-{NAME}',
subnet_id=subnet_rds2.id,
route_table_id=routetable_rds)
# EC2
## security group
ec2_security_group = aws.ec2.SecurityGroup(
resource_name=f"ec2-security-group-{NAME}",
vpc_id=shared_vpc.id,
# Outbound traffic - health check를 위해 ELB에 아웃바운드를 열어 줍니다.
egress=[{
'from_port': 80,
'to_port': 80,
'protocol': 'tcp',
'cidr_blocks': ['0.0.0.0/0']
}],
# Inbound traffic - http 통신 허용
ingress=[{
'description': 'Allow internet access to instance',
'from_port': 80,
'to_port': 80,
'protocol': 'tcp',
'cidr_blocks': ['0.0.0.0/0']
}]
)
# instance
app1 = aws.ec2.Instance(
resource_name=f"my_app1-{NAME}",
# ami는 직접 지정할 수도 있고, filtering으로 찾을 수도 있습니다.
ami="ami-067abcae434ee508b", # Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
instance_type="t2.micro",
availability_zone=zones.names[0], # AZ-a
vpc_security_group_ids=[ec2_security_group.id], # 여러 security group 적용 가능
subnet_id=subnet_app1.id,
# 추후 health check를 위한 서버 세팅
user_data=f"""#!/bin/bash
echo \"Hello, World -- from {zones.names[0]}!\" > index.html
nohup sudo python3 -m http.server 80 &
""",
tags = {"Name": "app1"}
)
app2 = aws.ec2.Instance(
resource_name=f"my_app2-{NAME}",
ami="ami-067abcae434ee508b", # Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
instance_type="t2.micro",
availability_zone=zones.names[2], # AZ-c
vpc_security_group_ids=[ec2_security_group.id],
subnet_id=subnet_app2.id,
user_data=f"""#!/bin/bash
echo \"Hello, World -- from {zones.names[2]}!\" > index.html
nohup sudo python3 -m http.server 80 &
""",
tags = {"Name": "app2"}
)
# ELB
## security group for elb
elb_security_group = aws.ec2.SecurityGroup(
resource_name=f"elb-security-group-{NAME}",
vpc_id=shared_vpc.id,
# applicaiton Load balancer에 적용되는 Security Group은 명시하지 않아도 자동설정됩니다.
# Outbound traffic - 모든 통신 허용
egress=[{
'from_port': 0,
'to_port': 0,
'protocol': '-1', # all
'cidr_blocks': ['0.0.0.0/0']
}],
# Inbound traffic - http 통신 허용
ingress=[{
'description' : 'Allow internet access to instance',
'from_port' : 80,
'to_port' : 80,
'protocol' : 'tcp',
'cidr_blocks' : ['0.0.0.0/0']
}]
)
## load balancer
# vpc = aws.ec2.get_vpc(id=shared_vpc.id)
# vpc_subnets = aws.ec2.get_subnet_ids(vpc_id=vpc.id)
load_balancer = aws.lb.LoadBalancer(
resource_name=f"elb-{NAME}",
internal=False,
security_groups=[elb_security_group.id],
subnets=[subnet_public1.id,subnet_public2.id],
load_balancer_type="application",
tags = {"Name": "pulumi-lb"}
)
# target group
target_group = aws.lb.TargetGroup(
resource_name="target-group",
port=80, # [로드밸런서 -> 타겟그룹] 요청이 80번 포트에서 처리됩니다.
protocol="HTTP",
target_type="ip", # ip를 기준으로 ec2 인스턴스를 target group에 등록합니다.
vpc_id=shared_vpc.id,
)
listener = aws.lb.Listener(
resource_name="listener",
load_balancer_arn=load_balancer.arn,
port=80, # [클라이언트 -> 로드밸런서] 요청이 80번 포트에서 처리됩니다.
protocol="HTTP",
default_actions=[{"type": "forward", "target_group_arn": target_group.arn}],
)
tg_ec2_attachment_1 = aws.lb.TargetGroupAttachment(
resource_name=f"tg-ec2-attachment-1-{NAME}",
target_group_arn=target_group.arn,
target_id=app1.private_ip,
port=80,
)
tg_ec2_attachment_2 = aws.lb.TargetGroupAttachment(
resource_name=f"tg-ec2-attachment-2-{NAME}",
target_group_arn=target_group.arn,
target_id=app2.private_ip,
port=80,
)
# RDS
## Security Group
db_security_group = aws.ec2.SecurityGroup(
resource_name=f"db-security-group-{NAME}",
vpc_id=shared_vpc.id
)
db_security_group_rule = aws.ec2.SecurityGroupRule(
resource_name=f"db-security-group-rule-{NAME}",
type="ingress",
security_group_id=db_security_group.id,
source_security_group_id=ec2_security_group,
protocol="tcp",
from_port=5432,
to_port=5432
)
## subnet group
db_subnet_group = aws.rds.SubnetGroup(
resource_name=f"db-subnet-group-{NAME}",
subnet_ids=[subnet_rds1.id, subnet_rds2.id] # AZ-a, AZ-c 커버
)
## instance
rds_database = aws.rds.Instance(
resource_name=f"rds-database-{NAME}",
db_subnet_group_name=db_subnet_group,
allocated_storage=20, # rds 용량 20GB
storage_type="gp2",
instance_class="db.t2.micro",
engine="postgres",
engine_version="10.6",
port=5432,
name=f"pulumiAwsDatabase", # DB name
identifier=f"rds-{NAME}", # RDS instance name
username="johnDoe", # master DB user
password="heIsDead", # user password
skip_final_snapshot=True,
# final_snapshot_identifier="pulumiAwsDatabaseSnapshot", # RDS 인스턴스 삭제 시 생성되는 last snapshot name
vpc_security_group_ids=[db_security_group.id]
)
pulumi.export("url", load_balancer.dns_name)