Setting Up an Auto Scaling Group with AWS CLI#12
Similar to my last project, we are setting up an Auto Scaling group, but this time though our terminal. An AWS account is needed for this project, along with the AWS CLI downloaded to your terminal. This was performed on a macOS so the download process may slightly differ on other operating systems.
For all other OS environments, follow this link to download AWS CLI correctly to your environment: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
Download AWS CLI to your terminal
curl "[https://awscli.amazonaws.com/AWSCLIV2.pkg](https://awscli.amazonaws.com/AWSCLIV2.pkg)" -o "AWSCLIV2.pkg" sudo installer -pkg AWSCLIV2.pkg -target /
Run the below code to verify that the AWS CLI can be found and run.
which aws aws --version
Taking these above steps will allow you to locate where the AWS CLI is on your computer and which version you have downloaded.
Create a user and give them programmatic and console access
aws iam create-user --user-name <NEW\_USER-NAME>
You will be returned code in JSON format indicating your user name, user ID, ARN (Amazon Resource Name), and the creation date of this user. Note: you will see your AWS account ID in the ARN output information.
I already have an Admin group created in my AWS account, therefore giving programmatic and console access to my new user is simply writing code and adding them to my Admin group.
aws iam add-user-to-group --user-name <USER-NAME> --group-name <GROUP-NAME>
Using the below code will verify that your new user was successfully placed in your group.
aws iam get-group --group-name <GROUP-NAME>
Download the Access Key and Secret Access Key in a safe place
aws iam create-access-key --user-name <USER-NAME>
Your information is returned in JSON format. The information consists of your user name, access key ID, status, secret access key, and the creation date. Saving your access and secret access key is highly important.
The above code opens a file for you to input the new user’s access key and secret access key. From the returned information in the JSON format above, copy and paste the two keys into this vim file. You will see that the default user is also there. Knowing how to do this is important when handling multiple credentials in further projects you may be given.
Create an EC2 Instance (t3.nano, Amazon 2 AMI)
In this step, the default settings are used for the security group and subnet ID that may have previously been set up in our AWS account. We are asked to use Amazon 2 AMI in this project, so it is pasted below in the code, along with our instance type.
aws ec2 run-instances --image-id ami-0742b4e673072066f --count 1 --instance-type t3.nano --key-name <EXISTING\_KEY\_NAME> --security-group-ids sg--<YOUR\_SG\_ID> subnet-id subnet-<DEFAULT\_SUBNET\_ID>
List the Instance to ensure creation
aws ec2 describe-instances --filters "Name=instance-type,Values=t3.nano" --query "Reservations.Instances.InstanceId"
The output above in our terminal proves that our Instance has been successfully created, and we can go into the AWS Console to double check that success.
Create Auto Scaling group with a minimum of 2 and maximum of 5
aws autoscaling create-auto-scaling-group --auto-scaling-group-name <ASG\_NAME> --launch-template LaunchTemplateId=<LT\_ID> --min-size 2 --max-size 5 --vpc-zone-identifier "<SUBNET>, <SUBNET>, <SUBNET>"
The code above will create instances that you can check in your console, seen below. In sticking with using the CLI, the code below will provide you with your Auto Scaling group with the given name.
aws autoscaling describe-auto-scaling-groups
Double-checking the Auto Scaling group on the Console.
Clean up environment
Now that we have successfully completed our task, it is time to undo everything by following these two steps:
- Delete launch template
aws ec2 delete-launch-template --launch-template-id <lt-0abcd290751193123>
2. Delete group while terminating any unwanted EC2 Instances
aws autoscaling terminate-instance-in-auto-scaling-group --instance-id <i-056a54b876bb83220> --no-should-decrement-desired-capacity
Again, double checking that your environment has been cleaned up never hurts, and may help you avoid unnecessary costs garnered while attempting this project.