https://dev.classmethod.jp/articles/route53-elb-healthcheck/
Route53のヘルスチェックではなく、ALBのヘルスチェックを使用できる
東京リージョン(ap-northeast-1)とオレゴン(us-west-2)にALBを作成
そのALBを送信先とするレイテンシールーティングを設定
前提: オレゴン(us-west-2)にkey1インポート済
-- 1. コマンド等のインストール
-- 1.1 aws cli version 2 インストール
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
-- 1.2 jqインストール
sudo yum -y install jq
-- 2. ALB作成 [ap-northeast-1での作業]
-- 2.1 EC2インスタンス作成 [ap-northeast-1での作業]
-- ap-northeast-1a
export AWS_DEFAULT_REGION=ap-northeast-1
vim a.sh
#!/bin/bash
yum -y update
yum -y install httpd
systemctl start httpd
systemctl enable httpd
echo $(hostname) > /var/www/html/index.html
aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance01}]' \
--subnet-id subnet-11111111111111111 \
--user-data file://a.sh
aws ec2 describe-instances
-- 2.2 ロードバランサーの作成 [ap-northeast-1での作業]
aws elbv2 create-load-balancer \
--name alb01 \
--subnets subnet-11111111111111111 subnet-22222222222222222 \
--security-groups sg-33333333333333333
aws elbv2 describe-load-balancers
aws elbv2 describe-load-balancers| jq -r .LoadBalancers.LoadBalancerArn
-- 2.3 ターゲットグループの作成 [ap-northeast-1での作業]
aws elbv2 create-target-group \
--name target01 \
--protocol HTTP \
--port 80 \
--vpc-id vpc-44444444444444444 \
--ip-address-type ipv4 \
--target-type instance
aws elbv2 describe-target-groups
aws elbv2 describe-target-groups| jq -r .TargetGroups.TargetGroupArn
aws elbv2 describe-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555 \
--targets Id=i-66666666666666666
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
-- 2.4 ALBリスナーの作成 [ap-northeast-1での作業]
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/7777777777777777 \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/7777777777777777
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/7777777777777777 | jq -r .Listeners.ListenerArn
-- 3. ALB作成 [us-west-2での作業]
-- 3.1 EC2インスタンス作成 [us-west-2での作業]
-- us-west-2a
export AWS_DEFAULT_REGION=us-west-2
vim a.sh
#!/bin/bash
yum -y update
yum -y install httpd
systemctl start httpd
systemctl enable httpd
echo $(hostname) > /var/www/html/index.html
aws ec2 run-instances \
--image-id ami-098e42ae54c764c35 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance01}]' \
--subnet-id subnet-88888888888888888 \
--user-data file://a.sh
aws ec2 describe-instances
-- 3.2 ロードバランサーの作成 [us-west-2での作業]
aws elbv2 create-load-balancer \
--name alb01 \
--subnets subnet-88888888888888888 subnet-00000000000000000 \
--security-groups sg-00fefcad5bf19065a
aws elbv2 describe-load-balancers
aws elbv2 describe-load-balancers| jq -r .LoadBalancers.LoadBalancerArn
-- 3.3 ターゲットグループの作成 [us-west-2での作業]
aws elbv2 create-target-group \
--name target01 \
--protocol HTTP \
--port 80 \
--vpc-id vpc-aaaaaaaaaaaaaaaaa \
--ip-address-type ipv4 \
--target-type instance
aws elbv2 describe-target-groups
aws elbv2 describe-target-groups| jq -r .TargetGroups.TargetGroupArn
aws elbv2 describe-target-group-attributes \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb \
--targets Id=i-ccccccccccccccccc
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb
-- 3.4 ALBリスナーの作成 [us-west-2での作業]
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:loadbalancer/app/alb01/dddddddddddddddd \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:loadbalancer/app/alb01/dddddddddddddddd
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:loadbalancer/app/alb01/dddddddddddddddd | jq -r .Listeners.ListenerArn
-- 4. ホステッドゾーンの確認
aws route53 list-hosted-zones
-- 5. レイテンシールーティング
※ALBのHostedZoneIdは下記で取得
aws elbv2 describe-load-balancers| jq -r .LoadBalancers[].CanonicalHostedZoneId
{
"Comment": "CREATE/DELETE/UPSERT a record ",
"Changes": [
{ "Action": "UPSERT",
"ResourceRecordSet": { "Name": "www.example.com.",
"Type": "A",
"SetIdentifier": "rec1",
"Region": "ap-northeast-1",
"AliasTarget": {
"HostedZoneId": "AAAAAAAAAAAAAA",
"DNSName": "dualstack.alb01-111111111.ap-northeast-1.elb.amazonaws.com.",
"EvaluateTargetHealth": true
}
}
},
{ "Action": "UPSERT",
"ResourceRecordSet": { "Name": "www.example.com.",
"Type": "A",
"SetIdentifier": "rec2",
"Region": "us-west-2",
"AliasTarget": {
"HostedZoneId": "BBBBBBBBBBBBB",
"DNSName": "dualstack.alb01-2222222222.us-west-2.elb.amazonaws.com.",
"EvaluateTargetHealth": true
}
}
}
]
}
aws route53 change-resource-record-sets \
--hosted-zone-id XXXXXXXXXXXXXXXXXXXXX \
--change-batch file://b.json
aws route53 list-resource-record-sets \
--hosted-zone-id XXXXXXXXXXXXXXXXXXXXX
-- 6. 動作確認
curl http://www.example.com/index.html
東京リージョンのEC2を停止してオレゴンリージョンにフェイルオーバを確認
フェイルオーバするまで、503 Service Temporarily Unavailableとなる
-- 7. クリーンアップ
-- Route53レコードの削除
export AWS_DEFAULT_REGION=us-west-2
-- ALBリスナーの削除 [us-west-2での作業]
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:loadbalancer/app/alb01/dddddddddddddddd
aws elbv2 delete-listener \
--listener-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:listener/app/alb01/dddddddddddddddd/iiiiiiiiiiiiiiii
-- ターゲットグループの削除 [us-west-2での作業]
aws elbv2 describe-target-groups
aws elbv2 deregister-targets \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb \
--targets Id=i-ccccccccccccccccc
aws elbv2 delete-target-group \
--target-group-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:targetgroup/target01/bbbbbbbbbbbbbbbb
-- ロードバランサーの削除 [us-west-2での作業]
aws elbv2 describe-load-balancers
aws elbv2 delete-load-balancer \
--load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:999999999999:loadbalancer/app/alb01/dddddddddddddddd
-- EC2インスタンスの削除 [us-west-2での作業]
aws ec2 describe-instances
aws ec2 terminate-instances --instance-ids i-ccccccccccccccccc
export AWS_DEFAULT_REGION=ap-northeast-1
-- ALBリスナーの削除 [ap-northeast-1での作業]
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/7777777777777777
aws elbv2 delete-listener \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:listener/app/alb01/7777777777777777/jjjjjjjjjjjjjjjj
-- ターゲットグループの削除 [ap-northeast-1での作業]
aws elbv2 describe-target-groups
aws elbv2 deregister-targets \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555 \
--targets Id=i-66666666666666666
aws elbv2 delete-target-group \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
-- ロードバランサーの削除 [ap-northeast-1での作業]
aws elbv2 describe-load-balancers
aws elbv2 delete-load-balancer \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/7777777777777777
-- EC2インスタンスの削除 [ap-northeast-1での作業]
aws ec2 describe-instances
aws ec2 terminate-instances --instance-ids i-66666666666666666