-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebapp.cloudformation.yml
198 lines (198 loc) · 5.05 KB
/
webapp.cloudformation.yml
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
---
Parameters:
KeyName:
Type: String
Default: nikolas
WebappSubnets:
Type: CommaDelimitedList
Default: subnet-063c705f, subnet-bd6155ca, subnet-fb2c209e
ALBSubnets:
Type: CommaDelimitedList
Default: subnet-063c705f, subnet-bd6155ca, subnet-fb2c209e
MinSize:
Type: Number
Default: 1
MaxSize:
Type: Number
Default: 2
VPC:
Type: String
Default: vpc-ff84de9a
AMIID:
Type: String
Default: ami-4df23734
Resources:
WebappApplication:
Type: "AWS::CodeDeploy::Application"
WebappDeploymentGroup:
Type: "AWS::CodeDeploy::DeploymentGroup"
Properties:
ApplicationName: !Ref WebappApplication
ServiceRoleArn: !GetAtt CodeDeployRole.Arn
DeploymentConfigName: CodeDeployDefault.AllAtOnce
AutoScalingGroups:
- Ref: AutoScalingGroup
ALBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: allow access to ALB from internet
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
WebappSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: allow access to Webapp from ALB
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '3000'
ToPort: '3000'
SourceSecurityGroupId:
Ref: ALBSecurityGroup
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
WebappDeploymentBucket:
Type: "AWS::S3::Bucket"
CodeDeployRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codedeploy.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
-
PolicyName: allow-autoscaling
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
- ec2:*
- autoscaling:*
Resource: "*"
WebappRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
- "codedeploy.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
-
PolicyName: "allow-webapp-deployment-bucket-bucket"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "s3:getObject"
Resource: !Sub arn:aws:s3:::${WebappDeploymentBucket}/*
WebappInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles:
- Ref: WebappRole
WebappLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
AssociatePublicIpAddress: true
ImageId:
Ref: AMIID
InstanceType: t2.medium
KeyName:
Ref: KeyName
SecurityGroups:
- Ref: WebappSecurityGroup
IamInstanceProfile:
Ref: WebappInstanceProfile
# UserData:
# "Fn::Base64":
# !Sub |
# #!/bin/bash -xe
# echo 'do we beed user data?'
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
HealthCheckType: ELB
HealthCheckGracePeriod: 300
MinSize:
Ref: MinSize
MaxSize:
Ref: MaxSize
LaunchConfigurationName:
Ref: WebappLaunchConfig
VPCZoneIdentifier:
Ref: WebappSubnets
TargetGroupARNs:
- Ref: ALBTargetGroup
Tags:
- Key: Name
Value: webapp-example
PropagateAtLaunch: true
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
-
Type: forward
TargetGroupArn:
Ref: ALBTargetGroup
LoadBalancerArn:
Ref: ApplicationLoadBalancer
Port: 80
Protocol: HTTP
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing
Subnets:
Ref: ALBSubnets
SecurityGroups:
- Ref: ALBSecurityGroup
ALBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
UnhealthyThresholdCount: 2
HealthyThresholdCount: 2
HealthCheckPath: /healthcheck
Port: 3000
Protocol: HTTP
VpcId:
Ref: VPC
Outputs:
WebappUrl:
Description: Webapp URL
Value: !GetAtt ApplicationLoadBalancer.DNSName
DeploymentGroup:
Description: Webapp Deployment Group
Value: !Ref WebappDeploymentGroup
DeploymentBucket:
Description: Deployment bucket
Value: !Ref WebappDeploymentBucket
ApplicationName:
Description: CodeDeploy Application name
Value: !Ref WebappApplication