Hi,
I’m trying to configure a dynamic access block for the “access_logs” block in the resource “aws_lb” (an application load balancer). My goal is to make the block optional, so that if no parameters are defined for the block, the inline block is omitted completely from the resource “aws_lb” block.
My configuration is currently as follows:
child module
alb resource
resource "aws_lb" "application_loadbalancer" {
name = "${var.prefix}${var.lb_name}"
internal = var.lb_internal
load_balancer_type = "application"
...
dynamic "access_logs" {
for_each = var.access_logs
content {
enabled = lookup(var.access_logs, "enabled", true)
bucket = lookup(var.access_logs, "bucket", null)
prefix = lookup(var.access_logs, "prefix", null)
}
}
child module variable declaration
variable "access_logs" {
description = "An access logs block"
type = map(any)
default = {}
}
root module
module "alb_int" {
source = "../../terraform-library/module-alb"
access_logs = {
enabled = true
bucket = "my-bucket"
prefix = "my-loadbalancer"
}
But this throws an error:
Error: Too many access_logs blocks
on line 0:
(source code not available)
No more than 1 "access_logs" blocks are allowed
I also tried with:
dynamic "access_logs" {
for_each = var.access_logs
content {
enabled = lookup(access_logs.value, "enabled", true)
bucket = lookup(access_logs.value, "bucket", null)
prefix = lookup(access_logs.value, "prefix", null)
}
}
But this throws an Error: Invalid Function Argument “Invalid value for “inputMap” parameter: lookup() requires a map as the first
argument.”
How would it be possible to iterate with the map itself as the “argument” and not the keys/values?
Many thanks in advance!
Graham