version: 0.2
phases:
build:
commands:
- |
#!/bin/bash
# Author - Rajesh Kumar
# Set AWS region
export AWS_DEFAULT_REGION=us-east-1
# Get running all instance IDs
instance_ids=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:environment,Values=uat" --query 'Reservations[].Instances[].InstanceId' --output text)
echo "List of Instances: $instance_ids"
# Create AMIs from instance IDs
for instance_id in $instance_ids; do
instance_name=$(aws ec2 describe-instances --instance-ids $instance_id --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value' --output text)
timestamp=$(date +%Y-%m-%d-%H-%M-%S)
ami_name="$instance_name-$timestamp"
aws ec2 create-image --instance-id $instance_id --name "$ami_name" --no-reboot
done
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EC2Read",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeImages",
"ec2:CreateImage"
],
"Resource": "*"
}
]
}
Latest posts by Rajesh Kumar (see all)
- Best AI tools for Software Engineers - November 4, 2024
- Installing Jupyter: Get up and running on your computer - November 2, 2024
- An Introduction of SymOps by SymOps.com - October 30, 2024