简体   繁体   中英

Integrate AWS API Gateway methods with multiple Lambdas using Terraform

I have to integrate more than 50 Lambda functions with relevant api gateway method. So that I create a terraform map with Lambda function name and api gateway resource path as below.

variable "lambdas" {
  description = "Map of Lambda function names and API gateway resource paths."
  type        = map
  default = {
    user = {
      name = "user-lambda-function"
      path = "user"
    },
    products= {
      name = "products-lambda-function"
      path = "products"
    },
    orders= {
      name = "orders-lambda-function"
      path = "orders"
    },

Then I iterate lambda function creation through this map using for_each as below.

resource "aws_lambda_function" "lambda_functions" {
  for_each         = var.lambdas
  filename         = "lambda_function_code.zip"
  function_name    = each.value.name
  role             = data.aws_iam_role.lambda_execution_role.arn
  handler          = "index.handler"
  source_code_hash = filebase64sha256("lambda_function_code.zip")

  runtime = "nodejs14.x"
}

After that I start to create API Gateway, resources and methods as below,

resource "aws_api_gateway_rest_api" "api_gateway" {
  name = var.api-gateway-name
}

resource "aws_api_gateway_resource" "resources" {
  for_each    = var.lambdas
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  parent_id   = aws_api_gateway_rest_api.api_gateway.root_resource_id
  path_part   = each.value.path
}

resource "aws_api_gateway_method" "methods" {
  for_each         = aws_api_gateway_resource.resources
  rest_api_id      = aws_api_gateway_rest_api.api_gateway.id
  resource_id      = each.value.id
  http_method      = "POST"
  authorization    = "NONE"
  api_key_required = false
}

Then I try to integrate above API Gateway method with relevant Lambda function by iterating above above methods. But here I have to input relevant lambda function invocation uri.

resource "aws_api_gateway_integration" "integration" {
  for_each = aws_api_gateway_method.methods
  rest_api_id             = each.value.rest_api_id
  resource_id             = each.value.resource_id
  http_method             = each.value.http_method
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = ""
}

I am struggling to input this relevant lambda function uri to integrate with relevant method.

My question is, How do I get relevant lambda function uri to input here with this iteration? Or any solution to achieve this without code every lambdas, resources and methods.

The aws_lambda_function has invoke_arn which is:

ARN to be used for invoking Lambda Function from API Gateway - to be used in aws_api_gateway_integration's uri

So you have to use that in your aws_api_gateway_integration .

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