{"id":13626227,"url":"https://github.com/kasukur/auto-scale-ec2-using-sqs","last_synced_at":"2025-04-16T14:32:52.575Z","repository":{"id":74826906,"uuid":"584059097","full_name":"kasukur/auto-scale-ec2-using-sqs","owner":"kasukur","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-02T04:21:39.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-02T22:23:04.521Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kasukur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-01-01T06:18:44.000Z","updated_at":"2023-08-03T22:31:55.000Z","dependencies_parsed_at":"2024-01-14T07:13:17.073Z","dependency_job_id":"93075bdd-2fe6-4cff-bc08-4058fb1c7078","html_url":"https://github.com/kasukur/auto-scale-ec2-using-sqs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasukur%2Fauto-scale-ec2-using-sqs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasukur%2Fauto-scale-ec2-using-sqs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasukur%2Fauto-scale-ec2-using-sqs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasukur%2Fauto-scale-ec2-using-sqs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kasukur","download_url":"https://codeload.github.com/kasukur/auto-scale-ec2-using-sqs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223716478,"owners_count":17191053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:02:13.334Z","updated_at":"2024-11-08T16:30:17.107Z","avatar_url":"https://github.com/kasukur.png","language":"Shell","funding_links":[],"categories":["Shell"],"sub_categories":[],"readme":"# Auto Scale EC2 using SQS\n\n## Table of Contents\n\n1. [Introduction](#Intro)\n2. [Create an EC2 Instance Role](#Role)\n3. [Create a Key pair](#KP)\n4. [Create a Security Group](#SG)\n5. [Create an S3 Bucket](#S3)\n6. [Create an SQS queue](#sqs)\n7. [Create a Cloud9 instance](#cloud9)\n8. [Create CloudWatch Alarms](#cw)\n7. [Create a Launch Template](#template)\n8. [Create an Auto Scaling Group](#ASG)\n9. [Verification and Monitoring](#verify)\n10. [Clean Up](#cleanup)\n11. [Summary](#summary)\n12. [Referrals](#referrals)\n\n\u003ca name=\"Intro\"\u003e\u003c/a\u003e\n\n### Introduction\n\nIn this blog we are going to set up auto scaling of EC2 instances using the SQS ApproximateNumberOfMessagesVisible metric.\n\nAWS Auto Scaling monitors your applications and automatically adjusts capacity to maintain steady, predictable performance at the lowest possible cost. Using AWS Auto Scaling, it’s easy to set up application scaling for multiple resources across multiple services in minutes.\n\n#### Benefits\n- Set up Scaling quickly.\n- Make SMART scaling decisions.\n- Automatically maintain performance.\n- Pay only for what you need.\n\n---\n\n### Demo\n\nLet's get started with the demo.\n\n\u003ca name=\"Role\"\u003e\u003c/a\u003e\n\n#### Step 1. Create an EC2 Instance Role\n\n1. Navigate to IAM \u003e Roles \u003e Click on **Create role**.\n2. Select **EC2** under Common use case and Click **Next**.\n3. Select **AmazonS3ReadOnlyAccess** and **AmazonSQSFullAccess** and Click **Next**.\n4. Enter Role name as **EC2InstanceRoleForSQS** and Click **Create role**.\n\n\u003e Note: you may create a custom policy for SQS with the required permissions instead of using the managed policy **AmazonSQSFullAccess** to provide the least amount of privileges.\n\n_AmazonSQSFullAccess_\n\n```json\n{\n\"Version\": \"2012-10-17\",\n\"Statement\": [\n    {\n        \"Action\": [\n            \"sqs:*\"\n        ],\n        \"Effect\": \"Allow\",\n        \"Resource\": \"*\"\n    }\n]\n}\n```\n\n_AmazonS3ReadOnlyAccess_\n\n```json\n{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"s3:Get*\",\n                \"s3:List*\",\n                \"s3-object-lambda:Get*\",\n                \"s3-object-lambda:List*\"\n            ],\n            \"Resource\": \"*\"\n        }\n    ]\n}\n```\n\n_Trusted entities_\n\n```json\n{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"Service\": \"ec2.amazonaws.com\"\n            },\n            \"Action\": \"sts:AssumeRole\"\n        }\n    ]\n}\n```\n\n---\n\n\u003ca name=\"KP\"\u003e\u003c/a\u003e\n\n#### Step 2. Create a key pair\n\n1. Navigate to EC2 \u003e Key Pairs (under Network \u0026 Security).\n2. Click **Create key pair**.\n3. Enter Name as `auto-scale`.\n4. Navigate to the folder where the key pair is downloaded and run.\n   \n   ```bash\n   chmod 400 \u003ckey_pair_name\u003e.pem\n   ```\n\n![Auto_Scale_Keypair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj672rfyzguxznfuwi2t.png)\n\u003e Auto_Scale_Keypair\n\n---\n\n\u003ca name=\"SG\"\u003e\u003c/a\u003e\n\n#### Step 3. Create a Security Group\n\n1. We are going to create a Security Group for SSH.\n2. Navigate to EC2 \u003e Security Groups \u003e Create a new security group for your ALB, and set the following values:\n   * Name: `MyIPSSH-SG`.\n   * Add an Inbound rule to allow `SSH (TCP 22)` traffic from `My IP`.\n3. Click **Create security group**.\n\n![MyIPSSH-SG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/br62zaedgkg28yx8u12g.png)\n\u003e MyIPSSH-SG\n\n---\n\n\u003ca name=\"S3\"\u003e\u003c/a\u003e\n\n#### Step 4. Create an S3 Bucket\n1. Navigate to EC2 \u003e Click **Create Bucket**\n2. Enter the Bucket name as `autoscalescripts`, add some random numbers to make the bucket name unique.\n3. Leave the rest of the settings as default and click **Create Bucket**\n4. Download `sendMessages.sh` and `receiveMessages.sh` from the [github](https://github.com/kasukur/auto-scale-ec2-using-sqs) and upload them to the S3 bucket.\n\n---\n\n\u003ca name=\"sqs\"\u003e\u003c/a\u003e\n\n### Step 5. Create an SQS queue\n\n1. Navigate to **Simple Queue Service** and click **Create queue**\n2. Enter **Name** as `MyMessages`, leave the rest as defaults and click **Create queue**\n\n\n![SQS_1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2840jonlzd6rcvmtlhjr.png)\n\u003e SQS_1\n\n![SQS_2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0hc7eqa4g80wskip1i8.png)\n\u003e SQS_2\n\n![SQS_3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsrlwh25smnutkqh6nw4.png)\n\u003e SQS_3\n\n![SQS_4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cxuzg9jeq12ab6vi42x.png)\n\u003e SQS_4\n\n---\n\n\u003ca name=\"cloud9\"\u003e\u003c/a\u003e\n\n### Step 6. Create a Cloud9 instance\n\n1. Navigate to **Cloud9** \u003e **Create Environment**.\n2. Enter **Name** as `awscli` and click **Create**.\n3. Click **Open** under **AWS Cloud9** \u003e **Environments**.\n4. When Cloud9 is ready, Click File \u003e Upload Local Files... and then upload `sendMessages.sh`.\n5. Let's check the AWS CLI version.\n\n    ```bash\n    Sri:~/environment $ aws --version\n    aws-cli/1.19.112 Python/2.7.18 Linux/4.14.301-224.520.amzn2.x86_64 botocore/1.20.112\n    ```\n\n6. The script uses `jq`, so we need to install `jq`.\n7. Open a Terminal and execute `sudo yum install jq -y`.\n8. Execute `chmod +x sendMessages.sh` to provide the execute permissions.\n9. Now run the script to send **2000** messages to `MyMessages` queue.\n\n\u003e Note: Alternatively you could use AWS CLI on your personal computer to send the messages.\n\n---\n\n\u003ca name=\"cw\"\u003e\u003c/a\u003e\n\n### Step 7. Create CloudWatch Alarms\n\nWe are going to create a ScaleOut Alarm to launch new instances when the **ApproximateNumberOfMessagesVisible** is greater than **500**.\n\n1. Navigate to CloudWatch \u003e Alarms \u003e Click **Create alarm**.\n2. Click **Select Metric**, search for **SQS**, select **SQS \u003e Queue Metrics**.\n3. Select **MyMessages \u003e ApproximateNumberOfMessagesVisible** and Click **Select Metric**.\n4. Change **Statistic** to **Sum**, **Period** to **1 minute**.\n    - Threshold type: **Static**.\n    - Whenever ApproximateNumberOfMessagesVisible is...: **Greater**.\n    - Define the threshold value: **500**.\n5. Click **Next**.\n6. Click **Remove** under **Notification** and Click **Next**.\n7. Enter Alarm name as **ScaleOut** and Click **Next**.\n8. Click **Create Alarm**.\n\nSimilar to ScaleOut, we also need to create a ScaleIn Alarm to launch new instances when the **ApproximateNumberOfMessagesVisible** is lesser than **300**.\n\n1. Select **ScaleOut** from CloudWatch \u003e Alarms, Click on **Actions** and **Copy**.\n2. Change the following:\n    - Whenever ApproximateNumberOfMessagesVisible is...: **Lower**.\n    - Define the threshold value: **300**.\n4. Click **Next**.\n5. Click **Remove** under **Notification** and Click **Next**.\n6. Enter Alarm name as **ScaleIn** and Click **Next**.\n7. Click **Create Alarm**.\n\n👉 It is important to not have the same value for scale-in and scale-out thresholds. We should leave a gap between them to prevent oscillation.\nFor example: let's say we have 3 instances, and the CPU goes to 60%, which triggers the +1 step scaling policy. If the load stays constant, it will now be distributed to all 4 instances and the average CPU will drop to around 45% and the scale-in alarm will go off. This will then keep happening in a loop until the load goes up or down sufficiently for one of the alarms to stay in the alarm state and the ASG to reache the min or max.\n\n\n![ScaleOut_CreateAlarm1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pt9c5atzk7rk9yg9bbo.png)\n\u003e ScaleOut_CreateAlarm1\n\n![ScaleOut_CreateAlarm2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5u55wcnn7n4rl9uxdig.png)\n\u003e ScaleOut_CreateAlarm2\n\n![ScaleOut_CreateAlarm3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es5938dpg564pac6jefl.png)\n\u003e ScaleOut_CreateAlarm3\n\n![ScaleOut_CreateAlarm4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inxbaifgkcyyy0d6ara4.png)\n\u003e ScaleOut_CreateAlarm4\n\n![ScaleOut_CreateAlarm5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k98eiuqaeibjpax72gmq.png)\n\u003e ScaleOut_CreateAlarm5\n\n![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcacp6l6siopwcwgbgds.png)\n\u003e ScaleOut_CreateAlarm6\n\n![ScaleOut_CreateAlarm7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwue82oizjtbp6t6hx1x.png)\n\u003e ScaleOut_CreateAlarm7\n\n![ScaleOut_CreateAlarm7.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0iozatxchop2oyfbfbk.png)\n\u003e ScaleOut_CreateAlarm7.1\n\n\n![ScaleIn_CreateAlarm1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46ribyq66ddw5msbj1a3.png)\n\u003e ScaleIn_CreateAlarm1\n\n![ScaleIn_CreateAlarm2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fon9gl7kgyjmi05weo9k.png)\n\u003e ScaleIn_CreateAlarm2\n\n![ScaleIn_CreateAlarm3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15s0i1jkkbouohwxogt5.png)\n\u003e ScaleIn_CreateAlarm3\n\n![ScaleIn_CreateAlarm4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b93zwazzvilpgs4twq8s.png)\n\u003e ScaleIn_CreateAlarm4\n\n![ScaleIn_CreateAlarm4.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrrxkco7eygw9d5am5i6.png)\n\u003e ScaleIn_CreateAlarm4.1\n\n---\n\n\u003ca name=\"template\"\u003e\u003c/a\u003e\n\n### Step 8. Create a Launch Template\n\nWe can use a Launch Template or Launch Configurations. Launch Template is preferred over Launch Configurations as we can have different versions of the template. Also we can't modify a Launch Configuration after we have created it.\n\nCreate a Launch Template that will be used by the Auto Scaling group. The Launch Template defines what the instances are and how they are created.\n\n1. Navigate to EC2 \u003e Instances \u003e Launch Templates.\n2. Create a new template, and name it `AutoScale-SQS`.\n3. Search for `AMI`, and pick the `Amazon Linux`.\n4. Set the instance type as `t2.micro`.\n5. Select `key pair` you created earlier.\n6. Select the `MyIPSSH-SG` security group you created earlier.\n7. Expand Advanced Details, and select `EC2InstanceRoleForSQS` Role under **IAM instance profile**.\n8. Paste the following script under **User data**.\n    * Note: These are commands to install jq, AWS CLI, copy scripts from the S3 bucket and to execute receiveMessages.sh.\n9. Click Create Launch Template.\n10. Click **View Launch templates**.\n\n**User data**\n\n\u003e Note: Please update the bucket name in the script.\n\n```bash\n#!/bin/bash\n# install jq\nsudo yum install jq -y\n# Update aws cli version\ncd /home/ec2-user\ncurl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\nunzip awscliv2.zip\nsudo ./aws/install\nsudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update\nwhich aws\nls -l /usr/local/bin/aws\naws --version\n# copy sendMessages.sh and receiveMessages.sh from S3\naws s3 cp s3://sqssri/ . --recursive --exclude \"*\" --include \"*.sh\"\nsudo chmod +x /home/ec2-user/*.sh\nnohup ./receiveMessages.sh \u0026\n```\n\n👉 As Launch Templates with User data are slow, it is recommended to create an AMI with the required software to improve the speed of instance initialisation.\n\n![LaunchTemplate1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xbrn9ucrk4ppmti5prl.png)\n\u003e LaunchTemplate1\n\n![LaunchTemplate1.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjgeub5zdtzcro7p2g8v.png)\n\u003e LaunchTemplate1.1\n\n![LaunchTemplate1.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye8vy4fvzxxjk6ujkhx7.png)\n\u003e LaunchTemplate1.2\n\n![LaunchTemplate1.3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq1oz6djdp5hundl6ceq.png)\n\u003e LaunchTemplate1.3\n\n![LaunchTemplate1.4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2k1m4xq1xa827tyi67r3.png)\n\u003e LaunchTemplate1.4\n\n---\n\n\u003ca name=\"ASG\"\u003e\u003c/a\u003e\n\n### Step 9. Create an Auto Scaling Group\n\n1. Navigate to EC2 \u003e Auto Scaling \u003e Auto Scaling Groups\n2. Click **Create Auto Scaling group**.\n3. Call the group `ASG-SQS`.\n4. Select Launch Template, and choose the template named `AutoScale-SQS`.\n5. We are using `default VPC`, which will be selected, so select `us-east-1a` as subnet.\n7. Click Next.\n8. Leave the default for Health checks, which is `EC2`.\n9. Leave the default for Configure advanced options and Click Next.\n10. For Group Size, enter the following values:\n    * Desired Capacity: `1`\n    * Minimum Capacity: `1`\n    * Maximum Capacity: `4`\n11. We will not be adding Scaling policies here, so leave the default `None` and Click Next.\n12. Click Next at `Add Notifications`.\n13. Click Next at `Add tags`.\n14. Click Create `Auto Scaling Group`.\n15. Navigate to EC2 \u003e Auto Scaling \u003e click `ASG-SQS` and then click `Automatic scaling`\n16. We are going to add two dynamic scaling policies, one for scale out and another one for scale in. Click **Create dynamic scaling policy** and enter the following values and then Click **Create**:\n    * Policy type: `Simple Scaling`\n    * Scaling policy name: `ScaleOut`\n    * CloudWatch alarm: `ScaleOut`\n    * Take the action: `Add` with `1` capacity units\n    * And then wait `60` seconds before allowing another scaling activity\n17. Click **Create dynamic scaling policy** and enter the following values and then Click **Create**:\n    * Policy type: `Simple Scaling`\n    * Scaling policy name: `ScaleIn`\n    * CloudWatch alarm: `ScaleIn`\n    * Take the action: `Remove` with `1` capacity units\n    * And then wait `120` seconds before allowing another scaling activity\n\n👉 It is best practice to scale up fast and scale down slow. Hence we used 60 seconds to scale out and 120 seconds to scale in.\n\n![ASG1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkzh595bvg4q44v9x4c3.png)\n\u003e ASG1\n\n![ASG2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltpxo3028i289y907b3g.png)\n\u003e ASG2\n\n![ASG3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j75emf9kpsq8ajqt6lup.png)\n\u003e ASG3\n\n![ASG4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/head3g2ex0o8v7wufxhg.png)\n\u003e ASG4\n\n![ASG5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fo8oixbltgppgi7l9j3.png)\n\u003e ASG5\n\n![ASG6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gakx52qryz95vejtd8e6.png)\n\u003e ASG6\n\n![ASG7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2nczeve56mfsg5x3nm8.png)\n\u003e ASG7\n\n![ASG7.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo2i142vkuqlyd9j50el.png)\n\u003e ASG7.1\n\n![ASG7.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yrbf6c9s39z6q1us0uo.png)\n\u003e ASG7.2\n\n![ASG8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94zljs854lutt51eu7vr.png)\n\u003e ASG8\n\n![ASG8.1_ScaleOut](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrmcytl7g9d0veax50x9.png)\n\u003e ASG8.1_ScaleOut\n\n![ASG8.2_ScaleIn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luk13ce7usne6sj96nxx.png)\n\u003e ASG8.2_ScaleIn\n\n![ASG8.3_Dynamic_Scaling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swwi96gnopgfor4qtgsf.png)\n\u003e ASG8.3_Dynamic_Scaling\n\n---\n\n\u003ca name=\"verify\"\u003e\u003c/a\u003e\n\n### Step 10. Verification and Monitoring\n\nWe have populated the queue with 2000 messages and have an auto scaling group launch with a maximum of 4 instances. We will now be verifying it using Cloud Watch Alarms and Auto Scaling group's Activity.\n\n1. Navigate to CloudWatch \u003e Alarms \u003e ScaleOut, the state of ScaleOut Alarm status will be `In alarm` and the ScaleIn Alarm status will be `OK`.\n2. After a period of 10 mins or so, you will notice 4 EC2 instances launched under EC2 \u003e Auto Scaling groups \u003e ASG-SQS \u003e Activity.\n3. Monitor the messages in `MyMessages` queue, the number of messages will reduce as they are processed by the EC2 instances.\n4. Navigate to CloudWatch \u003e Alarms \u003e ScaleIn, the state of ScaleIn Alarm status will be `In alarm` and the ScaleOut Alarm status will be `OK`.\n5. You will notice 3 EC2 instances terminated under EC2 \u003e Auto Scaling groups \u003e ASG-SQS \u003e Activity when the messages are less than **300**.\n\n#### How to verify that the SQS messages are being processed\n\nLog on to the EC2 Instance and then switch to the root user.\n\n```bash\n[ec2-user@ip-172-31-47-21 ~]$ sudo su -\n```\n\nExecute the following command, which will show the log file name.\n\n```bash\nps xf\n```\n\n```bash\n[root@ip-172-31-47-21 ~]# tail -f /var/log/cloud-init-output.log\nSleep for 1 second...\nSleep for 1 second...\nSleep for 1 second...\nSleep for 1 second...\n^C\n```\n\n![Messages_in_the_queue](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdto7k32ruxcj43mroww.png)\n\u003e Messages_in_the_queue\n\n![ScaleOut_Alarm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d81ihvzv12obsxlmwz9.png)\n\u003e ScaleOut_Alarm\n\n![ScaleOut_Activity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egncwgp70ou8qpnr11hh.png)\n\u003e ScaleOut_Activity\n\n![Processing_Messages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qgq4iooufewhoz5t9wr.png)\n\u003e Processing_Messages\n\n![ScaleIn_Alarm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6e3aspvrglespoihu5a.png)\n\u003e ScaleIn_Alarm\n\n![ScaleIn_Activity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4bwqzluwjoisbjyeaio.png)\n\u003e ScaleIn_Activity\n\n![Queue_is_Empty](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxfm2pjbazi7ene2j651.png)\n\u003e Queue_is_Empty\n\n---\n\n👉 Auto Scaling Group Tip: When you do not want to have instances running or for Disaster Recovery purposes or to save costs, you may set the following inputs to **Zero**.\n- Desired Capacity: `0`\n- Minimum Capacity: `0`\n- Maximum Capacity: `0`\n\n![ASG_Tip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5u8aclozbbqtnp7whyg6.png)\n\u003e ASG_Tip\n\n---\n\n\u003ca name=\"cleanup\"\u003e\u003c/a\u003e\n\n### Clean Up\n\n1. Terminate `Cloud9` EC2 instance.\n2. Delete `ASG-SQS` under `Auto Scaling groups`.\n3. Delete `AutoScale-SQS` under `Launch Templates`\n4. Delete `auto-scale` under `Key Pairs`\n5. Delete Security Group `MyIPSSH-SG`.\n6. Delete `EC2InstanceRoleForSQS` Role under IAM.\n7. Delete S3 bucket `autoscalescripts`.\n8. Delete SQS queue `MyMessages`.\n\n---\n\n\u003ca name=\"summary\"\u003e\u003c/a\u003e\n\n### Summary\n\n👉 It is important to not have the same value for scale-in and scale-out thresholds. We should leave a gap between them to prevent oscillation.\n\n👉 The best practice is to scale up fast, and scale down slow.\n\n👉 Launching an EC2 instance might be slow if we have to install software, configure..etc during the scale out. One way to speed up the process is by creating an AMI with all the required software and then use that AMI in the Launch template.\n\nHope you learnt something new from the above demo.\n\nSee you next time 👋\n\n---\n\n\u003ca name=\"referrals\"\u003e\u003c/a\u003e\n\n### Referrals\n\n- [Scaling based on Amazon SQS](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-using-sqs-queue.html)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasukur%2Fauto-scale-ec2-using-sqs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkasukur%2Fauto-scale-ec2-using-sqs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasukur%2Fauto-scale-ec2-using-sqs/lists"}