AWS/User data

From Ever changing code
Jump to navigation Jump to search

User_data scripts have many derivatives depends on operating system, and following do parse the script:

  • Linux - uses cloud_init
  • Windows 2012 - ec2config
  • Windows 2016< - ec2launch

Retrieve use_data script

  1. PS1 C:> Invoke-RestMethod -uri ; never worked
  2. in a console, select instance, Instance Settings > View/Change User Data


EC2Launch is a set of Windows PowerShell scripts that replaces the EC2Config service on Windows Server 2016 and later AMIs.

Configuration file


Log files are in C:\ProgramData\Amazon\EC2-Windows\Launch\Log

*Ec2Launch.txt - the app itself logs
*UserDataExecution.txt - contains parsing details and the script output
*WallpaperSetup.txt -

Script parsing sections:

<script>cmd://batch commands</script>  #always is parsed 1st
<powershell></powershell>              #2nd
<persist>true</persist>               #by default is false, true will run on each reboot

Example user_data.tpl script that will initialise 1st attached volume

$file = @"
select disk 1
attributes disk clear readonly
online disk
convert mbr
create partition primary
format quick fs=ntfs label="log"
assign letter="d"
Set-Content -Path ${env:TEMP}\diskpart -Value $file
diskpart.exe /s ${env:TEMP}\diskpart

Initialise disks using EC2Launch script, it will set a new attached disk with Active flag


Logs and important files

#User script content on Windows
Get-Content -path C:\Windows\TEMP\UserScript.ps1

Get-Content -path C:\programdata\Amazon\EC2-Windows\Launch\Log\UserData.log -Wait  #custom file log
Get-Content -path C:\programdata\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log -Wait
Get-Content -path C:\programdata\Amazon\EC2-Windows\Launch\Scripts\TentacleRegisterScheduleTask.log -Wait #custom file log
Get-Content -path C:\Program Files\SplunkUniversalForwarder\var\log\splunk\splunkd.log -wait

Get-Content -path C:\ProgramData\chocolatey\logs\choco.summary.log | more
Get-Content C:\ProgramData\chocolatey\logs\chocolatey.log |more