Home > Back-end >  What will be the purpose of .function_name in terraform code
What will be the purpose of .function_name in terraform code

Time:09-29

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

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related