본문 바로가기

퍼블릭 클라우드

AWS CLI를 사용하여 AMI를 생성하는 쉘 스크립트

반응형

AWS CLI를 사용하여 AMI를 생성하는 쉘 스크립트

createimage.sh 스크립트 생성

vim createimage.sh
#!/bin/bash

# Default values
DEFAULT_INSTANCE_ID="i-0b0e07d25b8fac832"
DEFAULT_AMI_NAME="instance-name-ami"
DEFAULT_AMI_VERSION="v1.1.1"
DEFAULT_AMI_DESCRIPTION="No description provided."

# Get input arguments or use defaults
INSTANCE_ID="${1:-$DEFAULT_INSTANCE_ID}"
AMI_NAME="${2:-$DEFAULT_AMI_NAME}"
AMI_VERSION="${3:-$DEFAULT_AMI_VERSION}"
AMI_DESCRIPTION="${4:-$DEFAULT_AMI_DESCRIPTION}"

# Generate current date
CURRENT_DATE=$(date +%Y%m%d)

# Display help message if requested
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  echo -e "\nUsage: ./createimage.sh [instance ID] [AMI name] [AMI version] [AMI description (optional)]\n"
else
  AMI_FULL_NAME="${AMI_NAME}-${CURRENT_DATE}-${AMI_VERSION}"

  # Create the AMI and tag the resources
  aws --region ap-northeast-2 ec2 create-image \
      --instance-id "$INSTANCE_ID" \
      --name "$AMI_FULL_NAME" \
      --description "$AMI_DESCRIPTION" \
      --tag-specifications "ResourceType=image,Tags=[{Key=Name,Value=$AMI_FULL_NAME}]" \
                          "ResourceType=snapshot,Tags=[{Key=Name,Value=$AMI_FULL_NAME}]" \
      --no-reboot
fi
더보기

예전 버전 #1

#!/bin/bash

# Check if instance ID, AMI name, and description are provided
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
  echo "Usage: ./createimage.sh [instance ID] [AMI name] [AMI version] [AMI description (optional)]"
  exit 1
fi

INSTANCE_ID="${1:-i-09cbc4624ea139076}"
AMI_NAME="${2:-prod-web-ami}"
AMI_VERSION="${3:-v0.0.1}"
AMI_DESCRIPTION="${4:-No description provided.}"

# Add current date and time to AMI name
CURRENT_DATE_TIME="$(date +%Y-%m-%d_%H-%M-%S)"
AMI_NAME="$AMI_NAME-$CURRENT_DATE_TIME-$AMI_VERSION"

# Create the AMI and tag the resources
aws ec2 create-image --instance-id "$INSTANCE_ID" --name "$AMI_NAME" --description "$AMI_DESCRIPTION" --no-reboot \
--tag-specifications "ResourceType=image,Tags=[{Key=Name,Value=$AMI_NAME}, {Key=Version,Value=$AMI_VERSION}]" \
"ResourceType=snapshot,Tags=[{Key=Name,Value=$AMI_NAME}, {Key=Version,Value=$AMI_VERSION}]"
chmod +x createimage.sh

 

참고URL

- Shell 스크립트로 Amazon Machine Image(AMI) 생성하기 : https://scbyun.com/1178

 

728x90
반응형