REST API -> VPC link -> NLB -> 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-NLBを作成
aws elbv2 create-load-balancer \
--name nlb01 \
--subnets subnet-11111111111111111 subnet-22222222222222222 \
--scheme internal \
--type network
aws elbv2 describe-load-balancers
aws elbv2 describe-load-balancers| jq -r .LoadBalancers.LoadBalancerArn
-- 5. ターゲットグループの作成
aws elbv2 create-target-group \
--name target01 \
--protocol TCP \
--port 80 \
--vpc-id vpc-33333333333333333 \
--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/4444444444444444
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/4444444444444444 \
--targets Id=i-55555555555555555 Id=i-66666666666666666
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/4444444444444444
-- 6. リスナーの作成
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777 \
--protocol TCP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/4444444444444444
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777 | jq -r .Listeners.ListenerArn
curl http://nlb01-7777777777777777.elb.ap-northeast-1.amazonaws.com
-- 7. VPC リンクを作成する
aws apigateway create-vpc-link \
--name vpclink01 \
--target-arns arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777
aws apigateway get-vpc-links
aws apigateway get-vpc-links| jq -r .Items.VpcLinkId
aws apigateway get-vpc-link \
--vpc-link-id 888888
-- 8. API を作成する
aws apigateway create-rest-api \
--name api01 \
--description "api01" \
--endpoint-configuration '{"types": ["REGIONAL"]}'
aws apigateway get-rest-apis
aws apigateway get-rest-api \
--rest-api-id aaaaaaaaaa
-- 9. リソース確認
aws apigateway get-resources \
--rest-api-id aaaaaaaaaa
-- 10. リソースにメソッドを作成する
aws apigateway put-method \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET \
--authorization-type NONE \
--no-api-key-required
aws apigateway put-method-response \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET \
--status-code 200 \
--response-models '{"application/json": "Empty"}'
aws apigateway put-integration \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET \
--type HTTP_PROXY \
--uri "http://nlb01-7777777777777777.elb.ap-northeast-1.amazonaws.com" \
--connection-type "VPC_LINK" \
--connection-id 888888 \
--passthrough-behavior "WHEN_NO_MATCH" \
--cache-namespace "bbbbbbbbbb" \
--timeout-in-millis 29000 \
--integration-http-method GET
aws apigateway put-integration-response \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET \
--status-code 200 \
--response-templates '{}'
aws apigateway get-method \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET
-- 11. デプロイ前にAPI をテストする
aws apigateway test-invoke-method \
--rest-api-id aaaaaaaaaa \
--resource-id bbbbbbbbbb \
--http-method GET
-- 12. API をデプロイする
aws apigateway get-deployments \
--rest-api-id aaaaaaaaaa
aws apigateway get-stages \
--rest-api-id aaaaaaaaaa
aws apigateway create-deployment \
--rest-api-id aaaaaaaaaa
aws apigateway create-stage \
--rest-api-id aaaaaaaaaa \
--stage-name stage01 \
--deployment-id dddddd \
--variables '{
"vpcLinkId": "888888"
}'
-- 13. デプロイ後にAPI をテストする
curl -v -X GET https://aaaaaaaaaa.execute-api.ap-northeast-1.amazonaws.com/stage01
-- 14. クリーンアップ
-- API削除
aws apigateway get-rest-apis
aws apigateway delete-rest-api \
--rest-api-id aaaaaaaaaa
-- VPC リンクの削除
aws apigateway delete-vpc-link \
--vpc-link-id 888888
-- リスナーの削除
aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777
aws elbv2 delete-listener \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:listener/net/nlb01/7777777777777777/cccccccccccccccc
-- ターゲットグループの削除
aws elbv2 describe-target-groups
aws elbv2 deregister-targets \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/4444444444444444 \
--targets Id=i-55555555555555555 Id=i-66666666666666666
aws elbv2 delete-target-group \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:targetgroup/target01/4444444444444444
-- ロードバランサーの削除
aws elbv2 describe-load-balancers
aws elbv2 delete-load-balancer \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/net/nlb01/7777777777777777
-- EC2インスタンスの削除
aws ec2 describe-instances
aws ec2 terminate-instances --instance-ids i-55555555555555555
aws ec2 terminate-instances --instance-ids i-66666666666666666