AWS user_data file type not provisioning

I am trying to create multiple ec2 instances based on a configuration using terraform.

resource "aws_instance" "collatz_instance" {
  for_each = { for config in var.instance_configuration : config.name => config }

  ami                    = each.value.ami
  key_name               = local.prefix
  instance_type          = "m6g.xlarge"
  vpc_security_group_ids = [aws_security_group.security_group.id]
  subnet_id              = aws_subnet.public_subnet.id
  user_data              = file("${each.value.script_path}")


  tags = merge(
    { "Name" = format("%s", "${each.value.name}-net-ec2-public") },
    local.tags,
  )
}

Here the user_data provision does not seem to be working

instance_configuration = [
    {
        ami: "ami-XXXXXX",
        name: "parithiban"
        script_path: "scripts/parithiban.sh"
    },
    {
        ami: "ami-XXXXXXX",
        name: "parithiban-another"
        script_path: "scripts/parithiban_one.sh"
    }
]

Name and ami is working perfectly only user_data is not working. Once the instance is launched i checked in Instance settings/Edit user data. I can see the same script there. I couldn’t find any errors too. I can see the same txt file in /var/lib/cloud/instance/.

My terrraform version is v1.0.10

Could you confirm that the script contents are different?

Hi @tbugfinder

Thanks for your reply. Not everything is different. Some logic might be common

For EX:

wget https://go.dev/dl/go1.17.5.linux-arm64.tar.gz
rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.17.5.linux-arm64.tar.gz
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile

In both instances script, I will be installing go. And then in addition to it, I will be adding additional commands.

Also, should the contents need to be different in both instances?

Even I replaced the second configuration path with the first one. Still, it is not provisioning.

As an alternative i used remote-exec this is working perfectly. Not sure why user_data not working

connection {
    type     = "ssh"
    user     = "ubuntu"
    timeout     = "1m"
    private_key = "${tls_private_key.collatz_key.private_key_pem}"
    host     = self.public_dns
  }

  provisioner "file" {
    source      = "scripts/${each.value.script_path}"
    destination = "/tmp/${each.value.script_path}"
  }

  provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/${each.value.script_path}",
      "/tmp/${each.value.script_path}",
    ]
  }

In order to debug it, you could add tags for script path and maybe sha256 sum of it, like

tags = merge(
    { "Name" = format("%s", "${each.value.name}-net-ec2-public") },
    { "ScriptPath" = "${each.value.script_path}",
    { "ScriptSHA256" = filesha256(each.value.script_path)},
    local.tags,
  )