AWS
Discover AWS CLI
Installing the AWS CLI with Pip
sudo apt install -y python-pip
pip install awscli --upgrade --user
aws --version
Configuring the AWS CLI
aws configure
AWS Access Key ID [None]: ****
AWS Secret Access Key [None]: ****
Default region name [None]:
Default output format [None]:
cat ~/.aws/credentials
[default]
aws_access_key_id = ***
aws_secret_access_key = ***
cat ~/.aws/config
[default]
output = json
region = eu-west-3
aws sts get-caller-identity --output text --query 'Account'
aws iam get-account-summary
aws ec2 describe-regions --output table
ec2 available commands
Create security group
aws ec2 create-security-group --group-name my-sg --description "My security group"
aws ec2 delete-security-group --group-name my-sg
aws ec2 describe-security-groups
aws ec2 create-security-group --group-name EC2SecurityGroup --description "Security Group for EC2 instances to allow port 22"
aws ec2 authorize-security-group-ingress --group-name EC2SecurityGroup --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 describe-security-groups --group-names EC2SecurityGroup
allow port 80,443
aws ec2 create-security-group --group-name EC2WebSecurityGroup --description "Security Group for Web EC2 instances to allow port 80,443"
aws ec2 authorize-security-group-ingress --group-name EC2WebSecurityGroup --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name EC2WebSecurityGroup --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 describe-security-groups --group-names EC2WebSecurityGroup
aws ec2 describe-security-groups
aws ec2 modify-instance-attribute --instance-id i-0d1741fe066c6cd10 --groups sg-0e5244d54d9886269 sg-04cae9f654d83289d
Run instances
Find the current Ubuntu Server 16.04 LTS AMI
aws ec2 describe-images --owners 099720109477 --filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-????????' 'Name=state,Values=available' | jq -r '.Images | sort_by(.CreationDate) | last(.[]).ImageId'
run-instances
aws ec2 run-instances --image-id ami-0370f4064dbc392b9 --key-name test1 --security-groups EC2SecurityGroup --instance-type t2.micro --placement AvailabilityZone=eu-west-3c --block-device-mappings DeviceName=/dev/sdh,Ebs={VolumeSize=10} --count 1 --tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]'
Start, Stop, Terminate instances
aws ec2 describe-volumes
aws ec2 start-instances --instance-ids i-0d1741fe066c6cd10
aws ec2 stop-instances --instance-ids i-00296f36a1008abaf
aws ec2 terminate-instances --instance-ids i-00296f36a1008abaf
Connecting to Your Linux Instance Using SSH
ssh -i '/home/username/test1.pem' ubuntu@IP_OR_HOSTANME
Create tags
aws ec2 create-tags --resources i-06fa5cfb1961b9212 --tags Key=Stack,Value=production
aws ec2 describe-instances --filters Name=tag-key,Values=Stack
EBS volume snapshot
aws ec2 run-instances --image-id ami-0370f4064dbc392b9 --key-name test1 --security-groups EC2SecurityGroup --instance-type t2.micro --placement AvailabilityZone=eu-west-3c --block-device-mappings DeviceName=/dev/sdh,Ebs={VolumeSize=10} --count 1 --tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]'
aws ec2 create-snapshot --volume-id vol-0f7129e425a0eb669 --description 'This is my root volume snapshot' --tag-specifications 'ResourceType=snapshot,Tags=[{Key=purpose,Value=prod},{Key=costcenter,Value=cc123}]'
aws ec2 describe-volumes
aws ec2 stop-instances --instance-ids i-073ae87873b158966
aws ec2 detach-volume --volume-id vol-0f7129e425a0eb669
aws ec2 delete-volume --volume-id vol-0f7129e425a0eb669
aws ec2 describe-snapshots
aws ec2 create-volume --snapshot-id snap-01e4985a4be46e81d --availability-zone eu-west-3c
aws ec2 attach-volume --device /dev/sda1 --instance-id i-073ae87873b158966 --volume-id vol-0b1e0a71024ad3542
aws ec2 start-instances --instance-ids i-073ae87873b158966
aws ec2 delete-snapshot --snapshot-id snap-01e4985a4be46e81d
ssh -i '/home/username/test1.pem' ubuntu@IP_OR_HOSTANME
Amazon EBS-backed AMI
aws ec2 describe-instances --filters "Name=tag-key,Values=webserver"
aws ec2 create-image --instance-id i-073ae87873b158966 --name "My server" --description "An AMI for my server" --no-reboot
aws ec2 run-instances --image-id ami-0e2b697a667a5f70c --key-name test1 --security-groups EC2SecurityGroup --instance-type t2.micro --placement AvailabilityZone=eu-west-3c --count 1 --tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]'
ssh -i '/home/username/test1.pem' ubuntu@IP_OR_HOSTANME
aws ec2 deregister-image --image-id ami-0e2b697a667a5f70c
Auto Scaling
autoscaling ec2 available commands
Create a Launch Configuration
Get ami image-id:
aws sts get-caller-identity --output text --query 'Account'
aws ec2 describe-images --owners "ACCOUNTNUMBER"
Create a Launch Configuration
aws autoscaling create-launch-configuration --launch-configuration-name my-launch-config --image-id ami-03604910459f6b96e --instance-type t2.micro --instance-monitoring Enabled=true --key-name test1 --security-groups EC2WebSecurityGroup EC2SecurityGroup
aws autoscaling describe-launch-configurations --launch-configuration-names my-launch-config
Create an Auto Scaling Group
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-auto-scaling-group --launch-configuration-name my-launch-config --min-size 1 --max-size 3 --availability-zones eu-west-3c
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-auto-scaling-group
aws autoscaling describe-launch-configurations --launch-configuration-names my-launch-config
Verify Your Auto Scaling Group
aws autoscaling enable-metrics-collection --auto-scaling-group-name my-auto-scaling-group --granularity "1Minute"
aws cloudwatch list-metrics --namespace "AWS/AutoScaling" --dimensions Name=AutoScalingGroupName,Value=my-auto-scaling-group
aws autoscaling describe-scaling-activities --auto-scaling-group-name my-auto-scaling-group
aws autoscaling describe-auto-scaling-instances
(Optional) Delete Your Scaling Infrastructure
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name my-auto-scaling-group --force-delete
aws autoscaling delete-launch-configuration --launch-configuration-name my-launch-config
Virtual Private Cloud (VPC)
Elastic Load Balancer
AWS SDK for Python
pip install boto3
Leave a comment