EC2 userdata script not working on custom AMI created using Windows 2022 core base AMI

0

Hello,

I am using an AMI image created from the Windows Server 2022 Core Base AMI to launch an EC2 instance. I have made no changes to the original AWS AMI. I launched a new instance out of the original AWS provided AMI, and created an image out of it. Eventually, I will have to use my own AMI image which will have my project related software pre-loaded. I have a requirement to run few commands once on instance launch. Before I get to my project specific use case, I wanted to try the basic example provided in the documentation. I am following the simple example given in this documentation page: Neither of

<powershell> $file = $env:SystemRoot + "\Temp\" + (Get-Date).ToString("MM-dd-yy-hh-mm") New-Item $file -ItemType file </powershell>

or

<script> echo Current date and time >> %SystemRoot%\Temp\test.log echo %DATE% %TIME% >> %SystemRoot%\Temp\test.log </script>

is working. When I log into the launched instance, I do not see the file created. I am able to run these commands without any issues when I login to the ec2 instance (logging in as local Administrator). Also, when I use the original AWS provided AMI, the userdata commands do in fact work. What could be the issue?

Here is the log output from the C:\ProgramData\Amazon\EC2Launch\log\agent.log file:

2023-08-29 20:31:14 Info: Run set wallpaper task.

2023-08-29 20:31:14 Info: Wallpaper path: C:\ProgramData\Amazon\EC2Launch\wallpaper\Ec2Wallpaper.jpg

2023-08-29 20:31:14 Info: Wallpaper attributes: hostName,instanceId,privateIpAddress,publicIpAddress,instanceSize,availabilityZone,architecture,memory,network

2023-08-29 20:31:14 Info: Default user startup folder path: C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

2023-08-29 20:31:14 Info: Stage: preReady completed.

2023-08-29 20:31:14 Console: EC2LaunchTelemetry: IsTelemetryEnabled=true

2023-08-29 20:31:14 Console: EC2LaunchTelemetry: AgentOsArch=windows_amd64

2023-08-29 20:31:14 Console: EC2LaunchTelemetry: IsAgentScheduledPerBoot=true

2023-08-29 20:31:14 Console: EC2LaunchTelemetry: AgentCommandErrorCode=0

2023-08-29 20:31:14 Info: Getting user data

2023-08-29 20:31:14 Info: Try parsing user data in YAML format.

2023-08-29 20:31:14 Info: Parsing failed, fall back to XML format.

2023-08-29 20:31:14 Info: Converting user data to YAML format.

2023-08-29 20:31:14 Info: Frequency is set to: once

2023-08-29 20:31:14 Info: Failed to log 'IsUserDataScheduledPerBoot' telemetry: global telemetry logger not initialized

2023-08-29 20:31:14 Info: Failed to log 'XmlUserDataUsingBlockingAgentCommandProbabilityLevel' telemetry: global telemetry logger not initialized

2023-08-29 20:31:14 Info: Run as user is set to: 'admin'.

2023-08-29 20:31:14 Info: Script is set to run as an inline process.

2023-08-29 20:31:14 Info: Script content detected

2023-08-29 20:31:14 Info: PowerShell content detected

2023-08-29 20:31:14 Info: User-data conversion completed.

2023-08-29 20:31:14 Console: User data format: xml

2023-08-29 20:31:14 Console: Message: Windows is Ready to use

2023-08-29 20:31:14 Info: Initialize user-data state.

2023-08-29 20:31:14 Info: User-data state initialized successfully.

2023-08-29 20:31:14 Warning: Skipping task postReadyUserData-executeScript-0

2023-08-29 20:31:14 Warning: Skipping task postReadyUserData-executeScript-1

2023-08-29 20:31:14 Info: Stage: postReadyUserData completed.

2023-08-29 20:31:14 Info: Run StartSsm task.

2023-08-29 20:31:14 Info: AmazonSSMAgent service already running.

2023-08-29 20:31:14 Info: AmazonSSMAgent is running now.

2023-08-29 20:31:14 Info: Stage: postReady completed.

2023-08-29 20:31:14 Info: Run-once already exists: C:\ProgramData\Amazon\EC2Launch\state.run-once

2023-08-29 20:31:14 Info: Replace C:\ProgramData\Amazon\EC2Launch\state\state.json with C:\ProgramData\Amazon\EC2Launch\state\previous-state.json

2023-08-29 20:31:15 Info: Success: C:\ProgramData\Amazon\EC2Launch\state\previous-state.json replaced C:\ProgramData\Amazon\EC2Launch\state\state.json

2023-08-29 20:31:15 Info: EC2Launch stopped

AWS
asked 8 months ago1256 views
4 Answers
0
Accepted Answer

The issue has been resolved.

I didn't understand the "shutdown with ec2launch v2 sysprep".

This is what I had to do: On the original windows instance, from the c:\progrm files\Amazon\ec2launch directory, I ran the following command: .\ec2launch.exe sysprep -s

This ran some preparatory steps and shutdown the ec2 windows instance.

I then created an new image and tried the userdata, and it worked!

AWS
answered 8 months ago
profile picture
EXPERT
reviewed 8 months ago
0

Apologies, I missed an aspect of my problem description. I have updated the question with the revised info.

AWS
answered 8 months ago
0

Hello.

The user data for the Windows instance is run using something called EC2Launch V2.
We thought that in order to run user data on a custom AMI, it might be necessary to stop the EC2 at the time of AMI creation.
https://repost.aws/knowledge-center/ec2-windows-run-command-new

Important: If you launch an instance from a custom AMI, then you must shut down the original instance that created the AMI. To do this, use EC2Launch, EC2Launch V2, or EC2Config. From the EC2Launch, EC2Launch V2, or EC2Config settings, choose Shutdown with Sysprep or Shutdown without Sysprep.

profile picture
EXPERT
answered 8 months ago
0

Hello,

Thanks for the above answer. I did do a shutdown of the main EC2 instance before creating the AMI image. The problem still occurs.

=> Here is some additional information that may give clues perhaps.

As noted in my post description above, the userdata script works when using the original AWS provided AMI. When I look at the C:\ProgramData\Amazon\EC2Launch\log\agent.log file for this successful scenario, I see the following lines:

2023-08-29 22:33:19 Info: Initialize user-data state.

2023-08-29 22:33:19 Info: User-data state initialized successfully.

2023-08-29 22:33:19 Info: Start script.

2023-08-29 22:33:19 Info: Frequency: once

2023-08-29 22:33:19 Info: Type: batch

2023-08-29 22:33:19 Info: Run as: admin

2023-08-29 22:33:19 Info: Not running as a detached process.

2023-08-29 22:33:19 Info: Script file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\UserScript.bat

2023-08-29 22:33:19 Info: Error file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\err.tmp

2023-08-29 22:33:19 Info: Output file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\output.tmp

2023-08-29 22:33:20 Info: Script execution finished successfully.

=> The audit.log complete output for the custom AMI is in my the post description above. For Custom AMI, I see the following line:

2023-08-29 20:31:14 Info: Script content detected

=> But, lines similar to the following as missing:

2023-08-29 22:33:19 Info: Script file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\UserScript.bat

2023-08-29 22:33:19 Info: Error file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\err.tmp

2023-08-29 22:33:19 Info: Output file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4107145055\output.tmp

2023-08-29 22:33:20 Info: Script execution finished successfully.

=> Does this give any further clue? It appears that the with custom AMI, the userdata script is recognized, but it is not executing.

AWS
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions