-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudFront_ALB_EC2.yml
231 lines (214 loc) · 6.07 KB
/
CloudFront_ALB_EC2.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
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
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFront ALB EC2
Parameters:
# ------------------------------------------------------------#
# Parameters
# ------------------------------------------------------------#
VolumeSize:
Default: 8
Type: Number
Ec21InstanceType:
Default: t2.micro
Type: String
Vpcid:
Type: AWS::EC2::VPC::Id
Description: Enter VPC ID
PublicSubnet1:
Type: AWS::EC2::Subnet::Id
Description: Enter Subnet ID
PublicSubnet2:
Type: AWS::EC2::Subnet::Id
Description: Enter Subnet ID
Resources:
# ------------------------------------------------------------#
# IAM
# ------------------------------------------------------------#
Ec2SsmRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
RoleName: EC2SsmRole
Ec2IamInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: Ec2InstanceProfile
Roles:
- !Ref Ec2SsmRole
# ------------------------------------------------------------#
# Security Group
# ------------------------------------------------------------#
AlbSg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: for ALB
GroupName: alb-sg
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
FromPort: -1
IpProtocol: -1
ToPort: -1
SecurityGroupIngress:
- SourcePrefixListId: pl-58a04531
FromPort: 80
IpProtocol: tcp
ToPort: 80
Tags:
- Key: Name
Value: alb-sg
VpcId: !Ref Vpcid
Ec2Sg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: for EC2
GroupName: ec2-sg
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
FromPort: -1
IpProtocol: -1
ToPort: -1
SecurityGroupIngress:
- FromPort: 80
IpProtocol: tcp
ToPort: 80
SourceSecurityGroupId: !Ref AlbSg
Tags:
- Key: Name
Value: ec2-sg
VpcId: !Ref Vpcid
# ------------------------------------------------------------#
# EC2
# ------------------------------------------------------------#
Ec2:
Type: AWS::EC2::Instance
Properties:
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
DeleteOnTermination: true
Encrypted: true
Iops: 3000
VolumeSize: !Ref VolumeSize
VolumeType: gp3
IamInstanceProfile: !Ref Ec2IamInstanceProfile
ImageId: ami-0f36dcfcc94112ea1
InstanceType: !Ref Ec21InstanceType
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: 0
GroupSet:
- !Ref Ec2Sg
SubnetId: !Ref PublicSubnet1
Tags:
- Key: Name
Value: ec2
UserData: !Base64 |
#!/bin/bash
yum update -y
yum upgrade -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
touch /var/www/html/index.html
echo "CloudFront -> ALB -> EC2" > /var/www/html/index.html
# ------------------------------------------------------------#
# ALB
# ------------------------------------------------------------#
Alb:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: ipv4
Name: alb
Scheme: internet-facing
SecurityGroups:
- !Ref AlbSg
Subnets:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
Tags:
- Key: Name
Value: alb
Type: application
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckEnabled: true
HealthCheckIntervalSeconds: 30
HealthCheckPath: /
HealthCheckPort: 80
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 5
IpAddressType: ipv4
Matcher:
HttpCode: 200
Name: targetgroup
Port: 80
Protocol: HTTP
ProtocolVersion: HTTP1
Tags:
- Key: Name
Value: targetgroup
Targets:
- Id: !Ref Ec2
Port: 80
TargetType: instance
UnhealthyThresholdCount: 2
VpcId: !Ref Vpcid
AlbListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
LoadBalancerArn: !Ref Alb
Port: 80
Protocol: HTTP
# ------------------------------------------------------------#
# CloudFront
# ------------------------------------------------------------#
CloudFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
TargetOriginId: Alb
ViewerProtocolPolicy: allow-all
Enabled: True
HttpVersion: http1.1
Origins:
- CustomOriginConfig:
HTTPPort: 80
OriginProtocolPolicy: http-only
DomainName: !GetAtt Alb.DNSName
Id: Alb
PriceClass: PriceClass_200
Outputs:
# ------------------------------------------------------------#
# Outputs
# ------------------------------------------------------------#
CloudFrontDomain:
Value: !GetAtt CloudFront.DomainName
Export:
Name: CloudFrontDomain
ALBDomain:
Value: !GetAtt Alb.DNSName
Export:
Name: AlbDNSName