본문 바로가기

퍼블릭 클라우드

[AWS] Shell 스크립트로 Amazon Machine Image(AMI) 생성하기

728x90

Shell 스크립트로 Amazon Machine Image(AMI) 생성하기

인스턴스 조회

instance-search.sh 스크립트

#!/bin/bash

aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[*].Instances[*].{A_Name:Tags[?Key==`Name`]|[0].Value,B_Instance:InstanceId,C_Type:InstanceType,D_PrivateIP:PrivateIpAddress,E_PublicIP:PublicIpAddress,F_Status:State.Name,G_CreatedDate:LaunchTime}' \
--output table

>> output <<

AMI 생성

createami.sh 스크립트

#!/bin/bash

IID=${1:-i-1111}
ImageName="prod-web-ami"
CurrentTime=`date +%Y%m%d`
ImageVersion=${2:-v1.1.1}
ImageDescription="operating(production) web server ami"


if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo -e "\nUsage: ./createami.sh [OPTION] [instance id] [image version]\n"
else
	aws ec2 create-image \
        --instance-id $IID \
        --name $ImageName-$CurrentTime-$ImageVersion \
        --description "$ImageDescription" \
        --tag-specifications 'ResourceType=image,Tags=[{Key=Name,Value=prod-web-ami}]' \
                        'ResourceType=snapshot,Tags=[{Key=Name,Value=prod-web-ami}]' \
        --no-reboot
fi

AWS 웹 콘솔

- 이미지 > AMI

- Elastic Block Store > 스냅샷

 

728x90