Hi AWS, I have to implement autoscaling for my application. Ideally I am following a very simple approach where I stored my application code inside S3 bucket and then I leveraged the EC2 UserData while creating the launch template which I am going to further use for creating an ASG.
I was trying to accomplish Auto Scaling using CI/CD and the basic workflow code I wrote is:
- name: Create the AMI for the EC2 instance
shell: powershell
run: |
$instance_id = Invoke-RestMethod -Uri http://169.254.169.254/latest/meta-data/instance-id
$ami_name="poc-ami-$(Get-Date -Format 'yyyyMMddHHmmss')"
$ami_id=(aws ec2 create-image --instance-id $instance_id --name "$ami_name" --tag-specifications "ResourceType=image,Tags=[{Key=Name,Value=$ami_name}]" --no-reboot --output text).ImageId
echo "AMI_ID=$ami_id" >> $GITHUB_ENV
- name: Create the JSON file for Launch Template specifications
id: create-json
uses: jsdaniell/create-json@v1.2.2
with:
name: "launch-template.json"
json: |
{
"NetworkInterfaces": [
{
"DeviceIndex": 0,
"AssociatePublicIpAddress": true,
"Groups": ["sg-089b265ba5151931e"],
"DeleteOnTermination": true
}
],
"ImageId": ${{ env.AMI_ID }},
"InstanceType": "t2.micro",
"TagSpecifications": [
{
"ResourceType": "instance",
"Tags": [
{"Key": "environment", "Value": "development"},
{"Key": "Name", "Value": "poc-lt"}
]
},
{
"ResourceType": "volume",
"Tags": [
{"Key": "environment", "Value": "development"},
{"Key": "Name", "Value": "poc-lt"}
]
}
],
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {"VolumeSize": 100}
}
]
}
- name: Create EC2 Launch Template
shell: powershell
run: |
$launch_template=(aws ec2 create-launch-template --launch-template-name TemplateForAutoScaling --version-description AutoScalingVersion1 --launch-template-data file://launch-template.json --region ap-south-1)
But what I figured out is it will create an ASG but it will not autoscale the application.
The other way I tried to achieve Auto Scaling is using Ansible something similar which is mentioned in this GitHub Repo https://github.com/manicminer/ansible-auto-scaling-tutorial. Is there any other way where we create a pre-warmed AMI where the application is installed which further can be used in launch template to launch AWS Auto Scaling Group.
Please recommend.