前提条件
AutoScalingへの権限
AutoScalingに対してフル権限があること。
EC2への権限
EC2に対してフル権限があること。
AWS CLIのバージョン
以下のバージョンで動作確認済
- AWS CLI 1.10.58
 
コマンド
aws --version
結果(例):
  aws-cli/1.10.58 Python/2.7.11 Darwin/15.6.0 botocore/1.4.48
デフォルトVPCの存在
デフォルトVPCが存在すること。
コマンド
VPC_ID=$( \
        aws ec2 describe-vpcs \
          --filters Name=isDefault,Values=true \
          --query 'Vpcs[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}
結果(例):
  vpc-xxxxxxxx
- 準備
======= 
0.1. リージョンの決定
変数の設定
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. セキュリティグループの指定
まず、セキュリティグループの一覧を確認します。
コマンド
aws ec2 describe-security-groups \
        --query 'SecurityGroups[].GroupName'
結果(例):
  [
    "default",
    "ec2-ssh-global-inbound"
  ]
利用するセキュリティグループ名を指定します。
変数の設定
VPC_SG_NAME='ec2-ssh-global-inbound'
コマンド
VPC_SG_ID=$( \
        aws ec2 describe-security-groups \
          --filter Name=group-name,Values=${VPC_SG_NAME} \
          --query 'SecurityGroups[].GroupId' \
          --output text \
) \
        && echo ${VPC_SG_ID}
結果(例):
  sg-xxxxxxxx
存在しない場合は、作成します:
http://qiita.com/tcsh/items/77f8c9887634fd67f4d8
変数の設定
ARRAY_VPC_SG_ID="${VPC_SG_ID} ${ARRAY_VPC_SG_ID}" \
          && echo ${ARRAY_VPC_SG_ID}
0.4. キーペアの指定
作成手順: http://qiita.com/tcsh/items/59303d9506ca7d13f744
まず、キーペアの一覧を確認します。
コマンド
aws ec2 describe-key-pairs \
        --query 'KeyPairs[].KeyName'
結果(例):
  <キーペアー名>
利用するキーペア名を指定します。
変数の設定
EC2_KEYPAIR_NAME='<キーペアー名>'
利用するキーペアの秘密鍵ファイルを指定します。
変数の設定
FILE_SSH_KEY="${HOME}/.ssh/<キーペア秘密鍵のファイル名>" \
        echo ${FILE_SSH_KEY}
秘密鍵が存在することを確認しましょう。
コマンド
ls ${FILE_SSH_KEY}
- 事前作業
=========== 
1.1. 起動設定名の決定
変数の設定
AS_LAUNCH_CONFIG_NAME="launchcongig-handson-$(date +%Y%m%d)" \
        && echo ${AS_LAUNCH_CONFIG_NAME}
同名の起動設定が存在しないことを確認します。
コマンド
aws autoscaling describe-launch-configurations \
        --launch-configuration-names ${AS_LAUNCH_CONFIG_NAME} 
結果:
  {
      "LaunchConfigurations": []
  }
1.2. インスタンスタイプの決定
変数の設定
EC2_INSTANCE_TYPE='t2.micro'
1.3. イメージIDの決定
AMIを選択します。
変数の設定
AMZLINUX_VERSION='2016.03.3'
EC2_IMAGE_NAME="amzn-ami-hvm-${AMZLINUX_VERSION}.x86_64-gp2"
コマンド
EC2_IMAGE_ID=$( \
        aws ec2 describe-images \
          --filters Name=name,Values="${EC2_IMAGE_NAME}" \
          --query 'Images[].ImageId' --output text \
) \
        && echo ${EC2_IMAGE_ID}
結果(例):
  ami-XXXXXXXX
- 起動設定の作成
================= 
2.1. 起動設定の作成
変数の確認
cat << ETX
        AS_LAUNCH_CONFIG_NAME: ${AS_LAUNCH_CONFIG_NAME}
        EC2_IMAGE_ID:          ${EC2_IMAGE_ID}
        EC2_INSTANCE_TYPE:     ${EC2_INSTANCE_TYPE}
        ARRAY_VPC_SG_ID:       ${ARRAY_VPC_SG_ID}
        EC2_KEYPAIR_NAME:      ${EC2_KEYPAIR_NAME}
ETX
コマンド
aws autoscaling create-launch-configuration \
        --launch-configuration-name ${AS_LAUNCH_CONFIG_NAME} \
        --image-id ${EC2_IMAGE_ID} \
        --instance-type ${EC2_INSTANCE_TYPE} \
        --security-groups ${ARRAY_VPC_SG_ID} \
        --key-name ${EC2_KEYPAIR_NAME} \
        --associate-public-ip-address
結果:
  (戻り値なし)
2.2. 起動設定の確認
コマンド
aws autoscaling describe-launch-configurations \
        --launch-configuration-names ${AS_LAUNCH_CONFIG_NAME} 
結果(例):
  {
    "LaunchConfigurations": [
      {
          "UserData": "",
          "EbsOptimized": false,
          "LaunchConfigurationARN": "arn:aws:autoscaling:ap-northeast-1:XXXXXXXXXXXX:launchConfiguration:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:launchConfigurationName/launchcongig-handson-20160829",
          "InstanceMonitoring": {
              "Enabled": true
          },
          "ClassicLinkVPCSecurityGroups": [],
          "CreatedTime": "2015-08-01T01:23:45.678Z",
          "BlockDeviceMappings": [],
          "KeyName": "prjx-ap-northeast-1-ec2",
          "SecurityGroups": [
              "sg-xxxxxxxx"
          ],
          "LaunchConfigurationName": "launchcongig-handson-20160829",
          "KernelId": "",
          "RamdiskId": "",
          "ImageId": "ami-xxxxxxxx",
          "InstanceType": "t2.micro",
          "AssociatePublicIpAddress": true
      }
    ]
  }