This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld-ecs-alb-cf-template.py
103 lines (92 loc) · 2.2 KB
/
helloworld-ecs-alb-cf-template.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
"""Generating CloudFormation template."""
from troposphere import elasticloadbalancingv2 as elb
from troposphere import (
Export,
GetAtt,
ImportValue,
Join,
Output,
Ref,
Select,
Split,
Sub,
Template,
ec2
)
t = Template()
t.add_description("Effective DevOps in AWS: ALB for the ECS Cluster")
t.add_resource(ec2.SecurityGroup(
"LoadBalancerSecurityGroup",
GroupDescription="Web load balancer security group.",
VpcId=ImportValue(
Join(
"-",
[Select(0, Split("-", Ref("AWS::StackName"))),
"cluster-vpc-id"]
)
),
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="3000",
ToPort="3000",
CidrIp="0.0.0.0/0",
),
],
))
t.add_resource(elb.LoadBalancer(
"LoadBalancer",
Scheme="internet-facing",
Subnets=Split(
',',
ImportValue(
Join("-",
[Select(0, Split("-", Ref("AWS::StackName"))),
"cluster-public-subnets"]
)
)
),
SecurityGroups=[Ref("LoadBalancerSecurityGroup")],
))
t.add_resource(elb.TargetGroup(
"TargetGroup",
DependsOn='LoadBalancer',
HealthCheckIntervalSeconds="20",
HealthCheckProtocol="HTTP",
HealthCheckTimeoutSeconds="15",
HealthyThresholdCount="5",
Matcher=elb.Matcher(
HttpCode="200"),
Port=3000,
Protocol="HTTP",
UnhealthyThresholdCount="3",
VpcId=ImportValue(
Join(
"-",
[Select(0, Split("-", Ref("AWS::StackName"))),
"cluster-vpc-id"]
)
),
))
t.add_resource(elb.Listener(
"Listener",
Port="3000",
Protocol="HTTP",
LoadBalancerArn=Ref("LoadBalancer"),
DefaultActions=[elb.Action(
Type="forward",
TargetGroupArn=Ref("TargetGroup")
)]
))
t.add_output(Output(
"TargetGroup",
Description="TargetGroup",
Value=Ref("TargetGroup"),
Export=Export(Sub("${AWS::StackName}-target-group")),
))
t.add_output(Output(
"URL",
Description="Helloworld URL",
Value=Join("", ["http://", GetAtt("LoadBalancer", "DNSName"), ":3000"])
))
print(t.to_json())