3

I have tried all troubleshooting in this question to no avail.

"UserData": {
                    "Fn::Base64": {
                        "Fn::Join": [
                            "",
                            [
                                "<powershell>\n",
                                "New-Item C:\\Temp\\test.txt -ItemType file \n",
                                "</powershell>\n",
                                "<persist>true</persist>"
                            ]
                        ]
                    }
                }

test.txt file doesnt get created in the C:\Temp folder.

C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log:

2018/01/19 02:26:55Z: Userdata execution begins
2018/01/19 02:26:55Z: Zero or more than one <persist> tag was not provided
2018/01/19 02:26:55Z: Unregistering the persist scheduled task
2018/01/19 02:27:18Z: Zero or more than one <runAsLocalSystem> tag was not provided
2018/01/19 02:27:18Z: Zero or more than one <script> tag was not provided
2018/01/19 02:27:19Z: Zero or more than one <powershellArguments> tag was not provided
2018/01/19 02:27:19Z: <powershell> tag was provided.. running powershell content
2018/01/19 07:49:57Z: Userdata execution begins
2018/01/19 07:49:57Z: Failed to get metadata: The result from http://169.254.169.254/latest/user-data was empty
2018/01/19 07:50:00Z: Unable to execute userdata: Userdata was not provided
2018/01/19 07:50:00Z: Userdata execution done

But i can see the script at http://169.254.169.254/latest/user-data

<powershell>
New-Item C:\Temp\test.txt -ItemType file 
</powershell>
<persist>true</persist>
Sean
  • 63
  • 2
  • 11

3 Answers3

0

TLDR; By running SysPrep on the instance used to create the custom AMI, my userdata scripts are now executing on first launch by Cloudformation.

Running Sysprep using the EC2Launch Settings Application:

  1. In the Amazon EC2 console, locate or create a Windows Server 2016 AMI.
  2. Launch a Windows instance from the AMI.
  3. Connect to your Windows instance and customize it.
  4. Search for and run the EC2LaunchSettings application. It is located in the following directory by default: C:\ProgramData\Amazon\EC2-Windows\Launch\Settings.
  5. Select or clear options as needed. These settings are stored in the LaunchConfig.json file. 6.Choose Shutdown with Sysprep.

After Sysprep has been configured for the Windows Server 2016 custom image the AMI can be safely created. Adding the newly created AMI to the CFN template the execution results in a success.

References:

  1. Microsoft System Preparation Tool configuration
  2. More information on SysPrep
Sean
  • 63
  • 2
  • 11
0

For the custom AMI's user data are already disabled to enable it run following command in powershell.

C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule

And then the userdata script should work.

0

Run the command and take the AMI

C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule
Jebaseelan Ravi
  • 697
  • 8
  • 22