Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF 0.12: template_file still contains unknown values during apply #21455

Closed
brikis98 opened this issue May 26, 2019 · 5 comments · Fixed by #22137
Closed

TF 0.12: template_file still contains unknown values during apply #21455

brikis98 opened this issue May 26, 2019 · 5 comments · Fixed by #22137

Comments

@brikis98
Copy link
Contributor

brikis98 commented May 26, 2019

Terraform Version

0.12.0

Terraform Configuration Files

The failure happens in a relatively large amount of code. I think the relevant parts are as follows.

I have one module called mysql:

resource "aws_db_instance" "example" {
  identifier_prefix   = "example"
  engine              = "mysql"
  allocated_storage   = 10
  instance_class      = "db.t2.micro"
  name                = "example"
  username            = "admin"
  password            = "password"
  skip_final_snapshot = true
}

output "address" {
  value = aws_db_instance.example.address
}

output "port" {
  value = aws_db_instance.example.port
}

I have another module called asg:

resource "aws_launch_configuration" "example" {
  image_id        = var.ami
  instance_type   = "t2.micro"
  user_data       = data.template_file.user_data.rendered
}

resource "aws_autoscaling_group" "example" {
  name = "example-${aws_launch_configuration.example.name}"

  launch_configuration = aws_launch_configuration.example.name
  vpc_zone_identifier  = data.aws_subnet_ids.default.ids

  min_size = 2
  max_size = 2
  min_elb_capacity = 2

  lifecycle {
    create_before_destroy = true
  }
}

data "aws_vpc" "default" {
  default = true
}

data "aws_subnet_ids" "default" {
  vpc_id = data.aws_vpc.default.id
}

data "template_file" "user_data" {
  template = <<EOF
#!/bin/bash

echo "db_address = ${db_address}"
echo "db_port = ${db_port}"
EOF

  vars = {
    db_address  = local.mysql_config.address
    db_port     = local.mysql_config.port
  }
}

locals {
  mysql_config = var.mysql_config == null ? data.terraform_remote_state.db[0].outputs : var.mysql_config
}

variable "ami" {}

variable "mysql_config" {
  description = "The config for the MySQL DB"
  type        = object({
    address = string
    port    = number
  })
  default     = null
}

data "terraform_remote_state" "db" {
  count = var.mysql_config == null ? 1 : 0

  backend = "s3"

  config = {
    bucket = "bucket"
    key    = "key"
    region = "us-east-2"
  }
}

The idea is that the asg module can get the DB data either via the mysql_config input variable or via terraform_remote_state. I'm trying to use these two modules together as follows:

provider "aws" {
  region = "us-east-2"
}

module "asg" {
  source = "../asg"

  ami = "ami-0c55b159cbfafe1f0"
  mysql_config = module.mysql
}

module "mysql" {
  source = "../mysql"
}

In other words, I'm trying to pass all the outputs from the mysql module straight to the mysql_config input variable of the asg module.

Expected Behavior

I run terraform apply and both MySQL and the ASG come up.

Actual Behavior

I run terraform apply, MySQL comes up, but when trying to bring up the ASG, I get the following error:

Error: configuration for module.asg.data.template_file.user_data still contains unknown values during apply (this is a bug in Terraform; please report it!)

As instructed, I'm now reporting the bug!

Steps to Reproduce

  1. terraform init
  2. terraform apply
@brikis98
Copy link
Contributor Author

brikis98 commented May 26, 2019

Update: this bug appears to be related to the use of conditional logic in the mysql_config local var. If I change this:

locals {
  mysql_config = var.mysql_config == null ? data.terraform_remote_state.db[0].outputs : var.mysql_config
}

To this:

locals {
  mysql_config = var.mysql_config
}

Then terraform apply works without errors (but breaks my main use case of supporting either terraform_remote_state or an input variable). So I guess the issue isn't related to passing through the output of the mysql module, but using that output in a conditional?

@kenichi-fukushima
Copy link

I hit an error that seems to be the same as the issue reported here.
Here is a small but complete configuration to reproduce it.
https://github.com/kenichi-fukushima/terraform-bug

@pselle pselle modified the milestones: v0.12.1, TBD Jun 4, 2019
@rlees85
Copy link

rlees85 commented Jul 15, 2019

Also hitting this bug with Terraform 0.12.4

I do not think it is related to #21465

Fails:

###############################################################################

data "template_file" "samba_server_setup_script" {
  template = file(format("%s/samba-server-setup.sh.tpl", local.tools))

  vars = {
    "region"      = var.region
    "env_name"    = var.env_name
    "vpc_name"    = var.vpc_name
    "user_name"   = var.samba_user_name
    "efs_mount"   = var.samba_efs_mountpoint
    "extra_init"  = var.client
    "efs_address" = module.commerce_efs.route53_records_name
    "stream_name" = var.stream_name
  }
}

###############################################################################

Works:

###############################################################################

data "template_file" "samba_server_setup_script" {
  template = file(format("%s/samba-server-setup.sh.tpl", local.tools))

  vars = {
    "region"      = var.region
    "env_name"    = var.env_name
    "vpc_name"    = var.vpc_name
    "user_name"   = var.samba_user_name
    "efs_mount"   = var.samba_efs_mountpoint
    "extra_init"  = var.client
    "efs_address" = format("%s.%s.%s.%s.%s", var.efs_class, var.env_name, var.stream_name, var.vpc_name, var.master_account_zone) # Should be: module.commerce_efs.route53_records_name - https://github.com/hashicorp/terraform/issues/21455
    "stream_name" = var.stream_name
  }
}

###############################################################################

Terraform looks like it does not like looking at module outputs or data source outputs for data source inputs now....

@rlees85
Copy link

rlees85 commented Aug 6, 2019

This is not fixed in Terraform 0.12.6

@ghost
Copy link

ghost commented Aug 18, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants