Terraform code in AWS deploys instance but not not running bash script

Terraform code in AWS deploys the instance but not not running bash script. Script runs updates, deploys apache, creates small page etc. Everything works and instances is running. When connected to instance, there is no /var/www folder, so this tells me this is not being run. Manually entering the commands works, just not with script.
Any assistance is appreciated!

Here are my files…
c1-versions.tf

Terraform Block

terraform {
  required_version = "~> 1.7" # which means any version equal & above 0.14 like 0.15, 0.16 etc and < 1.xx
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 5.38"
    }
  } 
}  

Provider Block

provider "aws" {
  region = "us-east-1"
}

/*
Note-1:  AWS Credentials Profile (profile = "default") configured on your local desktop terminal  
$HOME/.aws/credentials
*/

c2-ec2instance.tf

Resource: EC2 Instance

resource "aws_instance" "myec2vm" {
  ami = "ami-0742b4e673072066f"
  instance_type = "t3.micro"
  user_data = file("${path.module}/app1-install.sh")
  tags = {
    "Name" = "EC2 Demo"
  }
}

app1-install.sh

#! /bin/bash

# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html

sudo yum update -y

sudo yum install -y httpd

sudo systemctl enable httpd

sudo service httpd start

sudo echo '<h1>Welcome to Terraform Training - APP-1</h1>' | sudo tee /var/www/html/index.html

sudo mkdir /var/www/html/app1

sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);"> <h1>Welcome to Stack Simplify - APP-1</h1> <p>Terraform Demo</p> <p>Application Version: V1</p> </body></html>' | sudo tee /var/www/html/app1/index.html

#sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

sudo curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html

AWS Documentation to retrieve EC2 Instance Data

Retrieve instance metadata - Amazon Elastic Compute Cloud

Is there something wrong in this file?

Hi @support2,

From some web searching I learned that ami-0742b4e673072066f is an Amazon Linux 2 AMI, and Amazon Linux 2 uses cloud-init for system initialization, so your user_data content will be interpreted by cloud-init.

This is unfortunately more a cloud-init problem than a Terraform problem, since Terraform’s responsibility here is only to send the script you wrote to the AWS API. The script execution is done by cloud-init when your instance is booting.

Therefore you might get some more information by following the guide How to debug cloud-init.

Folks in this forum might still be able to offer suggestions about cloud-init, but since this is not really a Terraform-specific problem it might help to also ask this question in a cloud-init-specific forum, or in a more general forum where participants are more likely to be familiar with cloud-init, such as DevOps StackExchange.

Hey Apparentlymart,

From reading, am I understanding that ALL AWS linux AMIs use cloud-init. Reason I ask, person I’m taking the course with says “this code works”.
I’ve changed the AMI ID to another AWS linux destro but still fails.
Seems to me this would defeat the purpose if every Linux AMI forced cloud-init, then everything has to be manually installed?
I have not read How to debug cloud-init.
I did post the question to DevOps Stackexchange though.

Thanks

Cloud-init is the de-facto standard used by most Linux distributions, but it is technically possible to use other software to handle initialization, and some more specialized AMIs do so. This user_data mechanism is really just a network API that any software in your VM can access, but it’s conventional in general-purpose Linux distributions to run cloud-init during boot, and for cloud-init to fetch this data and act on it.

Unless you build your own AMI or you choose an image that was built for a more specialist purpose, it’s likely that most third-party Linux AMIs (but not necessarily other platforms) will use cloud-init.

DevOps StackExchange had no replies to my post.
Crap, I’m on first lab of this Terraform course and can’t get the first lab completed. Makes me feel like I’m going to have other issues.
Being new to Terraform I know I’ll most likely never figure out how cloud-init works in conjunction with Terraform.
Twiddling thumbs