Home > front end >  Issues with Userdata in Terraform
Issues with Userdata in Terraform

Time:03-30

I'm trying to pass user data over a file so the code will look less clumsy, but having trouble. I've tried all the different combinations and spent hours on this but nothing is working.

I went through the terraform documentation, it doesn't give any special instructions for the path value.

folder structure:

project1/env/dr/compute/main.tf

`module "share_server" {
count                       = 2
source                      = "../../../../terraform_modules/modules/compute/"
ami                         = data.aws_ami.amazonlinux2.id
instance_type               = "t3.micro"
availability_zone           = data.aws_availability_zones.az.names[count.index]
subnet_id                   = data.aws_subnets.app_subnet.ids[count.index]
associate_public_ip_address = "false"
key_name                    = "app7"
vpc_security_group_ids    = ["sg-08d38198dc153c410"]
instance_root_device_size = 20
kms_key_id                = "ea88e727-e506-4530-b92f-2827d8f9c94e"
volume_type               = "gp3"
platform                  = "linux"
backup                    = true
Environment               = "dr"
server_role               = "Application_Server"
server_component          = "share_servers"
hostname = "app-dr-test-10"

tags = {
Name = "${local.instance_name}-share-${count.index}"
}
}`

My ec2 module resides in the below folder structure.

project1/modules/compute/ec2.tf

project1/modules/computer/userdata/share_userdata.tpl

ec2.tf code below. I have removed the bottom half of the code so the post won't be too big to read.

resource "aws_instance" "ec2" {
  ami                         = var.ami   
  instance_type               = var.instance_type   
  availability_zone           = var.availability_zone   
  subnet_id                   = var.subnet_id   
associate_public_ip_address = var.associate_public_ip_address   
user_data = templatefile("userdata/share_userdata.tpl",     
{       hostname = var.hostname   }
)

error:

PS B:\PubOps\app7_terraform\environments\dr\compute> terraform apply 
│ Error: Invalid function argument │ 
│   on ..\..\..\..\terraform_modules\modules\compute\main.tf line 10, in resource "aws_instance" "ec2":
│ 
  10:   user_data = templatefile("userdata/share_userdata.tpl", 
│   11:     { 
│   12:       hostname = var.hostname 
│   13:   }) 
│ 
│ Invalid value for "path" parameter: no file exists at userdata/share_userdata.tpl; this function works only with files that are distributed as part of the       
│ configuration source code, so if this file will be created by a resource in this configuration you must instead obtain this result from an attribute of that    
│ resource. ╵ PS B:\PubOps\alfresco7_terraform\environments\dr\compute>

User data

` #!/bin/bash yum update -y

hostname

sudo hostnamectl set-hostname $hostname echo "127.0.0.1 $hostname $hostname localhost4 localhost4.localdomain4" > /etc/hosts echo "preserve_hostname: true" >> /etc/cloud/cloud.cfg

#EFS utility and mounting yum install -y amazon-efs-utils EOF `

References:

The same code mentioned below on Github, maybe it is working for the author, but not for me

https://github.com/kunduso/ec2-userdata-terraform/blob/add-userdata/ec2.tf

My goal is to set up user data and pass variables over the AWS parameter store as shown in the below URL, but I couldn't pass the basic setup.

https://skundunotes.com/2021/11/17/manage-sensitive-variables-in-aws-ec2-user-data-with-terraform/

Stuck on this from last Thursday, any help will be greatly appreciated.

I tried pointing the file like this -> ./share_userdata.tpl

Tried with absolute path b/project1/dr/compute/share_userdata.tpl

I also tried giving $module.path/share_userdata.tpl

None of them worked.

CodePudding user response:

The error is rather clear:

no file exists at userdata/share_userdata.tpl

Thus you must ensure that in the folder where you execute templatefile("userdata/share_userdata.tpl" there is a subfolder called userdata and in that folder there is a file share_userdata.tpl.

CodePudding user response:

you need to pass the full or relative path in your templatefile. you can try below code for relative path eg:

resource "aws_instance" "ec2" {
  ami                         = var.ami   
  instance_type               = var.instance_type   
  availability_zone           = var.availability_zone   
  subnet_id                   = var.subnet_id   
associate_public_ip_address = var.associate_public_ip_address   
user_data = templatefile("./userdata/share_userdata.tpl",     
{       hostname = var.hostname   }
)

Here ./ indicating current folder.

Assuming the ec2.tf and userdata folder exist in same path. for eg: project1/modules/compute/.

  • Related