How to pass both file and script in user_data?

Hello all,
My requirement is to bring up a ec2 instance and install nginx. I passed the series of commands to install nginx using the user_data. Now, I need to add some configuration to the nginx conf. For that, I need to upload a file using user_data and place it under /etc/nginx/sites-available path. How do I achieve that. I have searched and couldn’t find a example that uses both passing a file and script.
Any help will be appreciated.
Please let me know if any other input needed from my side to analyze. I am stuck here

You can either inline the file:

  user_data = <<-EOF
              #!/bin/bash
              apt-get update
              apt-get install -y nginx
              cat << 'EOT' > /etc/nginx/sites-available/mycustomsite.conf
              server {
                  listen 80;
                  server_name mydomain.com;
  
                  location / {
                      root /var/www/html;
                      index index.html index.htm;
                  }
              }
              EOT
              ln -s /etc/nginx/sites-available/mycustomsite.conf /etc/nginx/sites-enabled/
              rm -f /etc/nginx/sites-enabled/default
              systemctl restart nginx
              EOF

Or use the file and remote_exec provisioners:

  provisioner "file" {
    source      = "path/to/mycustomsite.conf"
    destination = "/tmp/mycustomsite.conf"
  }

  provisioner "remote-exec" {
    inline = [
      "mv /tmp/mycustomsite.conf /etc/nginx/sites-available/mycustomsite.conf",
      "ln -s /etc/nginx/sites-available/mycustomsite.conf /etc/nginx/sites-enabled/",
      "rm -f /etc/nginx/sites-enabled/default",
      "systemctl restart nginx"
    ]
  }

I found this in terraform documentation

user_data = "${file("user-data-apache.sh")}"

Can we append the shell commands with this? If yes, how to place the file in the required directory and where the file gets uploaded in the machine.
Just want to know the ways things can be done. I have tried using provisioner and faced error in reading the private key. I am using ubuntu and .pem key. Format might be the problem.

For the inline version yes, it would be the same.

I tried this.

  user_data       = <<EOF
#!/bin/bash
sudo apt update -y
sudo apt install -y nginx
"${file("default")}"
EOF

When i checked the userdata from the aws console, file content was populated. That’s not our desired output. How to place this file in a specified directory in the server? Thanks for your help so far.

As mentioned before you need to use the file provisioner.

Thanks a lot. BTW, I found the below configuration working.

echo -e "${file("default")}" > /tmp/new.txt