Modify EC2 Instance Type via AWS CLI

cloud(aws) level 1 lab 39
how to perform steps

  1. Before you can change the instance type, the EC2 instance must be in a stopped state.

aws ec2 stop-instances --instance-ids <instance-id>

Replace <instance-id> with the ID of the EC2 instance you want to modify.

  1. Once the instance is stopped, you can change its instance type using the following command:

aws ec2 modify-instance-attribute --instance-id <instance-id> --instance-type "{\"Value\": \"<new-instance-type>\"}"*

Replace <instance-id> with the ID of the instance you want to modify and <new-instance-type> with the desired EC2 instance type (e.g., t3.medium).

  1. After modifying the instance type, start the EC2 instance again

aws ec2 start-instances --instance-id <instance-id>

  1. Stop the EC2 instance
    aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances --filters “Name=tag:Name,Values=devops-ec2” --query “Reservations[].Instances[].InstanceId” --output text)

  2. Modify the instance type
    aws ec2 modify-instance-attribute --instance-id $(aws ec2 describe-instances --filters “Name=tag:Name,Values=devops-ec2” --query “Reservations[].Instances[].InstanceId” --output text) --instance-type “t2.nano”

  3. Start the EC2 instance
    aws ec2 start-instances --instance-ids $(aws ec2 describe-instances --filters “Name=tag:Name,Values=devops-ec2” --query “Reservations[].Instances[].InstanceId” --output text)

  4. Verify the instance state (optional)
    aws ec2 describe-instances --filters “Name=tag:Name,Values=devops-ec2” --query “Reservations[].Instances[].[InstanceId,State.Name,InstanceType]” --output table