How to get AWS Lambda ARN using Terraform?

半城伤御伤魂 提交于 2021-01-28 07:57:42

问题


I am trying to define a terraform output block that returns the ARN of a Lambda function. The Lambda is defined in a sub-module. According to the documentation it seems like the lambda should just have an ARN attribute already: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/lambda_function#arn

Using that as a source I thought I should be able to do the following:

output "lambda_arn" {
  value = module.aws_lambda_function.arn
}

This generates the following error:

Error: Unsupported attribute

  on main.tf line 19, in output "lambda_arn":
  19:   value = module.aws_lambda_function.arn

This object does not have an attribute named "arn".

I would appreciate any input, thanks.


回答1:


Documentation is correct. Data source data.aws_lambda_function has arn attribute. However, you are trying to access the arn from a custom module module.aws_lambda_function. To do this you have to define output arn in your module.

So in your module you should have something like this:

data "aws_lambda_function" "existing" {
  function_name = "function-to-get"
}

output "arn" {
  value = data.aws_lambda_function.existing.arn
}

Then if you have your module called aws_lambda_function:

module "aws_lambda_function" {
   source = "path-to-module"   
}

you will be able to access the arn:

module.aws_lambda_function.arn


来源:https://stackoverflow.com/questions/65798783/how-to-get-aws-lambda-arn-using-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!