1
1

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2 # AMI作成

Last updated at Posted at 2017-02-13

前提条件

EC2への権限

EC2に対してフル権限があること。

AWS CLIのバージョン

以下のバージョンで動作確認済

  • AWS CLI 1.11.34
コマンド
aws --version

結果(例):

  aws-cli/1.11.34 Python/2.7.10 Darwin/15.6.0 botocore/1.4.91

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

0. 準備

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. プロファイルの確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list

結果(例):

        Name                    Value             Type    Location
        ----                    -----             ----    --------
     profile       ec2as_full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
  access_key     ****************XXXX shared-credentials-file
  secret_key     ****************XXXX shared-credentials-file
      region        ap-northeast-1        env    AWS_DEFAULT_REGION

0.3. インスタンスIDの指定

既存のインスタンスに割り当てられているIPアドレスを確認します。

ここでは、10.192.0.22を割り当てられているインスタンスを選択します。

変数の設定
EC2_PRIVATE_ADDR='10.192.0.22'

インスタンスIDを取得します。

コマンド
EC2_INSTANCE_ID=$( \
        aws ec2 describe-instances \
          --filters Name=private-ip-address,Values=${EC2_PRIVATE_ADDR} \
          --query 'Reservations[].Instances[].InstanceId' \
          --output text \
) \
        && echo ${EC2_INSTANCE_ID}

結果(例):

  i-xxxxxxxxxxxxxxxxx

1. 事前作業

1.2. イメージ名の決定

変数の設定
EC2_IMAGE_NAME="redmine-prototype-$( date +%Y%m%d )" \
        && echo ${EC2_IMAGE_NAME}

同名のイメージの不存在確認

コマンド
aws ec2 describe-images \
        --filter "Name=name, Values=${EC2_IMAGE_NAME}"

結果(例):

  {
    "Images": []
  }
変数の設定
EC2_IMAGE_DESC='Redmine Image (Prototype)'

2. AMIの作成

2.1. 対象インスタンスのステータス確認

コマンド
EC2_INSTANCE_STATE=$( \
        aws ec2 describe-instances \
        --instance-ids ${EC2_INSTANCE_ID} \
        --query 'Reservations[].Instances[].State.Name' \
        --output text \
) \
        && echo ${EC2_INSTANCE_STATE}

結果(例):

  running

2.2. AMIの作成

変数の確認
cat << ETX

        EC2_INSTANCE_ID:  ${EC2_INSTANCE_ID}
        EC2_IMAGE_NAME:   ${EC2_IMAGE_NAME}
        EC2_IMAGE_DESC:  "${EC2_IMAGE_DESC}"

ETX
コマンド
aws ec2 create-image \
        --instance-id ${EC2_INSTANCE_ID} \
        --name ${EC2_IMAGE_NAME} \
        --description "${EC2_IMAGE_DESC}"

結果(例):

  {
    "ImageId": "ami-xxxxxxxx"
  }

インスタンスがリブートを開始します。

3. 事後作業

3.1. 対象インスタンスのステータス確認

コマンド
EC2_INSTANCE_STATE=$( \
        aws ec2 describe-instances \
        --instance-ids ${EC2_INSTANCE_ID} \
        --query 'Reservations[].Instances[].State.Name' \
        --output text \
) \
        && echo ${EC2_INSTANCE_STATE}

結果(例):

  running

イメージ情報が表示されるまで数分かかります。

3.2. AMIのステータス確認

コマンド
EC2_IMAGE_STATE=$( \
        aws ec2 describe-images \
          --filters Name=name,Values="${EC2_IMAGE_NAME}" \
          --query "Images[?Name == \`${EC2_IMAGE_NAME}\`].State" \
          --output text \
) \
        && echo ${EC2_IMAGE_STATE}

結果(例):

  available

availableになれば作成は完了しています。

AMIの確認

コマンド
aws ec2 describe-images \
        --filter "Name=name, Values=${EC2_IMAGE_NAME}"

結果(例):

  {
    "Images": [
      {
          "VirtualizationType": "hvm",
          "Name": "redmine-prototype-20170131",
          "Hypervisor": "xen",
          "EnaSupport": true,
          "SriovNetSupport": "simple",
          "ImageId": "ami-xxxxxxxx",
          "State": "available",
          "BlockDeviceMappings": [
              {
                  "DeviceName": "/dev/xvda",
                  "Ebs": {
                      "DeleteOnTermination": true,
                      "SnapshotId": "snap-xxxxxxxx",
                      "VolumeSize": 8,
                      "VolumeType": "gp2",
                      "Encrypted": false
                  }
              }
          ],
          "Architecture": "x86_64",
          "ImageLocation": "XXXXXXXXXXXX/redmine-prototype-20170131",
          "RootDeviceType": "ebs",
          "OwnerId": "XXXXXXXXXXXX",
          "RootDeviceName": "/dev/xvda",
          "CreationDate": "2017-02-13T01:23:45.000Z",
          "Public": false,
          "ImageType": "machine",
          "Description": "Redmine Image (Prototype)"
      }
    ]
  }

完了

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1