Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add userdata for aws instance using data-source in terraform

I need to create an AWS instance with user-data using terraform. I'm using terraform 0.10.0.

ec2.tf

data "template_file" "user_data" {
  template = "userdata.tpl"
}

resource "aws_instance" "fluentd-web" {
  ami           = "${lookup(var.amis, var.aws_region)}"
  instance_type = "t2.micro"
  subnet_id = "${var.subnet_id}"
  user_data = "${data.template_file.user_data.rendered}"
  security_groups =[ "${aws_security_group.ec2_sg.id}" ]
  connection {
     ***
  }
}

userdata.tpl

#! /bin/bash

apt install <**>

On applying the terraform, It shows no error. But the contents of the file is not getting populated as user-data. It just shows the file name as user-data. AWS Console

How can I get the contents of the file as user-data?

like image 372
Neeraj Avatar asked Aug 09 '17 07:08

Neeraj


1 Answers

Set the template as below:

template = "${file("${path.module}/userdata.tpl")}"

Here we use ${path.module} to get a module-relative path.

Reference: https://www.terraform.io/docs/modules/create.html

like image 92
BMW Avatar answered Oct 18 '22 16:10

BMW