5.1. ELBv2ロードバランサの作成 (handson-cli-webapp-stateless-alb)

作業の目的 [why]

ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"を作成します。

完了条件/事前条件 [設計者用情報]

完了条件 [after]

主処理は、以下を満たしたときに成功したものとします。

完了条件1
ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在する。

事前条件 [before]

主処理の実施は、以下の状態であることを前提とします。

事前条件1
ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在しない。
事前条件2
VPC"handson-cli-vpc"が存在する。
事前条件3
VPC"handson-cli-vpc"にサブネット"handson-cli-app-alpha-subnet"が存在する。
事前条件4
VPC"handson-cli-vpc"にサブネット"handson-cli-app-charlie-subnet"が存在する。
事前条件5
VPC"handson-cli-vpc"にセキュリティグループ"handson-cli-alb-sg"が存在する。

作業対象 [what]

  • ELBv2サービス

標準時間(合計)

8分

パラメータ設定

パラメータ設定の標準時間:2分

作業に必要なモノ・情報 [resource]

作業開始には、以下が全て揃っていることが必要です。

リソース1: ELBv2ロードバランサ名

  • 作成するELBv2ロードバランサの名称です。
  • 今回は"handson-cli-webapp-stateless-alb"とします。

リソース2: VPCのタグ名

  • 作成するELBv2ロードバランサが利用するサブネット/セキュリティが属するVPCのタグ名称です。
  • 今回は"handson-cli-vpc"とします。

リソース3: サブネット1のタグ名

  • 作成するELBv2ロードバランサが利用するサブネット1のタグ名称です。
  • 今回は"handson-cli-app-alpha-subnet"とします。

リソース4: サブネット2のタグ名

  • 作成するELBv2ロードバランサが利用するサブネット2のタグ名称です。
  • 今回は"handson-cli-app-charlie-subnet"とします。

リソース5: セキュリティグループ名

  • 作成するELBv2ロードバランサが利用するセキュリティグループの名称です。
  • 今回は"handson-cli-alb-sg"とします。

パラメータの指定

作業に必要なパラメータを変数に格納をします。

0.0. リージョンの指定

変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

0.1. ELBv2ロードバランサ名の指定

ELBv2ロードバランサ名を指定します。

変数の設定:

ELBV2_LB_NAME='handson-cli-webapp-stateless-alb'

0.2. VPCのタグ名

ELBv2ロードバランサが利用するサブネット/セキュリティが属するVPCのタグ名を指定します。

変数の設定:

VPC_TAG_NAME="handson-cli-vpc"

0.3. サブネット1のタグ名

ELBv2ロードバランサが利用するサブネット1のタグ名を指定します。

変数の設定:

VPC_SUBNET_TAG_NAME_1="handson-cli-app-alpha-subnet"

0.4. サブネット2のタグ名

ELBv2ロードバランサが利用するサブネット2のタグ名を指定します。

変数の設定:

VPC_SUBNET_TAG_NAME_2="handson-cli-app-charlie-subnet"

0.5. セキュリティグループ名

ELBv2ロードバランサが利用するセキュリティグループの名を指定します。

変数の設定:

VPC_SG_NAME="handson-cli-alb-sg"

パラメータの保存

設定されている変数の保存先となるファイル名を指定します。

変数の設定:

DIR_PARAMETER="${HOME}/tmp/parameter-handson-cli-webapp-stateless"
FILE_PARAMETER="${DIR_PARAMETER}/$(date +%Y-%m-%d)-elbv2-load_balancer-create.env" \
  && echo ${FILE_PARAMETER}

結果(例):

${HOME}/tmp/parameter-handson-cli-webapp-stateless/2019-05-19-elbv2-load_balancer-create.env

各変数に正しいパラメータ値が格納されていることを確認しながら保存します。

変数の確認:

cat << EOF > ${FILE_PARAMETER}

  # 0.0. AWS_DEFAULT_REGION:"ap-northeast-1"
         AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 0.1. ELBV2_LB_NAME:"handson-cli-webapp-stateless-alb"
         ELBV2_LB_NAME="${ELBV2_LB_NAME}"
  # 0.2. VPC_TAG_NAME:"handson-cli-vpc"
         VPC_TAG_NAME="${VPC_TAG_NAME}"
  # 0.3. VPC_SUBNET_TAG_NAME_1:"handson-cli-app-alpha-subnet"
         VPC_SUBNET_TAG_NAME_1="${VPC_SUBNET_TAG_NAME_1}"
  # 0.4. VPC_SUBNET_TAG_NAME_2:"handson-cli-app-charlie-subnet"
         VPC_SUBNET_TAG_NAME_2="${VPC_SUBNET_TAG_NAME_2}"
  # 0.5. VPC_SG_NAME:"handson-cli-alb-sg"
         VPC_SG_NAME="${VPC_SG_NAME}"

EOF

cat ${FILE_PARAMETER}

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

タスクの実施

タスク標準時間:6分

1. 前処理

1.1. 処理対象の状態確認

主処理の実施は、以下の状態であることを前提とします。

前提と異なることが判明した場合、直ちに処理を中止します。

事前条件1: ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在しない。

「ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在しない。」ことを確認します。

コマンド:

! aws elbv2 describe-load-balancers \
  --name ${ELBV2_LB_NAME} \
  --query 'LoadBalancers[].LoadBalancerName' \
  --output text

結果(例):

An error occurred (LoadBalancerNotFound) when calling the DescribeLoadBalancers operation: Load balancers '[handson-cli-webapp-stateless-alb]' not found

事前条件2: VPC"handson-cli-vpc"が存在する。

「VPC"handson-cli-vpc"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=${VPC_TAG_NAME}  \
  --query 'Vpcs[].Tags[?Key == `Name`].Value' \
  --output text

結果(例):

handson-cli-vpc

VPC IDを取得します。

コマンド:

VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${VPC_ID}

結果(例):

vpc-xxxxxxxxxxxxxxxxx

事前条件3: VPC"handson-cli-vpc"にサブネット"handson-cli-app-alpha-subnet"が存在する。

「VPC"handson-cli-vpc"にサブネット"handson-cli-app-alpha-subnet"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-subnets \
  --filters Name=vpc-id,Values=${VPC_ID} \
            Name=tag:Name,Values=${VPC_SUBNET_TAG_NAME_1}  \
  --query 'Subnets[].Tags[?Key == `Name`].Value' \
  --output text

結果(例):

handson-cli-app-alpha-subnet

事前条件4: VPC"handson-cli-vpc"にサブネット"handson-cli-app-charlie-subnet"が存在する。

「VPC"handson-cli-vpc"にサブネット"handson-cli-app-charlie-subnet"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-subnets \
  --filters Name=vpc-id,Values=${VPC_ID} \
            Name=tag:Name,Values=${VPC_SUBNET_TAG_NAME_2}  \
  --query 'Subnets[].Tags[?Key == `Name`].Value' \
  --output text

結果(例):

handson-cli-app-charlie-subnet

事前条件5: VPC"handson-cli-vpc"にセキュリティグループ"handson-cli-alb-sg"が存在する。

「VPC"handson-cli-vpc"にセキュリティグループ"handson-cli-alb-sg"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-security-groups \
  --filter Name=vpc-id,Values=${VPC_ID} \
    Name=group-name,Values=${VPC_SG_NAME} \
  --query 'SecurityGroups[].GroupName' \
  --output text

結果(例):

handson-cli-alb-sg

1.2. 主処理に必要な情報の取得

サブネット1のサブネットID取得

サブネット1のサブネットIDを取得します。

コマンド:

VPC_SUBNET_ID_1=$( \
  aws ec2 describe-subnets \
    --filters Name=vpc-id,Values=${VPC_ID} \
    --filters Name=tag:Name,Values=${VPC_SUBNET_TAG_NAME_1}  \
    --query "Subnets[].SubnetId" \
    --output text \
) \
  && echo ${VPC_SUBNET_ID_1}

結果(例):

subnet-xxxxxxxxxxxxxxxxx

サブネット2のサブネットID取得

サブネット2のサブネットIDを取得します。

コマンド:

VPC_SUBNET_ID_2=$( \
  aws ec2 describe-subnets \
    --filters Name=vpc-id,Values=${VPC_ID} \
    --filters Name=tag:Name,Values=${VPC_SUBNET_TAG_NAME_2}  \
    --query "Subnets[].SubnetId" \
    --output text \
) \
  && echo ${VPC_SUBNET_ID_2}

結果(例):

subnet-xxxxxxxxxxxxxxxxx

サブネットIDの配列への組み込み

変数の設定:

ARRAY_VPC_SUBNET_IDS="${VPC_SUBNET_ID_1} ${VPC_SUBNET_ID_2}" \
  && echo ${ARRAY_VPC_SUBNET_IDS}

結果(例):

subnet-xxxxxxxxxxxxxxxxx subnet-xxxxxxxxxxxxxxxxx

セキュリティグループIDを取得します。

セキュリティグループIDを取得します。

コマンド:

VPC_SG_ID=$( \
  aws ec2 describe-security-groups \
    --filter Name=vpc-id,Values=${VPC_ID} \
      Name=group-name,Values=${VPC_SG_NAME} \
    --query "SecurityGroups[].GroupId" \
    --output text \
) \
  && echo ${VPC_SG_ID}

結果(例):

sg-xxxxxxxxxxxxxxxxx

セキュリティグループIDの配列への組み込み

変数の設定:

ARRAY_VPC_SG_IDS="${VPC_SG_ID}" \
  && echo ${ARRAY_VPC_SG_IDS}

結果(例):

sg-xxxxxxxxxxxxxxxxx

2. 主処理

ALBロードバランサの作成

変数の確認:

cat << ETX

  # ELBV2_LB_NAME:"handson-cli-webapp-stateless-alb"
    ELBV2_LB_NAME="${ELBV2_LB_NAME}"
  # ARRAY_VPC_SUBNET_IDS:"subnet-xxxxxxxxxxxxxxxxx subnet-xxxxxxxxxxxxxxxxx"
    ARRAY_VPC_SUBNET_IDS="${ARRAY_VPC_SUBNET_IDS}"
  # ARRAY_VPC_SG_IDS:"sg-xxxxxxxxxxxxxxxxx"
    ARRAY_VPC_SG_IDS="${ARRAY_VPC_SG_IDS}"

ETX

コマンド:

aws elbv2 create-load-balancer \
  --name ${ELBV2_LB_NAME} \
  --subnets ${ARRAY_VPC_SUBNET_IDS} \
  --security-groups ${ARRAY_VPC_SG_IDS}

結果(例):

{
  "LoadBalancers": [
    {
        "IpAddressType": "ipv4",
        "VpcId": "vpc-xxxxxxxxxxxxxxxxx",
        "LoadBalancerArn": "arn:aws:elasticloadbalancing:ap-northeast-1:XXXXXXXXXXXX:loadbalancer/app/handson-cli-webapp-stateless-alb/xxxxxxxxxxxxxxxx",
        "State": {
            "Code": "provisioning"
        },
        "DNSName": "handson-cli-webapp-stateless-alb-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com",
        "SecurityGroups": [
            "sg-xxxxxxxxxxxxxxxxx"
        ],
        "LoadBalancerName": "handson-cli-webapp-stateless-alb",
        "CreatedTime": "2019-05-19T01:23:45.678Z",
        "Scheme": "internet-facing",
        "Type": "application",
        "CanonicalHostedZoneId": "Z14GRHDCWA56QT",
        "AvailabilityZones": [
            {
                "SubnetId": "subnet-xxxxxxxxxxxxxxxxx",
                "ZoneName": "<サブネット1のAZ名>"
            },
            {
                "SubnetId": "subnet-xxxxxxxxxxxxxxxxx",
                "ZoneName": "<サブネット2のAZ名>"
            }
        ]
    }
  ]
}

3. 後処理

完了条件の確認

主処理は、以下を満たしたときに成功したものとします。

完了条件1: ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在する。

「ELBv2ロードバランサ"handson-cli-webapp-stateless-alb"が存在する。」ことを確認します。

コマンド:

aws elbv2 describe-load-balancers \
  --name ${ELBV2_LB_NAME} \
  --query 'LoadBalancers[].LoadBalancerName' \
  --output text

結果(例):

handson-cli-webapp-stateless-alb

完了