m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

AWSのLambdaとAPIGatwwayを連携

作りたいもの

f:id:m_shige1979:20201230100338p:plain

要約

なんか簡単な処理をAPIを経由してアクセスしたい

lambdaでなんかできそう

postmanで再現するにはホストがいる

API Gatewayでいけそう⇦今ここ

多分、APIGateway+Lambdaの連携でできるかも

lambdaは色々なAWSサービスのトリガーで動作することができる

今回はAPI Gatewayから起動するようにする

やること

1. APIGatewayを作成

f:id:m_shige1979:20201230150947p:plain
f:id:m_shige1979:20201230150852p:plain
f:id:m_shige1979:20201230151224p:plain
f:id:m_shige1979:20201230151332p:plain

2. APIのリソースを作成

f:id:m_shige1979:20201230151456p:plain
f:id:m_shige1979:20201230151830p:plain
f:id:m_shige1979:20201230152047p:plain

3. APIのメソッドを作成

f:id:m_shige1979:20201230152232p:plainf:id:m_shige1979:20201230152344p:plainf:id:m_shige1979:20201230152456p:plain

4. lambdaを作成

f:id:m_shige1979:20201230152636p:plain
f:id:m_shige1979:20201230152939p:plain
f:id:m_shige1979:20201230153047p:plain
f:id:m_shige1979:20201230153248p:plain

添付コード

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);
};


f:id:m_shige1979:20201230153504p:plain

5. APIとLambdaを紐付けデプロイ

f:id:m_shige1979:20201230153940p:plain
f:id:m_shige1979:20201230154100p:plain
f:id:m_shige1979:20201230154217p:plain
f:id:m_shige1979:20201230154320p:plain
f:id:m_shige1979:20201230154506p:plain
f:id:m_shige1979:20201230154621p:plain

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にファイルが置かれたら起動するやつとか作ってみたい