3

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294
madushanka mdk
  • 153
  • 1
  • 6

1 Answers1

1

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Yes @Marcin. But here I create lambda functions by iterating "lambdas" map. When integrating by using "for_each" for every method how I get each relevant lambda function's invocation uri because lambda functions are also created by iterating that map? Problem is here I create resources by iterating because I faced some difficulties code every resource one by one. Please correct me if I am wrong. – madushanka mdk Feb 02 '22 at 01:56
  • 1
    @madushankamdk So use `aws_lambda_function.lambda_functions[each.key].invoke_arn`. – Marcin Feb 02 '22 at 02:10
  • Thank you @Marcin. It works. One thing to clarify, always map and created resources are same order right? Because here use index. – madushanka mdk Feb 02 '22 at 02:47
  • 2
    @madushankamdk Maps don't rely on ordering of items, but they use unique keys. If you were to use `count`, the order would be important. – Marcin Feb 02 '22 at 02:50