Implementing EC2 Auto Scaling using CI/CD

0

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.

1 Antwort
1

You will want to look at an ASG with code deploy instead...

https://docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-auto-scaling.html

https://ec2spotworkshops.com/running-amazon-ec2-workloads-at-scale/deploy_app_codedeploy.html#:~:text=An%20application%20specification%20file%20(AppSpec,are%20defined%20in%20the%20file.

Your code is deployed to each EC2 that is launched in the ASG ensuring the latest version of your application is always deployed.

profile picture
EXPERTE
beantwortet vor einem Monat
profile picture
EXPERTE
überprüft vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen