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-service-cf-template.py
102 lines (92 loc) · 2.15 KB
/
helloworld-ecs-service-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
"""Generating CloudFormation template."""
from troposphere.ecs import (
TaskDefinition,
ContainerDefinition
)
from troposphere import ecs
from awacs.aws import (
Allow,
Statement,
Principal,
Policy
)
from troposphere.iam import Role
from troposphere import (
Parameter,
Ref,
Template,
Join,
ImportValue,
Select,
Split,
)
from awacs.sts import AssumeRole
t = Template()
t.add_description("Effective DevOps in AWS: ECS service - Helloworld")
t.add_parameter(Parameter(
"Tag",
Type="String",
Default="latest",
Description="Tag to deploy"
))
t.add_resource(TaskDefinition(
"task",
ContainerDefinitions=[
ContainerDefinition(
Image=Join("", [
Ref("AWS::AccountId"),
".dkr.ecr.",
Ref("AWS::Region"),
".amazonaws.com",
"/",
ImportValue("helloworld-repo"),
":",
Ref("Tag")]),
Memory=32,
Cpu=256,
Name="helloworld",
PortMappings=[ecs.PortMapping(
ContainerPort=3000)]
)
],
))
t.add_resource(Role(
"ServiceRole",
AssumeRolePolicyDocument=Policy(
Statement=[
Statement(
Effect=Allow,
Action=[AssumeRole],
Principal=Principal("Service", ["ecs.amazonaws.com"])
)
]
),
Path="/",
ManagedPolicyArns=[
'arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole']
))
t.add_resource(ecs.Service(
"service",
Cluster=ImportValue(
Join(
"-",
[Select(0, Split("-", Ref("AWS::StackName"))),
"cluster-id"]
)
),
DesiredCount=1,
TaskDefinition=Ref("task"),
LoadBalancers=[ecs.LoadBalancer(
ContainerName="helloworld",
ContainerPort=3000,
TargetGroupArn=ImportValue(
Join(
"-",
[Select(0, Split("-", Ref("AWS::StackName"))),
"alb-target-group"]
),
),
)],
Role=Ref("ServiceRole")
))
print(t.to_json())