AWS CLI でGlobalAccelerator作成

# 公式ドキュメント

https://docs.aws.amazon.com/ja_jp/cli/latest/reference/globalaccelerator/index.html

https://docs.aws.amazon.com/ja_jp/global-accelerator/latest/dg/what-is-global-accelerator.html

# 前提条件

## AWS CLIバージョン

以下のバージョンで動作確認済
* AWS CLI 1.18.127

```ruby:コマンド
aws --version
```
```ruby:結果
aws-cli/1.18.127 Python/3.6.12 Linux/4.14.193-113.317.amzn1.x86_64 botocore/1.17.50
```

## ALB

既に作成済であること

# AWS CLI


## アクセラレータの作成


```ruby:コマンド
# GA_NAME={アクセラレータ名}
# aws globalaccelerator create-accelerator --region us-west-2 --name ${GA_NAME}

```

```ruby:結果
{
    "Accelerator": {
        "AcceleratorArn": "arn:aws:globalaccelerator::*******:accelerator/*******",
        "Name": "*******",
        "IpAddressType": "IPV4",
        "Enabled": true,
        "IpSets": [
            {
                "IpFamily": "IPv4",
                "IpAddresses": [
                    "*******",
                    "*******"
                ]
            }
        ],
        "DnsName": "*******.awsglobalaccelerator.com",
        "Status": "IN_PROGRESS",
        "CreatedTime": *******
        "LastModifiedTime": *******
    }
}
```

## リスナーの作成

本手順ではTCP80および443を指定


```ruby:コマンド
# GA_ARN=$(aws globalaccelerator list-accelerators --region us-west-2 --query Accelerators[?Name==\`${GA_NAME}\`].AcceleratorArn --output text) && echo ${GA_ARN}
# aws globalaccelerator create-listener --region us-west-2 --port-ranges FromPort=80,ToPort=80 FromPort=443,ToPort=443 --protocol TCP
```

```ruby:結果
{
    "Listener": {
        "ListenerArn": "arn:aws:globalaccelerator::*******:accelerator/*******/listener/*******",
        "PortRanges": [
            {
                "FromPort": 80,
                "ToPort": 80
            },
            {
                "FromPort": 443,
                "ToPort": 443
            }
        ],
        "Protocol": "TCP",
        "ClientAffinity": "NONE"
    }
}
```


## エンドポイントグループの作成


```ruby:コマンド
# GA_LISTENER_ARN=`aws globalaccelerator list-listeners --accelerator-arn ${GA_ARN} --region us-west-2 --query Listeners[].ListenerArn --output text` && echo ${GA_LISTENER_ARN}
# GA_ENDGROUPREGION="ap-northeast-1"
# aws globalaccelerator create-endpoint-group --region us-west-2 --endpoint-group-region ${GA_ENDGROUPREGION}
```

```ruby:結果
{
    "EndpointGroup": {
        "EndpointGroupArn": "arn:aws:globalaccelerator::*******:accelerator/*******/listener/*******/endpoint-group/*******",
        "EndpointGroupRegion": "ap-northeast-1",
        "EndpointDescriptions": [],
        "TrafficDialPercentage": 100.0,
        "HealthCheckPort": 80,
        "HealthCheckProtocol": "TCP",
        "HealthCheckIntervalSeconds": 30,
        "ThresholdCount": 3
    }
}

```

## エンドポイントグループへエンドポイント追加

作成済のALBをエンドポイントに追加

```ruby:コマンド
# GA_ENDPOINTGROUP_ARN=`aws globalaccelerator list-endpoint-groups  --listener-arn ${GA_LISTENER_ARN} --region us-west-2 --query EndpointGroups[].EndpointGroupArn --output text` && echo ${GA_ENDPOINTGROUP_ARN}
# ALB_NAME={ALB名}
# GA_ENDPOINT_ARN=$(aws elbv2 describe-load-balancers --query "LoadBalancers[?LoadBalancerName==\`${ALB_NAME}\`].LoadBalancerArn"  --output text) && echo ${GA_ENDPOINT_ARN}
# aws globalaccelerator update-endpoint-group --region us-west-2 --endpoint-group-arn ${GA_ENDPOINTGROUP_ARN} --endpoint-configurations EndpointId=${GA_ENDPOINT_ARN},Weight=128
```

```ruby:結果
{
    "EndpointGroup": {
        "EndpointGroupArn": "arn:aws:globalaccelerator::*******:accelerator/*******/listener/*******/endpoint-group/*******",
        "EndpointGroupRegion": "ap-northeast-1",
        "EndpointDescriptions": [
            {
                "EndpointId": "arn:aws:elasticloadbalancing:ap-northeast-1:*******:loadbalancer/app/{ELB名}/*******",
                "Weight": 128,
                "HealthState": "HEALTHY",
                "ClientIPPreservationEnabled": true
            }
        ],
        "TrafficDialPercentage": 100.0,
        "HealthCheckPort": 80,
        "HealthCheckProtocol": "TCP",
        "HealthCheckIntervalSeconds": 30,
        "ThresholdCount": 3
    }
}
```