When I configured Lambda + API Gateway with AWS Amplify, I sometimes had a little trouble because I couldn't get a normal response even when I tried to execute the test. So, along with how to add API in Amplify, I will write it as a memorandum and learning.
In conclusion, when calling the Lambda function from API Gateway, it seems that it is necessary to return the following response.
Lambda function output format for proxy integration (https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format)
return {
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}
headers
and nultiValueHeaders
are not specially required, it seems that you should at least return statusCode
and body
.
。
--WINdows10 version 20H2
Start with the project being created in Amplfy.
Add a Lambda function.
$ amplify add function
? Select which capability you want to add: Lambda function (serverless function)
? Provide an AWS Lambda function name: myfunc
? Choose the runtime that you want to use: Python
Only one template found - using Hello World by default.
Available advanced settings:
- Resource access permissions
- Scheduled recurring invocation
- Lambda layers configuration
? Do you want to configure advanced settings? No
? Do you want to edit the local lambda function now? Yes
If you answer Yes to Do you want to edit the local lambda function now ?, a text editor will open with the initial code similar to the following.
def handler(event, context):
print('received event:')
print(event)
return {
'message': 'Hello from your new Amplify Python lambda!'
}
Normally, when Lambda is written in python, the handler name is lambda_handler
, but when making it with Amplify, it seems that it is only handler
. The handler name specified in Lambda has also been changed, so there is no need to modify it.
If this is left as it is, the response that API Gateway does not expect is not obtained, so modify the code as follows.
def handler(event, context):
print('received event:')
print(event)
return {
'isBase64Encoded': False,
'statusCode': 200,
'headers': {},
'body': '{"message": "Hello from your new Amplify Python lambda!"}'
}
Press Enter in the terminal to add the Lambda function to your Amplify project.
? Press enter to continue
Successfully added resource myfunc locally.
Next steps:
Check out sample function code generated in <project-dir>/amplify/backend/function/myfunc/src
"amplify function build" builds all of your functions currently in the project
"amplify mock function <functionName>" runs your function locally
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
Add the API to your Amplify project.
$ amplify add api
? Please select from one of the below mentioned services: REST
? Provide a friendly name for your resource to be used as a label for this category in the project: myapi
? Provide a path (e.g., /book/{isbn}): /items
? Choose a Lambda source Use a Lambda function already added in the current Amplify project
? Choose the Lambda function to invoke by this path myfunc
? Restrict API access No
? Do you want to add another path? No
Successfully added resource myapi locally
Some next steps:
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud
Now that it's just added locally, deploy the Amplify project on your AWS account.
To deploy your project on AWS, use the command amplify push
.
When you run this command, the resources will be provisioned in the cloud as follows:
!
By default, the method is registered as ANY, so select ANY.
If you choose to test the client, you will be taken to a screen where you can enter a method or query string. This time we will run the test with GET.
I got the expected response.
If this returns the response of the Lambda function in an incorrect format that lacks the status code etc., it will be as follows.
When using Lambda + API Gateway, the response must be in the correct format.
Also, Amplify is a lot of fun because it's easy to implement the API.
Recommended Posts