前提条件
AutoScalingへの権限
AutoScalingに対してフル権限があること。
AWS CLIのバージョン
以下のバージョンで動作確認済
- AWS CLI 1.11.14
aws --version
結果(例):
aws-cli/1.11.14 Python/2.7.10 Darwin/15.6.0 botocore/1.4.71
バージョンが古い場合は最新版に更新しましょう。
sudo -H pip install -U awscli
- 準備
=======
0.1. リージョンの決定
export AWS_DEFAULT_REGION='ap-northeast-1'
0.2. 変数の確認
プロファイルが想定のものになっていることを確認します。
aws configure list
結果(例)
Name Value Type Location
---- ----- ---- --------
profile iamFull-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
AssumeRoleを利用している場合はprofileが ''と表示されます。 それ以外のときにprofileが '' と表示される場合は、以下を実行してください。
変数の設定:
export AWS_DEFAULT_PROFILE=<IAMユーザ名>
0.3. デフォルトVPCの存在
デフォルトVPCが存在すること。
0.4. 起動設定の決定
まず、起動設定の一覧を確認します。
aws autoscaling describe-launch-configurations \
--query 'LaunchConfigurations[].LaunchConfigurationName'
結果(例):
[
"handson-20161121"
]
利用する起動設定を指定します。
AS_LAUNCH_CONFIG_NAME='handson-20161121'
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/handson-20161121",
"InstanceMonitoring": {
"Enabled": true
},
"ClassicLinkVPCSecurityGroups": [],
"CreatedTime": "2015-08-01T01:23:45.678Z",
"BlockDeviceMappings": [],
"KeyName": "prjx-ap-northeast-1-ec2",
"SecurityGroups": [
"sg-xxxxxxxx"
],
"LaunchConfigurationName": "handson-20161121",
"KernelId": "",
"RamdiskId": "",
"ImageId": "ami-xxxxxxxx",
"InstanceType": "t2.micro",
"AssociatePublicIpAddress": true
}
]
}
0.5. VPC IDの取得
VPC_ID=$( \
aws ec2 describe-vpcs \
--filters Name=isDefault,Values=true \
--query 'Vpcs[].VpcId' \
--output text \
) \
&& echo ${VPC_ID}
結果(例):
vpc-xxxxxxxx
- 事前作業
===========
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グループの作成
============================
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}"
AS_DESIRED_CAPACITY: ${AS_DESIRED_CAPACITY}
ETX
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name ${AS_GROUP_NAME} \
--launch-configuration-name ${AS_LAUNCH_CONFIG_NAME} \
--min-size ${AS_GROUP_MIN} \
--max-size ${AS_GROUP_MAX} \
--vpc-zone-identifier "${ARRAY_VPC_SUBNET}" \
--desired-capacity ${AS_DESIRED_CAPACITY}
結果(例):
(戻り値なし)
- 事後作業
===========
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-20161121",
"HealthCheckGracePeriod": 0,
"SuspendedProcesses": [],
"DesiredCapacity": 1,
"Tags": [],
"EnabledMetrics": [],
"LoadBalancerNames": [],
"AutoScalingGroupName": "asgroup-handson-20161121",
"DefaultCooldown": 300,
"MinSize": 1,
"Instances": [
{
"InstanceId": "i-xxxxxxxx",
"AvailabilityZone": "ap-northeast-1",
"HealthStatus": "Healthy",
"LifecycleState": "InService",
"LaunchConfigurationName": "handson-20161121"
}
],
"MaxSize": 2,
"VPCZoneIdentifier": "subnet-xxxxxxxx",
"TerminationPolicies": [
"Default"
],
"LaunchConfigurationName": "handson-20161121",
"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-20161121",
"ActivityId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Details": "{"Availability Zone":"ap-northeast-1a","Subnet ID":"subnet-xxxxxxxx"}",
"StartTime": "2016-11-21T01:23:45.678Z",
"Progress": 100,
"EndTime": "2016-11-21T01:23:45Z",
"Cause": "At 2016-11-21T01:23:45Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1. At 2016-11-212T01: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
]