2

I am trying to pass the PowerShell script as the file IIS.txt which is present in the CWD.

I don't see the script running on the server. I am not sure if I am missing something. Any help would be appreciated.

resource "aws_instance" "db1" {
  ami           = "ami-1234567890"  
  instance_type = "t3.small"
  subnet_id     = "${aws_subnet.db.0.id}"
  key_name      = "ireland"
  user_data     = "${file("IIS.txt")}"

  tags = {
    Name = "sql node 1"
  }
}
2
  • Could you share the directory structure in either ls -la or tree command outputs? If you're on windows, dir /s would do too. The IIS.txt file should be in the same directory where you run terraform commands. Commented Oct 3, 2019 at 19:02
  • C:\Users\%\Desktop\AWS\AWS\Running PS script through Terraform 10/03 03:47 PM <DIR> . 10/03 03:47 PM <DIR> .. 10/02 12:53 PM <DIR> .terraform 10/03 02:05 PM 133 IIS.txt 10/03 03:27 PM 1,254 Instances.tf 06/21 06:44 AM 116 Provider.tf 10/03 03:47 PM 157 terraform.tfstate 10/03 03:46 PM 12,052 terraform.tfstate.backup 10/02 10:50 AM 420 terraform.tfvars 10/02 12:58 PM 710 vars.tf 10/03 03:03 PM 1,318 vpc-subnets.tf Commented Oct 4, 2019 at 15:12

1 Answer 1

2

I've used a template_file data and local_file resource for this.

data "template_file" "user_data" {
  template = "${file("iis.txt")}"
}

resource "local_file" "user_data" {
  content  = "${data.template_file.user_data.rendered}"
  filename = "user_data-${sha1(data.template_file.user_data.rendered)}.ps"
}

Then update your user_data property content of the local_file resource.

resource "aws_instance" "db1"
{
  ami           = "ami-1234567890"  
  instance_type = "t3.small"
  subnet_id     = "${aws_subnet.db.0.id}"
  key_name      = "ireland"
  user_data     = "${local_file.user_data.content}"
  tags =
  {
    Name = "sql node 1"
  }
}

This also allows you to get a little fancier and do a template script, and pull TF variables, etc into the template and render it just in time before you deploy.

Sign up to request clarification or add additional context in comments.

3 Comments

Note if this doesn't work you may need to share some of the contents of your script file "iis.txt". There are requirements for how user_data is formatted for it to actually be executed as a script.
IIS.txt <<-EOF <powershell> Install-WindowsFeature -name Web-Server -IncludemanagementTools </powershell> EOF @Dude0001 I will test your solution
Is the heredoc/EOF stuff literal characters in the .txt? If so I think you need to take them out. The file should start with <poweshell> and end with <\powershell> docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.