Lab 007: Change EC2 Instance Type
Requirements
During the migration process, the Nautilus DevOps team created several EC2 instances in different regions. They are currently in the process of identifying the correct resources and utilization and are making continuous changes to ensure optimal resource utilization. Recently, they discovered that one of the EC2 instances was underutilized, prompting them to decide to change the instance type. Please make sure the Status check is completed (if its still in Initializing state) before making any changes to the instance.
-
Change the instance type from t2.micro to t2.nano for datacenter-ec2 instance.
-
Make sure the ec2 instance datacenter-ec2 is in running state after the change.
-
Create the instance in us-east-1 region.
The Requirements section remains unchanged. The commands below implement one primary CLI path to change instance type and return the instance to running state.
Prerequisites
- AWS CLI is installed and authenticated.
- The instance
datacenter-ec2already exists inus-east-1. - The caller has permissions to stop, modify, and start the target instance.
Steps
For the AWS credentials, enter showcreds command on aws-client host to retrieve the credentials. For further details, check out the Project Nautilus documentation.
To create a EC2 instance with specified requirements using the AWS CLI, you can follow these steps:
#!/bin/bash
# Step 1: Get the Instance ID for datacenter-ec2
INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=datacenter-ec2" \
--query "Reservations[0].Instances[0].InstanceId" \
--output text)
# Step 2: Stop the Instance
echo "Stopping instance $INSTANCE_ID..."
aws ec2 stop-instances --instance-ids $INSTANCE_ID
aws ec2 wait instance-stopped --instance-ids $INSTANCE_ID
echo "Instance $INSTANCE_ID has been stopped."
# Step 3: Change the Instance Type
echo "Changing instance type to t2.nano..."
aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --instance-type "{\"Value\": \"t2.nano\"}"
echo "Instance type changed to t2.nano."
# Step 4: Start the Instance
echo "Starting instance $INSTANCE_ID..."
aws ec2 start-instances --instance-ids $INSTANCE_ID
aws ec2 wait instance-running --instance-ids $INSTANCE_ID
echo "Instance $INSTANCE_ID is now running."
# Step 5: Verify the Changes
echo "Verifying the instance state and type..."
aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--query "Reservations[0].Instances[0].[InstanceId,State.Name,InstanceType,Tags]" \
--output table
Verification
- Confirm the instance named
datacenter-ec2isrunning. - Confirm the instance type is
t2.nanoinus-east-1.
aws ec2 describe-instances \
--region us-east-1 \
--filters "Name=tag:Name,Values=datacenter-ec2" \
--query "Reservations[0].Instances[0].{InstanceId:InstanceId,State:State.Name,InstanceType:InstanceType}" \
--output table