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
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.Defining aws_lambda_function.test_lambda.function_name instead of direct function name gives us an advantage of configurable property at run time.
……….
You’ve got two related resources; a
aws_lambda_permission
resource, and aaws_lambda_function
.function_name
is one of the required parameters foraws_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 associatedaws_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.