https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/http-api-private-integration.html
HTTP API -> VPC link -> ALB -> EC2
-- 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. EC2インスタンス作成
各アベイラビリティーゾーンで少なくとも 1 つの EC2 インスタンスを起動します
-- ap-northeast-1a
aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance01}]' \
--instance-market-options '{"MarketType": "spot","SpotOptions": {"SpotInstanceType": "one-time"}}' \
--subnet-id subnet-11111111111111111
-- ap-northeast-1c
aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance02}]' \
--instance-market-options '{"MarketType": "spot","SpotOptions": {"SpotInstanceType": "one-time"}}' \
--subnet-id subnet-22222222222222222
aws ec2 describe-instances
-- 3. Apacheインストール
Apacheウェブサーバーを各 EC2 インスタンスにインストール
sudo yum -y update
sudo yum -y install httpd
sudo systemctl start httpd
sudo systemctl status httpd
sudo systemctl enable httpd
sudo su -
cd /var/www/html
echo $(hostname) > index.html
sudo yum -y install elinks
elinks http://localhost/
-- 4. ロードバランサーの作成
※internal-ALBを作成
aws elbv2 create-load-balancer \
--name alb01 \
--subnets subnet-11111111111111111 subnet-22222222222222222 \
--security-groups sg-33333333333333333 \
--scheme internal
aws elbv2 describe-load-balancers
aws elbv2 describe-load-balancers| jq -r .LoadBalancers.LoadBalancerArn
-- 5. ターゲットグループの作成
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 Id=i-77777777777777777
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
-- 6. リスナーの作成
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/8888888888888888 \
--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/8888888888888888
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/8888888888888888 | jq -r .Listeners.ListenerArn
-- 7. VPC リンクを作成する
aws apigatewayv2 create-vpc-link \
--name vpclink01 \
--subnet-ids subnet-11111111111111111 subnet-22222222222222222 \
--security-group-ids sg-33333333333333333
aws apigatewayv2 get-vpc-links
aws apigatewayv2 get-vpc-links| jq -r .Items.VpcLinkId
aws apigatewayv2 get-vpc-link \
--vpc-link-id xxxxxx
-- 8. HTTP API を作成する
aws apigatewayv2 create-api \
--name api01 \
--protocol-type HTTP
aws apigatewayv2 get-apis
aws apigatewayv2 get-apis| jq -r .Items.ApiId
aws apigatewayv2 get-api \
--api-id yyyyyyyyyy
-- 9. 統合を作成する
aws apigatewayv2 get-integrations \
--api-id yyyyyyyyyy
aws apigatewayv2 create-integration \
--api-id yyyyyyyyyy \
--connection-id xxxxxx \
--connection-type VPC_LINK \
--integration-method ANY \
--integration-type HTTP_PROXY \
--integration-uri arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:listener/app/alb01/8888888888888888/0000000000000000 \
--payload-format-version 1.0 \
--timeout-in-millis 30000
aws apigatewayv2 get-integrations \
--api-id yyyyyyyyyy | jq -r .Items.IntegrationId
-- 10. ルートを作成する
aws apigatewayv2 create-route \
--api-id yyyyyyyyyy \
--route-key 'ANY /{proxy+}' \
--target integrations/zzzzzzz
aws apigatewayv2 get-routes \
--api-id yyyyyyyyyy
-- 11. ステージのデプロイ
aws apigatewayv2 get-deployments \
--api-id yyyyyyyyyy
aws apigatewayv2 get-stages \
--api-id yyyyyyyyyy
aws apigatewayv2 create-deployment \
--api-id yyyyyyyyyy
aws apigatewayv2 create-stage \
--api-id yyyyyyyyyy \
--auto-deploy \
--deployment-id aaaaaa \
--stage-name '$default'
-- 12. API をテストする
curl -v internal-alb01-0000000000.ap-northeast-1.elb.amazonaws.com
https://yyyyyyyyyy.execute-api.ap-northeast-1.amazonaws.com/
-- 13. クリーンアップ
-- HTTP APIの削除
aws apigatewayv2 get-apis
aws apigatewayv2 delete-api \
--api-id yyyyyyyyyy
-- VPC リンクの削除
aws apigatewayv2 get-vpc-links
aws apigatewayv2 delete-vpc-link \
--vpc-link-id xxxxxx
-- リスナーの削除
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/8888888888888888
aws elbv2 delete-listener \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:listener/app/alb01/8888888888888888/0000000000000000
-- ターゲットグループの削除
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 Id=i-77777777777777777
aws elbv2 delete-target-group \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/5555555555555555
-- ロードバランサーの削除
aws elbv2 describe-load-balancers
aws elbv2 delete-load-balancer \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/8888888888888888
-- EC2インスタンスの削除
aws ec2 describe-instances
aws ec2 terminate-instances --instance-ids i-66666666666666666
aws ec2 terminate-instances --instance-ids i-77777777777777777