简体   繁体   中英

How to reference handler in Terraform for a Lambda on S3

My lambda is in a zip archive on S3, and I have deployed both the lambda and an API Gateway on AWS.

Here's what I have in my project root folder:

  • index.js (entrypoint)
  • package.json
  • README.md
  • node_modules
  • build.sh (a bash script that executes npm install and zip -r project-folder. )

index.js is the entry point, within the file, I'm using https://github.com/spring-media/aws-lambda-router to handle the routing:

exports.handler = router.handler({
    proxyIntegration: {
        routes: [
            <routes_defined>
        ]
    }
})

In my terraform infrastructure below (redacted many resources parameters for simplicity), I am creating a lambda resource using a module and feeding in the handler to be index.handler and references my S3 bucket. All the other resources are related to the API gateway and permission between the gateway and the lambda:

module "module_name" {
  handler            = "index.handler"
  s3_bucket          = <references_s3_bucket>
}


resource "aws_api_gateway_rest_api" "api_gateway"
resource "aws_api_gateway_rest_api_policy" "api_gateway_policy" 
resource "aws_api_gateway_resource" "proxy"

resource "aws_lambda_permission" "allow_api_gateway" 

resource "aws_api_gateway_method" "proxy" 
resource "aws_api_gateway_integration" "api_gateway_integration" 
resource "aws_api_gateway_deployment" "api_gateway_deploy" 
resource "aws_api_gateway_stage" "api_gateway_stage" 
resource "aws_api_gateway_api_key" "api_gateway_key" 
resource "aws_api_gateway_usage_plan" "api_gateway_usage_plan" 
resource "aws_api_gateway_usage_plan_key" "api_gateway_usage_plan_key" 

Now this doesn't show anything wrong upon deployment, until I actually test the API gateway on my AWS console. When I issue a request to the API gateway, it responds with an error of index.js is undefined or not exported . What seems to be the issue? I've made sure that the index.js is in the root folder as described in this issue: Index handler is undefined or not exported

Oh well, I think the setup of index.handler is correct, I simply missed out on the part where we had aliases setup, and my correct version wasn't set to active. Everything worked once I bumped my version to the active one:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM