mfjt diary


2020-05-04(月)

JAWS-UG 初心者支部&千葉支部 #26 新人さん歓迎!ハンズオン&LT(オンライン) を CLI でやってみる (ハンズオン 2 編) #jawsug_bgnr

ハンズオン 1 編の続き。

Lambda Function 作成

ロールはハンズオン 1 のときのものを流用する。

$ zip translate-function-with-apigw.zip translate-function-with-apigw.py
  adding: translate-function-with-apigw.py (deflated 48%)

$ aws lambda create-function \
    --function-name translate-function-with-apigw \
    --runtime python3.8 \
    --role "arn:aws:iam::760182048326:role/translate-function-role" \
    --handler translate-function-with-apigw.lambda_handler \
    --zip-file fileb://translate-function-with-apigw.zip
{
    "FunctionName": "translate-function-with-apigw",
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:760182048326:function:translate-function-with-apigw",
    "Runtime": "python3.8",
    "Role": "arn:aws:iam::760182048326:role/translate-function-role",
    "Handler": "translate-function-with-apigw.lambda_handler",
    "CodeSize": 462,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2020-05-04T09:48:09.842+0000",
    "CodeSha256": "NPM1vqsoCFi8HwNW5DdMNMq6gRWzgV6EA0fcLiVblzE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "cc6b2ba0-eb44-4897-b6c5-1709f8d49ef4",
    "State": "Active",
    "LastUpdateStatus": "Successful"
}

API 作成

リソースを作成

ID も確認する。

$ aws apigateway create-rest-api --name translate-api
{
    "id": "xq5is7wlmc",
    "name": "translate-api",
    "createdDate": "2020-05-04T19:51:18+09:00",
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "EDGE"
        ]
    }
}

$ aws apigateway get-resources --rest-api-id xq5is7wlmc
{
    "items": [
        {
            "id": "ebymz9abo0",
            "path": "/"
        }
    ]
}
子リソースを作成
$ aws apigateway create-resource \
    --rest-api-id xq5is7wlmc \
    --parent-id ebymz9abo0 \
    --path-part translate
{
    "id": "k8hfnw",
    "parentId": "ebymz9abo0",
    "pathPart": "translate",
    "path": "/translate"
}
メソッドを作成
$ aws apigateway put-method \
    --rest-api-id xq5is7wlmc \
    --resource-id k8hfnw \
    --http-method GET \
    --authorization-type NONE
{
    "httpMethod": "GET",
    "authorizationType": "NONE",
    "apiKeyRequired": false
}

$ aws apigateway put-integration \
    --rest-api-id xq5is7wlmc \
    --resource-id k8hfnw \
    --http-method GET \
    --type AWS_PROXY \
    --integration-http-method POST \
    --uri "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:760182048326:function:translate-function-with-apigw/invocations"
{
    "type": "AWS_PROXY",
    "httpMethod": "POST",
    "uri": "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:760182048326:function:translate-function-with-apigw/invocations",
    "passthroughBehavior": "WHEN_NO_MATCH",
    "timeoutInMillis": 29000,
    "cacheNamespace": "k8hfnw",
    "cacheKeyParameters": []
}

$ aws apigateway put-method-response \
    --rest-api-id xq5is7wlmc \
    --resource-id k8hfnw \
    --http-method GET \
    --status-code 200 \
    --response-models '{"application/json": "Empty"}'
{
    "statusCode": "200",
    "responseModels": {
        "application/json": "Empty"
    }
}
API Gateway への 権限付与
$ aws lambda add-permission \
    --function-name translate-function-with-apigw \
    --statement-id 72e0a706-02e8-479f-affb-0a2dcc5d4a29 \
    --action "lambda:InvokeFunction" \
    --principal apigateway.amazonaws.com
{
    "Statement": "{\"Sid\":\"72e0a706-02e8-479f-affb-0a2dcc5d4a29\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:ap-northeast-1:760182048326:function:translate-function-with-apigw\"}"
}
デプロイ
$ aws apigateway create-deployment \
    --rest-api-id xq5is7wlmc \
    --stage-name dev
{
    "id": "hq4jn2",
    "createdDate": "2020-05-04T20:08:33+09:00"
}

動作確認

$ curl https://xq5is7wlmc.execute-api.ap-northeast-1.amazonaws.com/dev/translate?input_text=%E5%88%9D%E3%82%81%E3%81%A6%E3%81%AEAPI%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%A7%E3%81%99
{"output_text": "This is my first API call"}

所感

  • メソッド作成時、統合タイプLambda 関数 にはなっているものの、Lambda プロキシ統合の使用が有効になっていないがためにきちんと動かず結構詰まった
    • put-integrationtypeAWS_PROXY にする
  • マネジメントコンソールから作った API と同じになるように put-integration やら put-method-response をとりあえず実行したので、そのあたりの本質はまだ理解できていない

参考