Terraform: cloud-config user-data from data.template_file isn't executed on aws_instance in v0.7.11

Created on 18 Nov 2016  ·  8Comments  ·  Source: hashicorp/terraform

Terraform Version

Terraform v0.7.11

Affected Resource(s)

Please list the resources as a list, for example:

  • aws_instance
  • template_file

Terraform Configuration Files

resource "aws_instance" "bastion" {
  ami = "${ var.ami-id }"
  associate_public_ip_address = true
  iam_instance_profile = "${ aws_iam_instance_profile.bastion.name }"
  instance_type = "${ var.instance-type }"
  key_name = "${ var.key-name }"

  source_dest_check = false
  private_ip = "${ var.bastion-ip }"
  subnet_id = "${ element( split(",", var.subnet-ids), 0 ) }"

  tags  {
    builtWith = "terraform"
    KubernetesCluster = "${ var.name }"
    depends-id = "${ var.depends-id }"
    Name = "${ var.name }-bastion"
    role = "bastion"
    visibility = "public"
  }

  user_data = "${ data.template_file.user-data.rendered }"

  vpc_security_group_ids = [
    "${ var.security-group-id }",
  ]
}

data "template_file" "user-data" {
  template = <<EOF
#cloud-config

---
coreos:
  update:
    reboot-strategy: etcd-lock

  etcd2:
    discovery-srv: ${ internal-tld }
    proxy: on

  units:
    - name: etcd2.service
      command: start
    - name: s3-iam-get.service
      command: start
      content: |
        [Unit]
        Description=s3-iam-get
        [Service]
        Type=oneshot
        RemainAfterExit=yes
        ExecStartPre=-/usr/bin/mkdir -p /opt/bin
        ExecStartPre=/usr/bin/curl -L -o /opt/bin/s3-iam-get \
          https://raw.githubusercontent.com/kz8s/s3-iam-get/master/s3-iam-get
        ExecStart=/usr/bin/chmod +x /opt/bin/s3-iam-get

    - name: dd-agent.service
      command: start
      runtime: true
      content: |
        [Unit]
        Description=Datadog Agent
        After=docker.service
        Requires=docker.service
        [Service]
        Environment=API_KEY=${ datadog-apikey }
        Environment=TAGS=simple-tag-0
        ExecStartPre=/usr/bin/docker pull datadog/docker-dd-agent:latest
        ExecStart=/usr/bin/docker run --name dd-agent -h %H -v /var/run/docker.sock:/var/run/docker.sock:ro -v /proc/:/host/proc/:ro -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e API_KEY=${ datadog-apikey } datadog/docker-dd-agent:latest
        ExecStop=/usr/bin/docker stop dd-agent

write-files:
  - path: /tmp/hey-look-a-file.txt
    content: |
      here's a line in this file
      here's another one
EOF

  vars {
    internal-tld = "${ var.internal-tld }"
    datadog-apikey = "${ var.datadog-apikey }"
  }
}

resource "null_resource" "dummy_dependency" {
  depends_on = [ "aws_instance.bastion" ]
}

Debug Output

gist: https://gist.github.com/seanknox/35fb07b18c3ca13b559f1f7c5bebcc5a

(I use a Makefile to run terraform; command actually executed is terraform apply)

Panic Output

No panics

Expected Behavior

An AWS EC2 instance is provisioned and bootstrapped according to supplied user-data cloud-config.

Actual Behavior

The instance is launched, but user_data doesn't appear to be executed whatsoever.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Use Terraform v0.7.11
  2. Define an aws_instance resource and supply user_data with a data template_file resource configured to write a file to the host via cloud-config
  3. terraform apply
  4. SSH to server after provisioning and see the file isn't present

Important Factoids

  • Configuration above works in Terraform v0.7.7; only difference in the breaking setup is using Terraform v0.7.11.
  • Same issue occurs when using the deprecated template_file resource to define cloud-config user data.

References

  • didn't see any
bug provideaws regression

Most helpful comment

Ping! here as well. Happened to me for an aws_launch_configuration. When I look at the tfstate I get the attribute "rendered": "" so it looks as though the rendering step doesn't happen on the template_file data source

All 8 comments

FYI, this appears to happen with v0.7.10 as well.

Ping! here as well. Happened to me for an aws_launch_configuration. When I look at the tfstate I get the attribute "rendered": "" so it looks as though the rendering step doesn't happen on the template_file data source

Unfortunately you have to use $${var} for inline templates now. I've updated the docs to state this. I'm really not sure how this stopped working but the truth is that it never should've worked from the beginning and in 0.8 is an error that we report. This will fix everything up for you!

Much thanks @mitchellh!

@mitchellh ok, thank you. how do we 'escape' strings like ${variable} to not be used by terraform though? Double $$ used to be the way in earlier versions.

Someone reported here https://groups.google.com/forum/#!topic/terraform-tool/_tXjlLbWOu8 that we should use now $$$${VAR} instead, what a joke :-)

Just following up to say Mitchell's suggestion to use $${var} did the trick—we're using a recent Terraform (v0.7.13) and inline templates are doing their thang.

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.

Was this page helpful?
0 / 5 - 0 ratings