Amit
Amit

Reputation: 67

Terraform template is not rendering data

This code use to work on terraform 0.11 without any issue. I used it earlier many times to to create AWS EC2 instance.

Below are the contents of /data/CreateBasionHost.tf file

data "template_file" "BasionHost_data" {
       template             = "${file("${path.module}/BasionHost.tpl")}"
}

resource "aws_instance" "BasionHost" {
  depends_on                  = ["aws_vpc_dhcp_options_association.dns_resolver"]
  depends_on                  = ["aws_directory_service_directory.MyActiveDirectory"]
  depends_on                  = ["aws_vpc_dhcp_options.DhcpOptionforAD"]
  depends_on                  = ["aws_iam_instance_profile.BackupInst_profile"]
 
  ami                         = "${var.CENTOS7_CUSTOMIZED_AMI}"
  instance_type               = "${var.NAT_INST_TYPE}"
  iam_instance_profile        = "${aws_iam_instance_profile.BackupInst_profile.name}"
  associate_public_ip_address = "true"
  source_dest_check           = "false" 
  disable_api_termination     = "false"
  subnet_id                   = "${aws_subnet.PublicSubnetB.id}"
  availability_zone           = "${var.AWS_REGION}b"
  vpc_security_group_ids      = ["${aws_default_security_group.default.id}"]
  key_name                    = "${var.NAT_INST_KEY_NAME}"
  private_ip                  = "${var.BASIONHOST_PRIVATE_IP}"
  user_data                   = "${data.template_file.BasionHost_data.rendered}"

}

And I use to pass pain yum command so that Linux server get updated. Below is contents of /data/BasionHost.tpl

#! /bin/bash
yum update -y
ln -s /usr/bin/clear /usr/bin/cls

But now this code is not working with terraform 0.14. I tried to read this [Terraform link] (https://www.terraform.io/docs/configuration/functions/templatefile.html?_ga=2.248470019.549632933.1609205994-820700225.1609205994)

It is mentioned to terraform website to use templatefile Function. But I am not able to achieve the same , examples are not very clear. Can someone please guide me ?

Upvotes: 1

Views: 728

Answers (1)

Marcin
Marcin

Reputation: 238081

I tried to replicate the issue using official CentOS 7 AMI using Terraform v0.14.3. There were no issues due totemplate_file. Instead in my tests the issue was cause by space in #! /bin/bash which lead to a user data not executing. So the solution was to remove it:

#!/bin/bash
yum update -y
ln -s /usr/bin/clear /usr/bin/cls

Upvotes: 1

Related Questions