Skip to content

Instantly share code, notes, and snippets.

@stefan-matic
Last active May 25, 2022 10:15
Show Gist options
  • Save stefan-matic/1365ccc5b2cb3de42faead9402a39056 to your computer and use it in GitHub Desktop.
Save stefan-matic/1365ccc5b2cb3de42faead9402a39056 to your computer and use it in GitHub Desktop.
Swaps volumes of two EC2 instance using AWS CLI. Includes Terraform to spawn two ec2 instances for testing.
#!/bin/bash
AWS_PROFILE="YOUR-AWS-PROFILE"
AWS_REGION="YOUR-AWS-REGION"
# OLD INSTANCE INFO
OLD_INSTANCE_ID="i-0123456789abcdef0"
OLD_INSTANCE_VOLUME_ID="vol-0123456789abcdef0"
OLD_INSTANCE_MOUNT_POINT="/dev/sda1"
# NEW INSTANCE INFO
NEW_INSTANCE_ID="i-0fedcba9876543210"
NEW_INSTANCE_VOLUME_ID="vol-0fedcba9876543210"
NEW_INSTANCE_MOUNT_POINT="/dev/sda1"
stop_detach_old() {
echo " >>> Stop and detach OLD instance started..."
aws ec2 stop-instances \
--instance-ids "${OLD_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the OLD instance to be stopped..."
aws ec2 wait instance-stopped \
--instance-ids "${OLD_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Old instance is stopped!"
aws ec2 detach-volume \
--volume-id "${OLD_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the OLD volume to become available..."
aws ec2 wait volume-available \
--volume-ids "${OLD_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Old volume is now available!"
echo " >>> Stop and detach OLD instance completed!"
}
stop_detach_new() {
echo " >>> Stop and detach NEW instance started..."
aws ec2 stop-instances \
--instance-ids "${NEW_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the NEW instance to be stopped..."
aws ec2 wait instance-stopped \
--instance-ids "${NEW_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Old instance is stopped!"
aws ec2 detach-volume \
--volume-id "${NEW_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the NEW volume to become available..."
aws ec2 wait volume-available \
--volume-ids "${NEW_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Old volume is now available!"
echo " >>> Stop and detach NEW instance completed!"
}
attach_start_new() {
echo " >>> Attach and start NEW instance started..."
aws ec2 attach-volume \
--device "${OLD_INSTANCE_MOUNT_POINT}" \
--instance-id "${NEW_INSTANCE_ID}" \
--volume-id "${OLD_INSTANCE_VOLUME_ID}"\
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the OLD volume to attach to the NEW instance..."
aws ec2 wait volume-in-use \
--volume-ids "${OLD_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> OLD volume is now attached to the NEW instance!"
aws ec2 start-instances \
--instance-ids "${NEW_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Attach and start NEW instance completed!"
}
attach_start_old() {
echo " >>> Attach and start OLD instance started..."
aws ec2 attach-volume \
--device "${NEW_INSTANCE_MOUNT_POINT}" \
--instance-id "${OLD_INSTANCE_ID}" \
--volume-id "${NEW_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Wait for the NEW volume to attach to the OLD instance...."
aws ec2 wait volume-in-use \
--volume-ids "${NEW_INSTANCE_VOLUME_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> NEW volume is now attached to the OLD instance!"
aws ec2 start-instances \
--instance-ids "${OLD_INSTANCE_ID}" \
--profile "${AWS_PROFILE}" \
--region "${AWS_REGION}" \
--output text
echo " >>> Attach and start OLD instance completed!"
}
stop_detach_old
stop_detach_new
attach_start_new
attach_start_old
echo "DONE!"
provider "aws" {
region = "YOUR-AWS-REGION"
profile = "YOUR-AWS-PROFILE"
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_vpc" "vpc" {
cidr_block = "172.16.0.0/16"
tags = {
Name = "EC2-volume-swap"
}
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "172.16.10.0/24"
availability_zone = "eu-central-1a"
tags = {
Name = "EC2-volume-swap"
}
}
resource "aws_instance" "old_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.old_server.id
device_index = 0
}
tags = {
Name = "Old Server"
}
}
resource "aws_network_interface" "old_server" {
subnet_id = aws_subnet.subnet.id
private_ips = ["172.16.10.100"]
tags = {
Name = "primary_network_interface"
}
}
resource "aws_instance" "new_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.new_server.id
device_index = 0
}
tags = {
Name = "New Server"
}
}
resource "aws_network_interface" "new_server" {
subnet_id = aws_subnet.subnet.id
private_ips = ["172.16.10.200"]
tags = {
Name = "primary_network_interface"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment