Reputation: 34079
I am creating a windows EC2 instance using Terraform. I am setting powershell script to user_data that will run after the instance is launched.
resource "aws_instance" "windows_runner" {
ami = var.ami_id
instance_type = var.instance_type
iam_instance_profile = aws_iam_instance_profile.runner_instance.name
key_name = var.key_name
security_groups = [aws_security_group.windows.id]
subnet_id = data.aws_subnet.work.id
tags = var.tags
user_data = teamplatefile("userdata.tftpl",
{
environment_name = var.environment_name,
instance_type = var.instance_type
})
}
So I am passing these two variables to templatefile
function, and I want to consume those two variables in powershell script. Based on the [documentation][1]
The "vars" argument must be a map. Within the template file, each of the keys in the map is available as a variable for interpolation
Here is how userdata.tftpl will consume those variables
<powershell>
Set-Location -Path "C:\GitLab-Runner"
$token = "sometoken"
$url = "https://gitlab-instance.domain.com/"
$runner_name = "windows-runner-$instance_type"
$executor = "shell"
$shell = "powershell"
$builds_location = "c:\builds"
$tags = "$environment_name-windows-$instance_type"
New-Item -Path $builds_location -ItemType Directory -ErrorAction SilentlyContinue
Write-Host "$builds_location has been created!"
# register runner. Trailing backtick to span on multiple line. White space matters
.\gitlab-runner.exe register `
--non-interactive `
--url $url `
--registration-token $token `
--name $runner_name `
--executor $executor `
--shell $shell `
--builds-dir $builds_location `
--tag-list $tags `
--locked="false"
.\gitlab-runner.exe install
.\gitlab-runner.exe start
</powershell>
Questions
1> How these variables will be available in powershell?
2> Can I simply use $environment_name
and $instance_type
in PS script?
3> or will these variables passed as parameters to the PS script? In this case do I need to define and match the parameter names or order?
4>Will the PS script execute in Adminstyrator mode on launched instance?
Bonus
I found examples using templatefile on linux and shell script. Not finding a good example with windows and powershell
[1]: https://developer.hashicorp.com/terraform/language/functions/templatefile
Upvotes: 1
Views: 1447