-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-ec2-autoscaling.yml
More file actions
138 lines (131 loc) · 4.02 KB
/
task-ec2-autoscaling.yml
File metadata and controls
138 lines (131 loc) · 4.02 KB
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
Parameters:
VPC:
Description: Gets the Current VPC
Type: AWS::EC2::VPC::Id
AmiId:
Description: AMI Image ID for Instance (default is latest AmaLinux2)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AppSubnets:
Description: "List of Subnets for use in AutoScaling Group"
Type: "List<AWS::EC2::Subnet::Id>"
AppSecurityGroupList:
Description: "List of Security Groups for use in AutoScaling Group. Pick at least 3 existing subnets"
Type: List<AWS::EC2::SecurityGroup::Id>
KeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: "An Existing EC2 Keypair to access created instances via SSH"
IPAddress:
Type: String
Description: "This must be a valid IP address. Default is 0.0.0.0/0. It is recommended to use a /32 address"
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: "must be a valid IP address of the form x.x.x.x/x"
ScalingNotificationTopic:
Type: String
Description: Name of SNS topic to notify on EC2 Autoscaling changes
Default: MySampleTopic
SNSSubscriptionEndpoint:
Description: The endpoint that recieves notifications from the Amazon SNS Topic. Endpoint value can be a URL or ARN
Type: String
Default: john.adedigba@gmail.com
SNSSubscriptionProtocol:
Type: String
Description: The subscription's protocol
AllowedValues:
- http
- https
- email
- email-json
- sms
- sqs
- application
- lambda
Default: emails
Resources:
AppASGLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
KeyName: !Ref KeyName
InstanceType: "t2.micro"
ImageId: !Ref AmiId
SecurityGroupIds: !Ref AppSecurityGroupList
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl restart httpd
SG:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn:
- AppSNSTopic
Properties:
AvailabilityZones:
Fn::GetAZs: ''
LaunchTemplate:
LaunchTemplateId: !Ref AppASGLaunchTemplate
Version: "1"
MaxSize: "3"
MinSize: "1"
HealthCheckGracePeriod: 60
TargetGroupARNs:
- !Ref AppALBTG
HealthCheckType: "ELB"
DesiredCapacity: "1"
NotificationConfigurations:
- TopicARN: !Ref AppSNSTopic
NotificationTypes:
- autoscaling:EC2_INSTANCE_LAUNCH
- autoscaling:EC2_INSTANCE_LAUNCH_ERROR
- autoscaling:EC2_INSTANCE_TERMINATE
- autoscaling:EC2_INSTANCE_TERMINATE_ERROR
- autoscaling:TEST_NOTIFICATION
AppSNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref SNSSubscriptionEndpoint
Protocol: !Ref SNSSubscriptionProtocol
TopicName: !Ref ScalingNotificationTopic
AppALB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: 'ipv4'
Scheme: 'internet-facing'
SecurityGroups: !Ref AppSecurityGroupList
Subnets:
- !Select [0, !Ref AppSubnets]
- !Select [1, !Ref AppSubnets]
- !Select [2, !Ref AppSubnets]
Type: 'application'
AppALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref AppALBTG
LoadBalancerArn: !Ref AppALB
Port: 80
Protocol: HTTP
AppALBTG:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
HealthCheckPath: /index.php
HealthCheckTimeoutSeconds: 5
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 5
VpcId: !Ref VPC
Outputs:
WebsiteURL:
Description: Application URL
Value:
Fn::Join:
- ''
- - http://
- Fn::GetAtt:
- AppALB
- DNSName