AWS: Terraform file Upload / Execute Command

I need to upload the file (.zip and .exe) and execute PowerShell commands on the ec2 instances while building it using terraform.

I have a base image (server 2012) where I am making all the changes using terraform. All instances are connected to the internal AWS network.

I tried " data “template_file” "userdata " it is only working with sysprep AMI and not working with the normal AMI which is not built using sysprep.

Also, all the instances are not exposed to the internet hence, I cannot use terraform remote_exec or other solutions which require internet. I can’t expose them.

I tried many solutions but nothing working. Currently i am running commands on sysprep instance as shown below:

data "template_file" "userdata" {
  template = "<powershell>${file("setup.ps1")}</powershell><persist>false</persist>"
}

In the script, I am running PSSession commands to make changes to the instances.

Problem:

  1. How to upload files using terraform?

  2. How to run commands on the non-sysprep images.

Hi @rockie

Take a look at File Provisioner

:warning: As the documentation says “Provisioners should only be used as a last resort”. I suggest that you look at Ansible, a complementary tool for software configuration.

I have a tutorial explaining how to use Terraform and Ansible Together.

Some people like to configure each machine to boot and execute Ansible, that is certainly great because the machine or image can use its tags to know its function and configure those Ansible roles/tags automatically.