I'm configuring a terraform module with lambda and VPC endpoint deployment, and currently, I would like to use more cleaner way of defining and accessing the vars in the code
S3 vars are used for creating lambda function aws resource and vpc endpoint vars for creating aws_vpc_endpoint resource
Below are my local vars which have the same conditions
Is it possible to somehow create one var for s3 and a second one for vpc endpoint where I can apply only one condition to the entire var without duplicating?
s3_bucket = var.s3_existing_package != null ? try(var.s3_existing_package.bucket, null) : null
s3_key = var.s3_existing_package != null ? try(var.s3_existing_package.key, null) : null
s3_object_version = var.s3_existing_package != null ? try(data.aws_s3_object.this.version_id, null) : null
## VPC endpoint
vpc_id = var.enable_api_gw && var.api_gw_endpoint_type == "PRIVATE" ? try(var.vpc_id, null) : null
vpc_endpoint_subnet_ids = var.enable_api_gw && var.api_gw_endpoint_type == "PRIVATE" ? try(var.vpc_endpoint_subnet_ids, null) : null
vpc_endpoint_default_sg_allowed_cidrs = var.enable_api_gw && var.api_gw_endpoint_type == "PRIVATE" ? try(var.vpc_endpoint_default_sg_allowed_cidrs, null) : null
vpc_endpoint_additional_security_group_ids = var.enable_api_gw && var.api_gw_endpoint_type == "PRIVATE" ? try(var.vpc_endpoint_additional_security_group_ids, []) : []
https://developer.hashicorp.com/terraform/language/values/locals#declaring-a-local-value
Here is one example using your code
and a terraform plan on that will show
My recommendation, start simple like that one variable one local and output, complicate that local as much as you want to test then bring it to your project and integrate with the rest of your resources, the same
tryas you have should be no problem