'Multipart cloud init for windows on AWS

I have a small question about cloud config again; this time on Windows. I’m creating an EC2 instance with windows 2019.

Here is how I do:

resource "aws_instance" "ec2" {
  ami       = data.aws_ami.windows_ami.id # Windows_Server-2019-English-Full-Base 
...
  user_data = data.template_cloudinit_config.user_data.rendered
...
}

During the provisioning, I put a user_data and it's basically a powershell script that I load from a file that looks like this:

<powershell>
Set-PSDebug -Trace 1

echo "Hello World" | Set-Content -Path c:\hello.txt

</powershell>
<runAsLocalSystem>true</runAsLocalSystem>
<persist>false</persist>

When I have ONE part in my template_cloudinit_config it works fine. I can go to %Programdata%\Amazon\EC2... logs and check the execution. And the file is properly created on C:

However, when I have two parts in my cloudinit, then it doesn't execute.

data "template_cloudinit_config" "user_data" {

  gzip          = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = data.template_file.config.rendered
  }

  part {
    content_type = "text/x-shellscript"
    content      = data.template_file.config2.rendered
  }
}

When I have a multi part, nothing in logs. If I have one part, it gets properly executed. It's the same powershell script in both case. The only difference is the name of the file that is created.

I was wondering if anyone has ever do something like this? and can help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source