skip to Main Content

I’m learning more about terraform and AWS. I’ve seen a code in
Working with aws_lambda_permission and aws_apigatewayv2_api

resource "aws_lambda_permission" "api_permission" {
  statement_id  = "allow_apigw_invoke"
  function_name = aws_lambda_function.get_user_lambda.lambda_function_name
  action        = "lambda:InvokeFunction"
  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_apigatewayv2_api.users_api.execution_arn}/*/*/${split("/", aws_apigatewayv2_route.get_user_route.route_key)[1]}"

}

I would like to know why the function name is given as

function_name = aws_lambda_function.get_user_lambda.lambda_function_name

Is there any single term used to indicate module.name of the module.function name

3

Answers


  1. If you are going to change your function name when you define aws_lambda_function.get_user_lambda, you will have to manually updated entire codebase to change the name in other parts which depend on the function.

    Using aws_lambda_function.get_user_lambda.function_name saves you a lot of work, as correct name will be used always, and you do not have to manually change that name.

    Login or Signup to reply.
  2. resource "aws_lambda_function" "test_lambda" {
      filename      = "lambdatest.zip"
      function_name = var.lambda_function_name
      role          = aws_iam_role.iam_for_lambda.arn
      handler       = "exports.handler"
      runtime       = "nodejs12.x"
    }
    
    resource "aws_lambda_permission" "allow_cloudwatch" {
      statement_id  = "AllowExecutionFromCloudWatch"
      action        = "lambda:InvokeFunction"
      function_name = aws_lambda_function.test_lambda.function_name
      principal     = "events.amazonaws.com"
      source_arn    = "arn:aws:events:eu-west-1:111122223333:rule/RunDaily"
      qualifier     = aws_lambda_alias.test_alias.name
    }
    

    Defining aws_lambda_function.test_lambda.function_name instead of direct function name gives us an advantage of configurable property at run time.

    ……….

    Login or Signup to reply.
  3. You’ve got two related resources; a aws_lambda_permission resource, and a aws_lambda_function. function_name is one of the required parameters for aws_lambda_permission; it tells Terraform which Lambda function to apply the permission to.

    You can certainly hard-code a function name, if you want, but doing it this way means changing the aws_lambda_function‘s name doesn’t require you to also change the hard-coded name in the associated aws_lambda_permission. Terraform may also not understand the dependencies between the function and its permission this way and wind up creating things out-of-order.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search