作りたいもの
要約
なんか簡単な処理をAPIを経由してアクセスしたい
↓
lambdaでなんかできそう
↓
postmanで再現するにはホストがいる
↓
API Gatewayでいけそう⇦今ここ
↓
多分、APIGateway+Lambdaの連携でできるかも
lambdaは色々なAWSサービスのトリガーで動作することができる
やること
1. APIGatewayを作成
↓
↓
↓
2. APIのリソースを作成
↓
↓
3. APIのメソッドを作成
↓ ↓
4. lambdaを作成
↓
↓
↓
添付コード
exports.handler = async (event, context, callback) => { console.log("EVENT: \n" + JSON.stringify(event, null, 2)) // リクエスト console.log("body: ", event.body); // レスポンス const response = { statusCode: 200, headers: { "Content-Type": "application/json" }, body: JSON.stringify({ "aaa": 111, "bbb": "aaa", }), }; callback(null, response); };
↓
5. APIとLambdaを紐付けデプロイ
↓
↓
↓
↓
↓
6. curlで確認
% curl https://5ftyywmhe6.execute-api.ap-northeast-1.amazonaws.com/dev/test1 {"aaa":111,"bbb":"aaa"}% %
できた ^_^
おまけ
lambdaからみたAPI Gatewayからのeventの中身
{ "resource": "/test1", "path": "/test1", "httpMethod": "GET", "headers": { "Accept": "*/*", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "JP", "Host": "一応隠す", "User-Agent": "curl/7.64.1", "Via": "一応隠す", "X-Amz-Cf-Id": "一応隠す", "X-Amzn-Trace-Id": "一応隠す", "X-Forwarded-For": "一応隠す", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, "multiValueHeaders": { "Accept": [ "*/*" ], "CloudFront-Forwarded-Proto": [ "https" ], "CloudFront-Is-Desktop-Viewer": [ "true" ], "CloudFront-Is-Mobile-Viewer": [ "false" ], "CloudFront-Is-SmartTV-Viewer": [ "false" ], "CloudFront-Is-Tablet-Viewer": [ "false" ], "CloudFront-Viewer-Country": [ "JP" ], "Host": [ "一応隠す" ], "User-Agent": [ "curl/7.64.1" ], "Via": [ "一応隠す" ], "X-Amz-Cf-Id": [ "一応隠す" ], "X-Amzn-Trace-Id": [ "一応隠す" ], "X-Forwarded-For": [ "一応隠す" ], "X-Forwarded-Port": [ "443" ], "X-Forwarded-Proto": [ "https" ] }, "queryStringParameters": null, "multiValueQueryStringParameters": null, "pathParameters": null, "stageVariables": null, "requestContext": { "resourceId": "一応隠す", "resourcePath": "/test1", "httpMethod": "GET", "extendedRequestId": "一応隠す", "requestTime": "30/Dec/2020:06:47:19 +0000", "path": "/dev/test1", "accountId": "一応隠す", "protocol": "HTTP/1.1", "stage": "dev", "domainPrefix": "一応隠す", "requestTimeEpoch": 一応隠す, "requestId": "一応隠す", "identity": { "cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "sourceIp": "一応隠す", "principalOrgId": null, "accessKey": null, "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "curl/7.64.1", "user": null }, "domainName": "一応隠す", "apiId": "一応隠す" }, "body": null, "isBase64Encoded": false }
リクエストパラメータ
GETの場合は「queryStringParameters」とかに、POSTの場合は「body」に入るみたい
"queryStringParameters": null, "multiValueQueryStringParameters": null, "body": null,
Lambda単体で動作検証したい場合はここにパラメータ突っ込めば良いかな・・・
次に作りたいもの
とりあえず、 S3にファイルが置かれたら起動するやつとか作ってみたい