AWS Classroomnotes 11/Mar/2023

AWS CLI Contd…

  • Lets try to create ubuntu 22.04 ec2 instance and install apache(php info)
  • Steps:

    • ami id
    • security group (22 and 80 )
    • key pair: my_id_rsa
    • instance size: t2.micro
  • Refer Here for the script in aws cli to create ec2 instance


  • Try doing the same to install php info page with ubuntu 20 ami
  • delete the ec2 instance

# get the ami id
ami_id=$(aws ec2 describe-images --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-*" --query "Images[0].ImageId" --output text)

# get default vpc id
vpc_id=$(aws ec2 describe-vpcs --filters "Name=is-default,Values=true" --query "Vpcs[0].VpcId" --output text)

# create security group with 22,80 ports opened
sg_id=$(aws ec2 create-security-group --group-name sshnhttpsg --description "open 22 and 80" --vpc-id $vpc_id --output text --query "GroupId")

# open 22,80 ports to all
aws ec2 authorize-security-group-ingress \
    --group-id $sg_id \
    --protocol tcp \
    --port 22 \
    --cidr ''

aws ec2 authorize-security-group-ingress \
    --group-id $sg_id \
    --protocol tcp \
    --port 80 \
    --cidr ''

# get the instance id
aws ec2 run-instances \
    --image-id $ami_id \
    --instance-type "t2.micro" \
    --key-name "my_id_rsa" \
    --security-group-ids $sg_id \

# using instance id fetch public ip
public_ip=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[0].Instances[0].PublicIpAddress" --output text) 
echo "http://${public_ip}"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner