Home > OS >  Using Terraform user_data with Windows Powershell
Using Terraform user_data with Windows Powershell

Time:02-19

I am trying to create Windows EC2 instance, and I want to change its hostname immediately after creation. I was trying to do it with user_data, but it looks like never gets executed. Does anyone know how can I do this?

Script:

<powershell>
Rename-Computer -NewName "Server044"
Restart-Computer
</powershell>

Terraform Code:


resource "aws_instance" "web" {
.
.

user_data = base64encode(file("${"host.ps1"}"))

}

CodePudding user response:

Looking at the modules I work with, we don't use base64encode() for powershell userdata. I think you try dropping that, or else look into the user_data_base64 argument.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#user_data

Also, remove the unnecessary string interpolation.

user_data = file("host.ps1")

You may also try adding the -Force parameter to your Rename-Computer cmdlet so it suppresses the confirmation prompt, and -Restart to do it with a single command.

<powershell>
Rename-Computer -NewName "Server044" -Force -Restart
</powershell>

CodePudding user response:

In the end, I changed the script file to be .txt, and then just used powershell tags inside. That worked for me

  • Related