Search code examples
amazon-web-servicesterraformbitnamicloud-init

Terraform copies but (cloud-init) doesn't run user_data script on Bitnami EC2 instance


I'm trying to have Terraform run my bash script after deploying my Bitnami EC2 instance (AMI: ami-0f185ef928bf37528). I can find the file at /var/lib/cloud/instance/scripts/part-001 but it isn't being run.

My desired script is a little more complicated but even this isn't being run:

#!/bin/sh
set -x
echo "Hello World.  The time is now $(date -R)!" | tee /root/output.txt
mkdir /opt/bitnami/projects
exit

main.tf contains

resource "aws_instance" "app_server" {
  ami             = "ami-0f185ef928bf37528"
  instance_type   = "t2.micro"
  key_name        = "app_server"
  security_groups = [aws_security_group.ec2_sg.name]
  user_data       = file("init.sh")
}

I'm not seeing any obvious issues or indicators in /var/log/cloud-init-output.log and /var/log/cloud-init.log


Solution

  • I tried to replicate your issue, but your code works perfectly:

          ___ _ _                   _
          | _ |_) |_ _ _  __ _ _ __ (_)
          | _ \ |  _| ' \/ _` | '  \| |
          |___/_|\__|_|_|\__,_|_|_|_|_|
      
      *** Welcome to the Node.js packaged by Bitnami 17.8.0-2                 ***
      *** Documentation:  https://docs.bitnami.com/aws/infrastructure/nodejs/ ***
      ***                 https://docs.bitnami.com/aws/                       ***
      *** Bitnami Forums: https://community.bitnami.com/                      ***
    bitnami@ip-172-31-37-75:~$ sudo su -
    root@ip-172-31-37-75:~# ls
    output.txt
    root@ip-172-31-37-75:~# cat output.txt 
    Hello World.  The time is now Sat, 02 Apr 2022 22:53:55 +0000!
    root@ip-172-31-37-75:~# cd /opt/bitnami/
    apache/       bncert/       common/       gonit/        node/         python/       scripts/      var/          
    apache2/      bndiagnostic/ git/          nami/         projects/     redis/        stats/        
    root@ip-172-31-37-75:~# cd /opt/bitnami/p
    projects/ python/   
    root@ip-172-31-37-75:~# cd /opt/bitnami/p
    projects/ python/   
    root@ip-172-31-37-75:~# cd /opt/bitnami/projects/
    

    Thus its possible that your real code that you are using is different then in the question.