Skip to content

p4irin/explore_terraform_localstack_aws_api_gateway_lambda_hello_js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Explore Terraform, LocalStack, AWS api gateway, AWS lambda hello javascript function

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

General steps

  1. $ localstack start
  2. Create lambda function
  3. Create REST API
  4. Get the id of the root resource
  5. Create a resource using the root as a parent
  6. Add method to resource
  7. Create integration
  8. Create deployment
  9. $ localstack stop

Code

The javscript code for the lambda function is in lambda.js.

zip the file into function.zip.

$ zip function.zip lambda.js
...
$

AWS CLI

$ localstack start

Create the lambda function

$ 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

Create REST API

$ 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

Get the id of the root resource

$ 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

Create a resource using the root resource as its parent

$ 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

Add method to resource

$ 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"

Create integration

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

Create deployment

$ aws --profile localstack apigateway create-deployment \
  --rest-api-id $rest_api_id \
  --stage-name dev

Verify your API

By calling it:

$ curl -X GET http://${rest_api_id}.execute-api.localhost.localstack.cloud:4566/dev/test

{"message":"Hello from Lambda"}

$ localstack stop

Terraform

Refer 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.

References

About

Use lambda proxy integration to integrate an api method with a lambda function

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published