1

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"}"))

}

2 Answers 2

2

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.

Reference: Terraform > aws_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>
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

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.