Skip to content

Instantly share code, notes, and snippets.

@devenes
Created April 30, 2022 01:16
Show Gist options
  • Save devenes/68d6f47b19f6e7167485d13911e1bc2f to your computer and use it in GitHub Desktop.
Save devenes/68d6f47b19f6e7167485d13911e1bc2f to your computer and use it in GitHub Desktop.
AWS CLI Cheat Sheet by Enes Turan

AWS CLI Cheat Sheet by Enes Turan

sudo hostnamectl set-hostname aws-cli
bash
aws --version

Install AWS CLI Version 2

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
/usr/local/bin/aws --version
which aws
aws configure
Access key ID: XXXXXXXXXXXXXXXXXXX
Secret access key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]: us-east-1
Default output format [None]: json

Check AWS CLI Configuration

aws sts get-caller-identity --query Account --output text

Create Security Groups

aws ec2 create-security-group \
 --group-name roman_numbers_sec_grp \
 --description "This Sec Group is to allow ssh and http from anywhere"

Check Security Groups with the following command:

aws ec2 describe-security-groups --group-names roman_numbers_sec_grp

You can check IP Addresses of the Security Group with the following command:

curl https://checkip.amazonaws.com

Create Rules for the Security Groups

aws ec2 authorize-security-group-ingress \
 --group-name roman_numbers_sec_grp \
 --protocol tcp \
 --port 22 \
 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress \
--group-name roman_numbers_sec_grp \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0

Create EC2 Instance

After creating Security Groups, we will create our EC2 instances.

  • Latest AMI id should be used.

Get the latest AMI id with the following command:

aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region us-east-1

Get the latest AMI id with using query:

aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[0].[Value]' --output text

Assign the value to a variable:

LATEST_AMI=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[0].[Value]' --output text)
echo $LATEST_AMI

Create userdata.sh file:

sudo vim userdata.sh

Run EC2 Instance with userdata.sh file:

aws ec2 run-instances --image-id $LATEST_AMI --count 1 --instance-type t2.micro --key-name xxxxxxx --security-groups roman_numbers_sec_grp --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=roman_numbers}]' --user-data file:///Users/ODG/Desktop/git_dir/devenes-cw/porfolio_lesson_plan/week_6/CLI_solution/userdata.sh

Or use the following command:

aws ec2 run-instances \
 --image-id $LATEST_AMI \
 --count 1 \
 --instance-type t2.micro \
 --key-name devenes \
 --security-groups roman_numbers_sec_grp \
 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=roman_numbers}]' \
 --user-data file:///home/ec2-user/userdata.sh

To see the each instances Ip we'll use describe instance CLI command:

aws ec2 describe-instances --filters "Name=tag:Name,Values=roman_numbers"

You can run the query to find Public IP and Instance ID of instances:

aws ec2 describe-instances --filters "Name=tag:Name,Values=roman_numbers" --query 'Reservations[].Instances[].PublicIpAddress[]'
aws ec2 describe-instances --filters "Name=tag:Name,Values=roman_numbers" --query 'Reservations[].Instances[].InstanceId[]'

Delete Instances:

aws ec2 terminate-instances --instance-ids xxxxxxxxxxxx

Delete Security Groups:

aws ec2 delete-security-group --group-name roman_numbers_sec_grp

Resources

https://docs.aws.amazon.com/cli/latest/userguide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment