Home > other >  Terraform: Passing variables while importing module
Terraform: Passing variables while importing module

Time:10-27

After reading this https://developer.hashicorp.com/terraform/language/values/variables#assigning-values-to-root-module-variables, I was certain that there are 3 ways to set variable value.

Recently I came across code in one our projects which passes variables while importing module.

module "robot_shell" {
  source = "./modules/xx_shell"

  xx_resource_name_prefix             = local.resource_name_prefix
  cloudwatch_log_group_retention_days = 30
  s3_expiration_days                  = 30
}

file in the module ./modules/xx_shell

resource "aws_s3_bucket_lifecycle_configuration" "dest" {
  bucket = aws_s3_bucket.dest.bucket
  rule {
    id     = "expire"
    status = "Enabled"
    expiration {
      days = var.s3_expiration_days
    }
  }
}

variable definition variables.tf

variable "s3_expiration_days" {
  type        = number
  description = "S3 bucket objects expiration days https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration#days"
}

Why does terraform documentation does not talk about it? Or is this a old way which is not used anymore?

CodePudding user response:

Its a normal way to pass variables to modules as you described. This is described in TF docs in:

The link that you provided in the question is about passing variables to root/parent module when you run plan/apply. But when you want to pass variables to sub-modules defined using module block, you pass them through module arguments.

  • Related