'How can I get all AWS Lambdas by Tag in Terraform
I have Lambdas with Tag of Name=production.
I would like to get them using Terraform, looking at aws_lambda_function, I can only get single lambda by function_name
data "aws_lambda_function" "existing" {
function_name = var.function_name
}
Solution 1:[1]
You can use the aws_resourcegroupstaggingapi_resources to retrieve information for several AWS resources that don't have more specific data sources.
For your use case, considering Name=production, you can use:
data "aws_resourcegroupstaggingapi_resources" "existing" {
resource_type_filters = ["lambda:function"]
tag_filter {
key = "Name"
values = ["production"]
}
}
output "arns" {
value = data.aws_resourcegroupstaggingapi_resources.existing.resource_tag_mapping_list.*.resource_arn
}
Update: as noted in a comment, the code above returns information from resource_tag_mapping_list, which is mostly compliant information and ARN of resources. But you can pair that with a regular aws_lambda_function data source and using a for_each to retrieve all information from your Lambda functions:
# continuation of the code above
data "aws_lambda_function" "existing" {
for_each = toset(data.aws_resourcegroupstaggingapi_resources.existing.resource_tag_mapping_list.*.resource_arn)
function_name = each.value
}
output "functions" {
value = data.aws_lambda_function.existing.*
}
# example of information available with this data source
output "functions_runtime" {
value = {for fn, result in data.aws_lambda_function.existing: fn => result.runtime}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
