前提条件
AutoScalingへの権限
AutoScalingに対してフル権限があること。
EC2への権限
EC2に対してRead権限があること。
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 autoscaling describe-launch-configurations \
        --query 'LaunchConfigurations[].LaunchConfigurationName'
結果(例):
  {
    "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
      }
    ]
  }
利用する起動設定を指定します。
AS_LAUNCH_CONFIG_NAME='launchcongig-handson-20160829'   echo ${AS_LAUNCH_CONFIG_NAME}
- 事前作業
=========== 
1.1. AutoScalingグループ名の決定
AS_GROUP_NAME="asgroup-handson-$(date +%Y%m%d)" \
          && echo ${AS_GROUP_NAME}
同名のAutoScalingグループが存在しないことを確認します。
aws autoscaling describe-auto-scaling-groups \
        --auto-scaling-group-names ${AS_GROUP_NAME}
結果:
  {
      "AutoScalingGroups": []
  }
1.2. 稼動するインスタンス数の指定
今回のAutoScalingグループの最大インスタンス数は2にします。
AS_GROUP_MAX='2'
今回のAutoScalingグループの最小インスタンス数は1にします。
AS_GROUP_MIN='1'
実際に稼動するインスタンス数は1にします。
AS_DESIRED_CAPACITY='1'
1.3. サブネットの指定
AutoScalingグループの対象となるサブネットを指定します。
複数のAZにあるサブネットを指定すると、AutoScalingが自動的に利用可能な
AZにあるサブネットを選択してくれます。
ここでは、現在のリージョンに存在するサブネットを全て指定します。
ARRAY_VPC_SUBNET=$( \
        aws ec2 describe-subnets \
          --filters Name=vpcId,Values=${VPC_ID} \
          --query 'Subnets[].SubnetId' \
          --output text \
)
ARRAY_VPC_SUBNET=$( echo ${ARRAY_VPC_SUBNET} | sed 's/ /,/g' ) \
        && echo ${ARRAY_VPC_SUBNET}
結果(例):
  subnet-0ff1c878 subnet-xxxxxx subnet-xxxxxxxx
1.4. 稼動インスタンスを確認
同一リージョンでインスタンスが起動していないことを確認します。
EC2_INSTANCE_STATUS='running'
aws ec2 describe-instances \
        --filters Name=instance-state-name,Values=${EC2_INSTANCE_STATUS}
結果:
  {
      "Reservations": []
  }
- AutoScalingグループの作成
============================ 
2.1. AutoScalingグループの作成
cat << ETX
        AS_LAUNCH_CONFIG_NAME: ${AS_LAUNCH_CONFIG_NAME}
        AS_GROUP_NAME:         ${AS_GROUP_NAME}
        AS_GROUP_MIN:          ${AS_GROUP_MIN}
        AS_GROUP_MAX:          ${AS_GROUP_MAX}
        ARRAY_VPC_SUBNET:     "${ARRAY_VPC_SUBNET}"
ETX
aws autoscaling create-auto-scaling-group \
        --launch-configuration-name ${AS_LAUNCH_CONFIG_NAME} \
        --auto-scaling-group-name ${AS_GROUP_NAME} \
        --min-size ${AS_GROUP_MIN} \
        --max-size ${AS_GROUP_MAX} \
        --vpc-zone-identifier "${ARRAY_VPC_SUBNET}"
結果(例):
  (戻り値なし)
- 事後確認
=========== 
3.1. AutoScalingグループの確認
aws autoscaling describe-auto-scaling-groups \
        --auto-scaling-group-names ${AS_GROUP_NAME}
結果(例):
  {
    "AutoScalingGroups": [
      {
          "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:XXXXXXXXXXXX:autoScalingGroup:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:autoScalingGroupName/asgroup-handson-20160829",
          "HealthCheckGracePeriod": 0,
          "SuspendedProcesses": [],
          "DesiredCapacity": 11,
          "Tags": [],
          "EnabledMetrics": [],
          "LoadBalancerNames": [],
          "AutoScalingGroupName": "asgroup-handson-20160829",
          "DefaultCooldown": 300,
          "MinSize": |AS_GROUP_MIN|,
          "Instances": [
              {
                  "InstanceId": "i-xxxxxxxx",
                  "AvailabilityZone": "ap-northeast-1",
                  "HealthStatus": "Healthy",
                  "LifecycleState": "InService",
                  "LaunchConfigurationName": "launchcongig-handson-20160829"
              }
          ],
          "MaxSize": 2,
          "VPCZoneIdentifier": "subnet-xxxxxxxx",
          "TerminationPolicies": [
              "Default"
          ],
          "LaunchConfigurationName": "launchcongig-handson-20160829",
          "CreatedTime": "2015-08-01T01:23:45.678Z",
          "AvailabilityZones": [
              "ap-northeast-1a"
          ],
          "HealthCheckType": "EC2"
      }
    ]
  }
3.2. Scalingアクティビティの確認
MAX_ITEMS='1'
aws autoscaling describe-scaling-activities \
        --auto-scaling-group-name ${AS_GROUP_NAME} \
        --max-items ${MAX_ITEMS}
結果(例):
  {
    "Activities": [
      {
        "Description": "Launching a new EC2 instance: i-xxxxxxxx",
        "AutoScalingGroupName": "asgroup-handson-20160829",
        "ActivityId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "Details": "{"Availability Zone":"ap-northeast-1a","Subnet ID":"subnet-xxxxxxxx"}",
        "StartTime": "2016-08-29T01:23:45.678Z",
        "Progress": 100,
        "EndTime": "2016-08-29T01:23:45Z",
        "Cause": "At 2016-08-29T01:23:45Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2016-08-292T01:23:45Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.",
        "StatusCode": "Successful"
      }
    ]
  }
3.3. 稼動インスタンスを確認
同一リージョンでインスタンスが起動していることを確認します。
aws ec2 describe-instances \
        --filters Name=instance-state-name,Values=${EC2_INSTANCE_STATUS} \
        --query 'Reservations[].Instances[].InstanceId'
結果(例):
  i-xxxxxxxx