-- 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. IAMロール作成 [アカウントBで実施]
aws sts get-caller-identity
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json
-- 3. ポリシーをロールにアタッチ [アカウントBで実施]
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole \
--role-name role01
-- 4. Lambda関数作成 [アカウントBで実施]
vim test.js
'use strict';
exports.handler = async (event) => {
let response = {
statusCode: 200,
body: JSON.stringify("hello world!")
};
console.log("response: " + JSON.stringify(response))
return response;
};
chmod 755 test.js
zip test.zip test.js
aws lambda create-function \
--function-name func01 \
--zip-file fileb://test.zip \
--handler test.handler \
--runtime nodejs14.x \
--role arn:aws:iam::888888888888:role/role01
aws lambda list-functions | grep func01
aws lambda get-function --function-name func01
-- 5. API を作成する
aws sts get-caller-identity
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 1111111111
-- 6. API でリソースを作成する
aws apigateway get-resources \
--rest-api-id 1111111111
aws apigateway create-resource \
--rest-api-id 1111111111 \
--parent-id 2222222222 \
--path-part "resource01"
-- 7. リソースにメソッドを作成する
aws apigateway put-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--authorization-type NONE \
--no-api-key-required \
--request-parameters {}
aws apigateway put-integration \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--type AWS_PROXY \
--integration-http-method POST \
--content-handling CONVERT_TO_TEXT \
--uri "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:888888888888:function:func01/invocations"
aws apigateway put-integration-response \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--status-code 200 \
--response-templates '{"application/json": ""}'
aws apigateway put-method-response \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--status-code 200 \
--response-models '{"application/json": "Empty"}'
aws apigateway get-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET
aws apigateway get-integration \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET
aws apigateway get-integration-response \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--status-code 200
aws apigateway get-method-response \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--status-code 200
-- 8. Lambda関数に権限を追加する [アカウントBで実施]
aws lambda add-permission \
--function-name func01 \
--statement-id apigw \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn arn:aws:execute-api:ap-northeast-1:999999999999:1111111111/*/*/resource01
aws lambda get-policy \
--function-name func01 | jq -r .Policy | jq .
-- 9. デプロイ前にAPI をテストする
aws apigateway test-invoke-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--path-with-query-string ''
-- 10. API をデプロイする
aws apigateway get-deployments \
--rest-api-id 1111111111
aws apigateway get-stages \
--rest-api-id 1111111111
aws apigateway create-deployment \
--rest-api-id 1111111111
aws apigateway create-stage \
--rest-api-id 1111111111 \
--stage-name stage01 \
--deployment-id 444444 \
-- 11. デプロイ後にAPI をテストする
https://1111111111.execute-api.ap-northeast-1.amazonaws.com/stage01/resource01/
-- 12. クリーンアップ
-- API削除
aws apigateway get-rest-apis
aws apigateway delete-rest-api \
--rest-api-id 1111111111
-- Lambda関数の削除 [アカウントBで実施]
aws lambda get-function --function-name func01
aws lambda delete-function --function-name func01
-- ロールの削除 [アカウントBで実施]
aws iam list-roles | grep role01
aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name role01