Lab 011: Attach Elastic Network Interface to EC2 Instance
Requirements
The Nautilus DevOps team has been creating a couple of services on AWS cloud. They have been breaking down the migration into smaller tasks, allowing for better control, risk mitigation, and optimization of resources throughout the migration process. Recently they came up with requirements mentioned below.An instance named nautilus-ec2 and an elastic network interface named nautilus-eni already exists in us-east-1 region.Attach the nautilus-eni network interface to the nautilus-ec2 instance.Make sure status is attached before submitting the task.Please make sure instance initialisation has been completed before submitting this task.
Note
This document preserves the original requirement text and uses one direct CLI workflow to attach the ENI.
Prerequisites
- AWS CLI is installed and authenticated.
- The instance
nautilus-ec2and network interface tagnautilus-eniexist inus-east-1. - Your identity can describe instances/network interfaces and attach ENIs.
Steps
INSTANCE_NAME="nautilus-ec2"
ENI_NAME="nautilus-eni"
REGION="us-east-1"
INSTANCE_ID=$(aws ec2 describe-instances \
--region "$REGION" \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text)
if [ -z "$INSTANCE_ID" ]; then
echo "Instance with name $INSTANCE_NAME not found."
exit 1
fi
ENI_ID=$(aws ec2 describe-network-interfaces \
--region "$REGION" \
--filters "Name=tag:Name,Values=$ENI_NAME" \
--query "NetworkInterfaces[*].NetworkInterfaceId" \
--output text)
if [ -z "$ENI_ID" ]; then
echo "Network Interface with name $ENI_NAME not found."
exit 1
fi
ATTACHMENT_ID=$(aws ec2 attach-network-interface \
--region "$REGION" \
--network-interface-id "$ENI_ID" \
--instance-id "$INSTANCE_ID" \
--device-index 1 \
--query "AttachmentId" \
--output text)
echo "Attached Network Interface $ENI_NAME to instance $INSTANCE_NAME with attachment ID $ATTACHMENT_ID."
Verification
- Confirm the network interface status is
in-useand attachment status isattached. - Confirm the ENI is attached to the expected instance.
aws ec2 describe-network-interfaces \
--region us-east-1 \
--network-interface-ids "$ENI_ID" \
--query "NetworkInterfaces[0].{NetworkInterfaceId:NetworkInterfaceId,Status:Status,AttachmentStatus:Attachment.Status,InstanceId:Attachment.InstanceId}" \
--output table