MultiCloud Classroom notes 14/Feb/2025

CLI contd..

CLI to create, stop and resize, terminate ec2 instances

  • Creating ec2 instance
  • output: ec2 instance with a public ip
  • inputs:
    • tags: Name: from-cli
    • ami: ami-00bb6a80f01f03502
    • instance type: t2-micro
    • subnet: subnet-067813a42fb6cbebe
    • key pair: my_idrsa
    • security group: docker
    • region: ap-south-1
  • command
aws ec2 run-instances \
    --image-id "ami-00bb6a80f01f03502" \
    --instance-type "t2.micro" \
    --key-name "my_idrsa" \
    --subnet-id "subnet-067813a42fb6cbebe" \
    --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=from-cli}]" \
    --associate-public-ip-address \
    --security-group-ids "sg-077690d1b4c7e9836"
  • Lets start writing in script to create ec2
#!/bin/bash

# lets fail the scrip if number of arguments is not six
number=$#

if [[ $number -ne 6 ]]; then
    echo "wrong usage you are supposed to 6 arguments"
    echo ''' Try following command
    ./manageec2.sh ami-00bb6a80f01f03502 t2.micro my_idrsa subnet-067813a42fb6cbebe from-cli sg-077690d1b4c7e9836
    '''
    exit 1
fi

image_id=$1
instance_type=$2
key_name=$3
subnet_id=$4
name=$5
sg_id=$6


echo "number of arguments passed are ${number}"


# aws ec2 run-instances \
#     --image-id "ami-00bb6a80f01f03502" \
#     --instance-type "t2.micro" \
#     --key-name "my_idrsa" \
#     --subnet-id "subnet-067813a42fb6cbebe" \
#     --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=from-cli}]" \
#     --associate-public-ip-address \
#     --security-group-ids "sg-077690d1b4c7e9836"


aws ec2 run-instances \
    --image-id ${image_id} \
    --instance-type ${instance_type} \
    --key-name ${key_name} \
    --subnet-id ${subnet_id} \
    --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${name}}]" \
    --associate-public-ip-address \
    --security-group-ids ${sg_id}
  • Now write a script to stop the ec2 instance, try taking one input instance id
  • Now write a script to start the ec2 instance, try taking one input instance id
  • Now write a script to terminate the ec2 instance, try taking one input instance id

AWS CLI Output formats

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%