Use lambda proxy integration to integrate an api method with a lambda function. The lambda function is a javascript function that simply returns the string "Hello from Lambda!"
Deploy with
- AWS CLI
- Terraform
$ localstack start- Create lambda function
- Create REST API
- Get the id of the root resource
- Create a resource using the root as a parent
- Add method to resource
- Create integration
- Create deployment
$ localstack stop
The javscript code for the lambda function is in lambda.js.
zip the file into function.zip.
$ zip function.zip lambda.js
...
$$ localstack start$ aws --profile localstack lambda create-function \
--function-name apigw-lambda \
--runtime nodejs16.x \
--handler lambda.apiHandler \
--memory-size 128 \
--zip-file fileb://function.zip \
--role arn:aws:iam::111111111111:role/apigw$ aws --profile localstack apigateway create-rest-api \
--name 'API Gateway Lambda integration'
{
"id": "fxb6vjawg2",
...
}You'll need that "id" in the next and later steps.
Copy and paste the value of "id" in the following command
$ rest_api_id=fxb6vjawg2$ aws --profile localstack apigateway get-resources \
--rest-api-id $rest_api_id
{
"items": [
{
"id": "edqon7lp3u",
"path": "/"
}
]
}You'll need that root resource id in the next and later steps.
Copy and paste the value of "id" into this command
$ root_resource_id=edqon7lp3u$ aws --profile localstack apigateway create-resource \
--rest-api-id $rest_api_id \
--parent-id $root_resource_id \
--path-part "{somethingId}"
{
"id": "a9baesz9x3",
"parentId": "edqon7lp3u",
"pathPart": "{somethingId}",
"path": "/{somethingId}"
}You'll need the "id" of the created resource to add a method to it and integrate it with the lambda function.
Copy and paste into
$ resource_id=a9baesz9x3$ aws --profile localstack apigateway put-method \
--rest-api-id $rest_api_id \
--resource-id $resource_id \
--http-method GET \
--request-parameters "method.request.path.somethingId=true" \
--authorization-type "NONE"Integrate the resource with the lambda function.
$ aws --profile localstack apigateway put-integration \
--rest-api-id $rest_api_id \
--resource-id $resource_id \
--http-method GET \
--type AWS_PROXY \
--integration-http-method POST \
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:apigw-lambda/invocations \
--passthrough-behavior WHEN_NO_MATCH$ aws --profile localstack apigateway create-deployment \
--rest-api-id $rest_api_id \
--stage-name devBy calling it:
$ curl -X GET http://${rest_api_id}.execute-api.localhost.localstack.cloud:4566/dev/test
{"message":"Hello from Lambda"}$ localstack stopRefer to th *.tf files.
The stage_name attribute of the aws_api_gateway_deployment resource is deprecated. Instead, an explicit aws_api_gateway_stage resource is used.
All dependencies are implicit. Resources are created in correct order.