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.

profile picture
질문됨 2달 전655회 조회
1개 답변
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
전문가
답변함 2달 전
profile picture
전문가
검토됨 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인