Skip to content

Commit 15e6972

Browse files
committed
EC2 Cloud Formation config.
1 parent ce1e223 commit 15e6972

1 file changed

Lines changed: 158 additions & 17 deletions

File tree

cloudformation.json

Lines changed: 158 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,172 @@
11
{
22
"AWSTemplateFormatVersion": "2010-09-09",
3-
"Description": "Elastic Beanstalk Multi-container Docker Environment",
3+
"Description": "Minimal 2-AZ EC2 cluster with ASG, pulling from ECR",
4+
"Parameters": {
5+
"InstanceType": {
6+
"Type": "String",
7+
"Default": "t3.micro"
8+
},
9+
"DesiredCapacity": {
10+
"Type": "Number",
11+
"Default": 2
12+
},
13+
"ECSAMI": {
14+
"Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>",
15+
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
16+
}
17+
},
418
"Resources": {
5-
"MyApplication": {
6-
"Type": "AWS::ElasticBeanstalk::Application",
19+
"VPC": {
20+
"Type": "AWS::EC2::VPC",
721
"Properties": {
8-
"ApplicationName": "MyDockerApp",
9-
"Description": "Elastic Beanstalk application for multi-container Docker"
22+
"CidrBlock": "10.0.0.0/16",
23+
"EnableDnsSupport": true,
24+
"EnableDnsHostnames": true
1025
}
1126
},
12-
"MyEnvironment": {
13-
"Type": "AWS::ElasticBeanstalk::Environment",
27+
"InternetGateway": {
28+
"Type": "AWS::EC2::InternetGateway"
29+
},
30+
"AttachGateway": {
31+
"Type": "AWS::EC2::VPCGatewayAttachment",
1432
"Properties": {
15-
"EnvironmentName": "MyDockerEnv",
16-
"ApplicationName": {
17-
"Ref": "MyApplication"
18-
},
19-
"SolutionStackName": "64bit Amazon Linux 2 v4.2.3 running Docker",
20-
"VersionLabel": "InitialVersion",
21-
"OptionSettings": [
33+
"VpcId": { "Ref": "VPC" },
34+
"InternetGatewayId": { "Ref": "InternetGateway" }
35+
}
36+
},
37+
"PublicSubnet1": {
38+
"Type": "AWS::EC2::Subnet",
39+
"Properties": {
40+
"VpcId": { "Ref": "VPC" },
41+
"CidrBlock": "10.0.1.0/24",
42+
"AvailabilityZone": { "Fn::Select": [ 0, { "Fn::GetAZs": "" } ] },
43+
"MapPublicIpOnLaunch": true
44+
}
45+
},
46+
"PublicSubnet2": {
47+
"Type": "AWS::EC2::Subnet",
48+
"Properties": {
49+
"VpcId": { "Ref": "VPC" },
50+
"CidrBlock": "10.0.2.0/24",
51+
"AvailabilityZone": { "Fn::Select": [ 1, { "Fn::GetAZs": "" } ] },
52+
"MapPublicIpOnLaunch": true
53+
}
54+
},
55+
"PublicRouteTable": {
56+
"Type": "AWS::EC2::RouteTable",
57+
"Properties": {
58+
"VpcId": { "Ref": "VPC" }
59+
}
60+
},
61+
"PublicRoute": {
62+
"Type": "AWS::EC2::Route",
63+
"DependsOn": "AttachGateway",
64+
"Properties": {
65+
"RouteTableId": { "Ref": "PublicRouteTable" },
66+
"DestinationCidrBlock": "0.0.0.0/0",
67+
"GatewayId": { "Ref": "InternetGateway" }
68+
}
69+
},
70+
"SubnetRouteTableAssociation1": {
71+
"Type": "AWS::EC2::SubnetRouteTableAssociation",
72+
"Properties": {
73+
"SubnetId": { "Ref": "PublicSubnet1" },
74+
"RouteTableId": { "Ref": "PublicRouteTable" }
75+
}
76+
},
77+
"SubnetRouteTableAssociation2": {
78+
"Type": "AWS::EC2::SubnetRouteTableAssociation",
79+
"Properties": {
80+
"SubnetId": { "Ref": "PublicSubnet2" },
81+
"RouteTableId": { "Ref": "PublicRouteTable" }
82+
}
83+
},
84+
"InstanceSecurityGroup": {
85+
"Type": "AWS::EC2::SecurityGroup",
86+
"Properties": {
87+
"VpcId": { "Ref": "VPC" },
88+
"GroupDescription": "Allow inbound traffic",
89+
"SecurityGroupIngress": [
90+
{
91+
"IpProtocol": "tcp",
92+
"FromPort": 80,
93+
"ToPort": 80,
94+
"CidrIp": "0.0.0.0/0"
95+
}
96+
],
97+
"SecurityGroupEgress": [
2298
{
23-
"Namespace": "aws:autoscaling:launchconfiguration",
24-
"OptionName": "InstanceType",
25-
"Value": "t3.micro"
99+
"IpProtocol": "-1",
100+
"CidrIp": "0.0.0.0/0"
26101
}
27102
]
28103
}
104+
},
105+
"InstanceRole": {
106+
"Type": "AWS::IAM::Role",
107+
"Properties": {
108+
"AssumeRolePolicyDocument": {
109+
"Version": "2012-10-17",
110+
"Statement": [
111+
{
112+
"Effect": "Allow",
113+
"Principal": { "Service": [ "ec2.amazonaws.com" ] },
114+
"Action": [ "sts:AssumeRole" ]
115+
}
116+
]
117+
},
118+
"ManagedPolicyArns": [
119+
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
120+
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
121+
]
122+
}
123+
},
124+
"InstanceProfile": {
125+
"Type": "AWS::IAM::InstanceProfile",
126+
"Properties": {
127+
"Roles": [ { "Ref": "InstanceRole" } ]
128+
}
129+
},
130+
"LaunchTemplate": {
131+
"Type": "AWS::EC2::LaunchTemplate",
132+
"Properties": {
133+
"LaunchTemplateData": {
134+
"ImageId": { "Ref": "ECSAMI" },
135+
"InstanceType": { "Ref": "InstanceType" },
136+
"IamInstanceProfile": {
137+
"Name": { "Ref": "InstanceProfile" }
138+
},
139+
"SecurityGroupIds": [ { "Ref": "InstanceSecurityGroup" } ],
140+
"UserData": {
141+
"Fn::Base64": {
142+
"Fn::Join": [
143+
"",
144+
[
145+
"#! /bin/bash\n",
146+
"REGISTRY_URL=332187735950.dkr.ecr.eu-west-2.amazonaws.com\n",
147+
"IMAGE=asyncdb/0.0.2\n",
148+
"aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin $REGISTRY_URL\n",
149+
"docker pull $REGISTRY/$IMAGE\n",
150+
"docker run -d -p 80:80 $REGISTRY/$IMAGE\n"
151+
]
152+
]
153+
}
154+
}
155+
}
156+
}
157+
},
158+
"AutoScalingGroup": {
159+
"Type": "AWS::AutoScaling::AutoScalingGroup",
160+
"Properties": {
161+
"VPCZoneIdentifier": [ { "Ref": "PublicSubnet1" }, { "Ref": "PublicSubnet2" } ],
162+
"LaunchTemplate": {
163+
"LaunchTemplateId": { "Ref": "LaunchTemplate" },
164+
"Version": { "Fn::GetAtt": [ "LaunchTemplate", "LatestVersionNumber" ] }
165+
},
166+
"DesiredCapacity": { "Ref": "DesiredCapacity" },
167+
"MinSize": "1",
168+
"MaxSize": "4"
169+
}
29170
}
30171
}
31172
}

0 commit comments

Comments
 (0)