Post

AWS - EC2 with UserData

This is more of a list of questions that come up when working with EC2s and UserData scripts.

What UserData did the EC2 instance run at startup?

1
aws ec2 describe-instance-attribute --instance-id i-0fe45e27d4b6f1cc3 --attribute userData | jq -r '.UserData.Value' | base64 -d

What’s the size in bytes of UserData?

Why check for the size of UserData?

I’d like to pass UserData as a Parameter in CloudFormation. To do that, I have to pass it as a String. I prefer to have it encoded in base64, before passing it to CloudFormation. But CloudFormation sets limits on pass it String parameters, i.e., they should not exceed 4096 bytes.

I work in PowerShell as well bash. So I check the size as follows:

1
2
3
4
PS > cd ec2-scripts
PS > $UserDataScript = Get-Content -Raw ./UserData.ps1
PS > $UserDataScriptEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($UserDataScript))
PS > [System.Text.Encoding]::UTF8.GetByteCount($UserDataScriptEncoded)

or in bash

1
2
3
$ > cd ec2-scripts
$ > userdata_script_encoded=$(base64 -w 0 -i UserData.ps1)
$ > userdata_script_size=$userdata_script_encoded | wc -c

If UserData is larger than 4096 bytes, there are other more complex ways to use it, such as cfn-init. But, if I can, I’d rather avoid that route.

How can I tell that my Windows EC2 with userdata started without errors?

Look at the logs for EC2Launch as mentioned in this answer from AWS.

1
2
3
cd C:\ProgramData\Amazon\EC2Launch\log
Get-Content .\console.log
Get-Content .\agent.log

If there was an error, you’ll see such output:

1
2
3
4
5
6
7
YYYY-MM-DD 23:21:41 Info: Not running as a detached process.
YYYY-MM-DD 23:21:41 Info: Script file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch1771702063\UserScript.ps1
YYYY-MM-DD 23:21:41 Info: Error file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch1771702063\err.tmp
YYYY-MM-DD 23:21:41 Info: Output file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch1771702063\output.tmp
YYYY-MM-DD 23:25:16 Error: Script produced error output.
YYYY-MM-DD 23:25:16 Info: Stage: postReadyUserData completed.
YYYY-MM-DD 23:25:16 Info: Run StartSsm task.

After the temporary files are generated comes an important entry Error: Script produced error output. To see what failed, open the temporary error file; in the above example, that would be: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch1771702063\err.tmp

If the userdata script ran to completion without errors, you’ll see the output below: note that there’s no entry that states Error: Script produced error output.

1
2
3
4
5
6
7
8
9
10
YYYY-MM-DD 23:15:59 Info: Not running as a detached process.
YYYY-MM-DD 23:15:59 Info: Script file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4065378474\UserScript.ps1
YYYY-MM-DD 23:15:59 Info: Error file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4065378474\err.tmp
YYYY-MM-DD 23:15:59 Info: Output file is created at: C:\Windows\system32\config\systemprofile\AppData\Local\Temp\EC2Launch4065378474\output.tmp
YYYY-MM-DD 23:19:19 Info: Script execution finished successfully.
YYYY-MM-DD 23:19:19 Info: Stage: postReadyUserData completed.
YYYY-MM-DD 23:19:19 Info: Run StartSsm task.
YYYY-MM-DD 23:19:20 Info: AmazonSSMAgent is not in running state, sleep for 2 seconds.
YYYY-MM-DD 23:19:22 Info: AmazonSSMAgent is running now.
YYYY-MM-DD 23:19:22 Info: Stage: postReady completed.

Where do I find cfn-init logs on Windows EC2 instances?

According to https://repost.aws/knowledge-center/cloudformation-helper-scripts-windows, the logs is located at C:\cfn\log\cfn-init.log. It’s output is similar to the userdata logs above.

As for Linux, the logs are, thanks to a stackover answer, stored as follows:

  • /var/log/cfn-init-cmd.log: cfn-init and command output with timestamps.
  • /var/log/cfn-init.log: cfn-init and command output.
  • /var/log/cloud-init.log: `cloud init logs pertaining to running the commands in user-data.
This post is licensed under CC BY 4.0 by the author.