AWS

AWS CLI から ELB ~ Auto Scaling グループの作成・設定を行う

AWS CLI から ELB ~ Auto Scaling グループの作成を行ってみた時のメモ

ELB の作成

以下のコマンドを実行する
(aws cli の region が ap-northeast-1 に設定された状態で試しました)

ELBの作成

始めにELBの作成を行う
ヘルスチェックなどいくつかの設定は別のコマンドで設定しなければいけないよう

aws elb create-load-balancer \
        --load-balancer-name sample-elb \
        --listeners 'Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80' \
        --subnets 'subnet-123456' 'subnet-456789' \
        --security-groups 'sg-123456' \
        --scheme internal

今回は内部向けロードバランサー
http://docs.aws.amazon.com/ja_jp/elasticloadbalancing/latest/classic/elb-internal-load-balancers.html
を作成しようと思うので
--scheme internal
をつけているが、内部向けでなければこのオプションを外す

クロスゾーン負荷分散・接続のストリーミングの有効化

今回はクロスゾーン負荷分散と接続のストリーミングを有効化したいので以下のコマンドを追加で実行する

aws elb modify-load-balancer-attributes \
  --load-balancer-name sample-elb \
  --load-balancer-attributes '{
    "CrossZoneLoadBalancing":{
        "Enabled":true
    },
    "ConnectionDraining":{
        "Enabled":true,
        "Timeout":300
    }
  }'

ヘルスチェック設定を追加

更にヘルスチェックの設定を行うために以下のコマンドも実行する

aws elb configure-health-check \
  --load-balancer-name sample-elb \
  --health-check Target=HTTP:80/target/ping/path,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3

上記で設定は完了

値の確認

正しく作成・設定されたか確認する
(値は適当な値に書き換えてます)

 aws elb describe-load-balancers --load-balancer-name sample-elb
{
    "LoadBalancerDescriptions": [
        {
            "Subnets": [
                "subnet-123456",
                "subnet-456789"
            ],
            "CanonicalHostedZoneNameID": "CanonicalHostedZoneNameID",
            "VPCId": "vpc-123456",
            "ListenerDescriptions": [
                {
                    "Listener": {
                        "InstancePort": 80,
                        "LoadBalancerPort": 80,
                        "Protocol": "HTTP",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": []
                }
            ],
            "HealthCheck": {
                "HealthyThreshold": 2,
                "Interval": 30,
                "Target": "HTTP:80/target/ping/path",
                "Timeout": 3,
                "UnhealthyThreshold": 2
            },
            "BackendServerDescriptions": [],
            "Instances": [],
            "DNSName": "internal-sample-elb-123456.ap-northeast-1.elb.amazonaws.com",
            "SecurityGroups": [
                "sg-123456"
            ],
            "Policies": {
                "LBCookieStickinessPolicies": [],
                "AppCookieStickinessPolicies": [],
                "OtherPolicies": []
            },
            "LoadBalancerName": "sample-elb",
            "CreatedTime": "2017-08-27T04:54:54.290Z",
            "AvailabilityZones": [
                "ap-northeast-1b",
                "ap-northeast-1c"
            ],
            "Scheme": "internal",
            "SourceSecurityGroup": {
                "OwnerAlias": "123456",
                "GroupName": "default"
            }
        }
    ]
}

起動設定 ~ Auto Scaling グループを作成

起動設定を作成

以下のコマンドで起動設定を作成

試した時の状況としてオプションとして行ってることとして

  • 詳細モニタリングをOFFにしてる
  • インスタンスプロファイルを設定してる
  • ブロックデバイスを手動で設定してる

あたりが入ってる

aws autoscaling create-launch-configuration \
  --launch-configuration-name launch-configuration-for-sample-elb \
  --image-id ami-123456 \
  --instance-type m4.large \
  --security-groups sg-123456 \
  --instance-monitoring Enabled=false \
  --iam-instance-profile sample-instance-profile \
  --key-name keyname \
  --block-device-mappings '[
    {
        "DeviceName": "/dev/sdx",
        "Ebs": {
          "VolumeSize": 100,
          "VolumeType": "gp2",
          "DeleteOnTermination": true
        }
      },
      {
        "DeviceName": "/dev/xvda",
        "Ebs": {
          "VolumeSize": 8,
          "VolumeType": "gp2",
          "DeleteOnTermination": true
        }
      }
    ]'

Auto Scaling グループを作成

上記で作成した ELB と 起動設定を利用して Auto Scaling グループを作成

注意点として一旦台数固定の Scaling Policy は無しで作成してる

aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name auto-scaling-group-for-sample-elb \
    --launch-configuration-name launch-configuration-for-sample-elb \
    --min-size 2 \
    --max-size 2 \
    --desired-capacity 2 \
    --load-balancer-names sample-elb \
    --health-check-type EC2 \
    --health-check-grace-period 300 \
    --availability-zones ap-northeast-1b ap-northeast-1c \
    --vpc-zone-identifier subnet-123456,subnet-456789 \
    --tags '[
      {
        "PropagateAtLaunch": true,
        "Key": "Name",
        "Value": "sample-elb-instance"
      }
    ]'

Auto Scaling グループの通知設定を作成

先程作成した Auto Scaling グループに通知設定を追加

aws autoscaling put-notification-configuration \
  --auto-scaling-group-name auto-scaling-group-for-sample-elb \
  --topic-arn arn:aws:sns:ap-northeast-1:XXXXXX:XXXXXX \
  --notification-type autoscaling:EC2_INSTANCE_LAUNCH autoscaling:EC2_INSTANCE_LAUNCH_ERROR autoscaling:EC2_INSTANCE_TERMINATE autoscaling:EC2_INSTANCE_TERMINATE_ERROR

値の確認

正しく作成・設定されたか確認する
(値は適当な値に書き換えてます)

$ aws autoscaling describe-launch-configurations --launch-configuration-names launch-configuration-for-sample-elb
{
    "LaunchConfigurations": [
        {
            "UserData": "",
            "IamInstanceProfile": "intent-recommend-search",
            "EbsOptimized": false,
            "LaunchConfigurationARN": "arn:aws:autoscaling:ap-northeast-1:XXXXXX:launchConfiguration:XXXXXX:launchConfigurationName/launch-configuration-for-sample-elb",
            "InstanceMonitoring": {
                "Enabled": false
            },
            "ClassicLinkVPCSecurityGroups": [],
            "CreatedTime": "2017-08-27T06:31:16.558Z",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "VolumeSize": 8,
                        "VolumeType": "gp2"
                    }
                },
                {
                    "DeviceName": "/dev/sdx",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "VolumeSize": 100,
                        "VolumeType": "gp2"
                    }
                }
            ],
            "KeyName": "keyname",
            "SecurityGroups": [
                "sg-123456"
            ],
            "LaunchConfigurationName": "launch-configuration-for-sample-elb",
            "KernelId": "",
            "RamdiskId": "",
            "ImageId": "ami-123456",
            "InstanceType": "m4.large"
        }
    ]
}


$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name auto-scaling-group-for-sample-elb
{
    "AutoScalingGroups": [
        {
            "AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:XXXXXXX:autoScalingGroup:XXXXXX:autoScalingGroupName/auto-scaling-group-for-sample-elb",
            "TargetGroupARNs": [],
            "SuspendedProcesses": [],
            "DesiredCapacity": 2,
            "Tags": [
                {
                    "ResourceType": "auto-scaling-group",
                    "ResourceId": "auto-scaling-group-for-sample-elb",
                    "PropagateAtLaunch": true,
                    "Value": "sample-elb-instance",
                    "Key": "Name"
                }
            ],
            "EnabledMetrics": [],
            "LoadBalancerNames": [
                "intent-recommend-search-ja-JP"
            ],
            "AutoScalingGroupName": "auto-scaling-group-for-sample-elb",
            "DefaultCooldown": 300,
            "MinSize": 2,
            "Instances": [
                ...
            ],
            "MaxSize": 2,
            "VPCZoneIdentifier": "subnet-123456,subnet-456789",
            "HealthCheckGracePeriod": 300,
            "TerminationPolicies": [
                "Default"
            ],
            "LaunchConfigurationName": "launch-configuration-for-sample-elb",
            "CreatedTime": "2017-08-27T06:31:16.558Z",
            "AvailabilityZones": [
                "ap-northeast-1b",
                "ap-northeast-1c"
            ],
            "HealthCheckType": "EC2",
            "NewInstancesProtectedFromScaleIn": false
        }
    ]
}


$ autoscaling describe-notification-configurations --auto-scaling-group-name auto-scaling-group-for-sample-elb
{
    "NotificationConfigurations": [
        {
            "AutoScalingGroupName": "auto-scaling-group-for-sample-elb",
            "NotificationType": "autoscaling:EC2_INSTANCE_LAUNCH",
            "TopicARN": "arn:aws:sns:ap-northeast-1:XXXXXX:XXXXXX"
        },
        {
            "AutoScalingGroupName": "auto-scaling-group-for-sample-elb",
            "NotificationType": "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
            "TopicARN": "arn:aws:sns:ap-northeast-1:XXXXXX:XXXXXX"
        },
        {
            "AutoScalingGroupName": "auto-scaling-group-for-sample-elb",
            "NotificationType": "autoscaling:EC2_INSTANCE_TERMINATE",
            "TopicARN": "arn:aws:sns:ap-northeast-1:XXXXXX:XXXXXX"
        },
        {
            "AutoScalingGroupName": "auto-scaling-group-for-sample-elb",
            "NotificationType": "autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
            "TopicARN": "arn:aws:sns:ap-northeast-1:XXXXXX:XXXXXX"
        }
    ]
}

参考: