Skip to content

Instantly share code, notes, and snippets.

@DoctorVin
Created April 8, 2015 01:10
Show Gist options
  • Save DoctorVin/12c3c845452baa968025 to your computer and use it in GitHub Desktop.
Save DoctorVin/12c3c845452baa968025 to your computer and use it in GitHub Desktop.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec suscipit faucibus gravida.
Morbi lobortis consectetur ligula, et consequat enim interdum non. Sed gravida lacus id
ornare molestie. Duis placerat metus at lobortis porttitor. Praesent malesuada scelerisque
leo a vestibulum. Fusce finibus, mauris ut ultricies blandit, lacus mauris laoreet odio,
nec elementum nibh orci et lectus. Duis ullamcorper arcu augue, eget sagittis lacus
pellentesque et. Proin eu pretium est. Vestibulum quis malesuada amet.
provider "aws" {
access_key = "key"
secret_key = "secret"
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-7f89a64f"
instance_type = "t1.micro"
count = "3"
user_data = "${file(\"lorem.txt\")}"
tags {
Name = "Web-${count.index}"
Serial = "${count.index}"
}
}
@DoctorVin
Copy link
Author

Test case for a weird sort-of race in terraform resource parsing. The problem is at line 11 of resource.tf, where we open a file and pull the contents into a variable. In hindsight, doing things this way is not efficient at all, as the file is read on every iteration. However, it's probably a typical "new guy mistake" in that you might not realize that file() is a function and doing IO while the parser is setting up your resources.

The problem in this test case manifests as weird, non-deterministic values in the tags for name and serial number. These values are internally consistent, but instead of consistently generating Web-0, Web-1, Web-2, you get (for example):

doctorvin$ terraform plan | grep -E 'Name|Serial'
    tags.Name:                "" => "Web-0"
    tags.Serial:              "" => "0"
    tags.Name:                "" => "Web-1"
    tags.Serial:              "" => "1"
    tags.Name:                "" => "Web-1"
    tags.Serial:              "" => "1"
doctorvin$ terraform plan | grep -E 'Name|Serial'
    tags.Name:                "" => "Web-2"
    tags.Serial:              "" => "2"
    tags.Name:                "" => "Web-2"
    tags.Serial:              "" => "2"
    tags.Name:                "" => "Web-2"
    tags.Serial:              "" => "2"
doctorvin$ terraform plan | grep -E 'Name|Serial'
    tags.Name:                "" => "Web-${count.index}"
    tags.Serial:              "" => "${count.index}"
    tags.Name:                "" => "Web-${count.index}"
    tags.Serial:              "" => "${count.index}"
    tags.Name:                "" => "Web-2"
    tags.Serial:              "" => "2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment