Welcome back to the series of AWS Cloudformation For Beginners 👨🏻💻. In this blog we create launch configuration along with security group and autoscaling group.
If you are a beginner and want to start your journey towards infra-as-code developer as part of your devops role buckle up 🚴♂️ and lets get started and understand core cloudformation concepts by implementing it…🎬
🎨 Diagrammatic Representation 🎨
🌟Deploy Auto Scaling Group With Launch Config🌟
In this we are going to create launch configuration for our autoscaling with properties Image ID and Instance Type as mandatory properties.
🔳 Parameters:-
✦ PublicSubnets :- Using CommaDelimitedList parameters we can create a list of values of type string and pass it to arguments as a list.
Parameters:
PublicSubnets:
Type: CommaDelimitedList
Description: The list of SubnetIds in your Virtual Private Cloud (VPC)
Default: <subnet id 1>, <subnet id 2> #You can add multiple subnet ids here
🔳 Resources
✦ ASGSecurityGroup:- Creating Security group and enabling ingress with http and ssh port.
➖ GroupName:- This property is used to mention security group name.
➖ GroupDescription:- This property is used to mention security group description and its mandatory property for this resource.
➖ SecurityGroupIngress:- This property is used to add ingress rules for [udp/tcp] ports enabled secured access to your resources.
➖ Tags:- One of the most important property used in all resources. Alaways make sure to attach tags for all your resources.
✦ AsgConfig:- This resource is used to create pre-configured instance configuration which is later used by autoscaling group to deploy instances.
➖ ImageId:- This property is used to mention EC2 image ID based on which you want to launch your EC2 Instance.
➖ InstanceType:- This property is used to mention which type of instance you want to launch smal/medium/large based on your requirement.
➖ SecurityGroupIds:- This property is used to add list of security group you want to attach to your EC2 instance for enabling access control based on your security requirements.
➖ User Data: User data is user data/commands that you can specify at the time of launching your instance. These data/command executes after your EC2 instance starts. You don’t need to SSH into your EC2 instance and run those command one by one. Rather all you need is to specify the whole script in the user data section and they get executed once your instance boots up. You can use AWS CloudFormation to automatically install, configure, and start applications on Amazon EC2 instances. Doing so enables you to easily replicate deployments and update existing installations without connecting directly to the instance, which can save you a lot of time and effort.
Resources:
ASGSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: AllowEC2TrafficForASG
GroupDescription: Enable SSH access and HTTP access on the inbound port for launch configuration
VpcId: <vpc id> #Make sure this is same vpc id where your autoscaling group will launch
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: ASGSecurityGroup
AsgConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
InstanceType: t2.micro
SecurityGroups:
- !Ref ASGSecurityGroup
ImageId: "ami-04505e74c0741db8d"
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
sudo su
sudo apt-get update -y
sudo apt-get install -y apache2
sudo ufw allow -y 'Apache'
sudo systemctl start apache2
sudo systemctl enable apache2
echo Hello Viewers from $(hostname -f) > /var/www/html/index.html
✦ AsgGroup:- This resource is used to launch autoscaling group to by using pre-defined launch configuration to achieve high availability.
➖ VPCZoneIdentifier:- Here you will have to define availability zones/subnetIds in which you want your instances to be launched and autoscaled.
➖ MinSize:- This is mandatory parameter and you need to define the min number of instances which should always be running as part of this autoscaling group.
➖ MaxSize:- This is mandatory parameter and you need to define the max number of instances till which autoscaling group can expand based on multiple scenarios.
➖ Tags:- One of the most important property used in all resources. Alaways make sure to attach tags for all your resources.
AsgGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier: !Ref PublicSubnets
LaunchConfigurationName: !Ref AsgConfig
MinSize: '1'
MaxSize: '2'
HealthCheckGracePeriod: 300
MaxInstanceLifetime: 2592000
Tags:
- Key: Name
Value: AsgGroup
🔳 Outputs: Its always a best practice to print output for your resources.
✦ outputASGSecurityGroup:- Id of security group created for autoscaling launch configuration.
✦ outputAsgConfig:- Id for autoscaling launch configuration.
✦ outputAsgGroup:- Id for autoscaling group.
Outputs:
outputASGSecurityGroup:
Description: Id of security group created for autoscaling launch configuration
Value: !Ref ASGSecurityGroup
outputAsgConfig:
Description: Id for autoscaling launch configuration
Value: !Ref AsgConfig
outputAsgGroup:
Description: Id for autoscaling group
Value: !Ref AsgGroup
🔊 To view github code click here
1️⃣ Lets validate our template 👨💻
aws cloudformation validate-template --template-body file://<file path>
2️⃣ After successful template verification lets create stack using our template 👨💻
aws cloudformation create-stack --stack-name asglaunchconfig --template-body file://<file path>
3️⃣ Check if the stack we created via template is completed successfully 👨💻
aws cloudformation list-stack-resources --stack-name asglaunchconfig
4️⃣ Describe stack and its resources to view its properties 👨💻
aws cloudformation describe-stacks --stack-name asglaunchconfig
aws cloudformation describe-stack-resources --stack-name asglaunchconfig
5️⃣ Check events for stack formation 👨💻
aws cloudformation describe-stack-events --stack-name asglaunchconfig
❗️❗️Important AWS Documentation To Be Viewed❗️❗️
⛔️ AWS::AutoScaling::LaunchConfiguration
⛔️ Supported AWS-specific parameter types
⛔️ AWS::AutoScaling::AutoScalingGroup
⛔️ AWS::EC2::SecurityGroup
🥁🥁 Conclusion 🥁🥁
In this blog I have covered 3 resources in which we will create
✦ Security group for Autoscaling.
✦ Launch Configuration for EC2 instances with Userdata.
✦ Autoscaling group deployment with launch configuration created.
I have used AWS CLI command to deploy these template and trust me AWS CLI is the realtime hero and I would suggest you to get acquainted towards it. Going forward I will be releasing further parts to this CloudFormation journey
📢 Stay tuned for my next blog…..
So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks
Author - Dheeraj Choudhary
RELATED ARTICLES
Deploy AWS Configuration Along With Security Group And AutoScaling Group Using CloudFormation
Welcome back to the series of AWS Cloudformation For Beginners 👨🏻💻. In this blog we create launch configuration along with security grou ...
Deploy AWS Target Group, Elastic Load Balancer & ELB Listener Using CloudFormation
Welcome back to the series of AWS Cloudformation For Beginners 👨🏻💻. In this blog I am going to deploy resources which are very important ...