1. Use ebextentions. Here’s an example.

2. Here’s the Powershell script. You’ll need to zip up the script and upload it to S3 per the ebextentions config file above.
$datetimef = get-date -format yy.MM.dd.HH.mm
$log = 'C:\ebextentions\datadog\install-datadog-agent-log.txt'
Write-Output ("$datetimef START install-datadog-agent.ps1") | Out-File $log -Append
$agentInstalled = sc.exe query | findstr DatadogAgent
if (!($agentInstalled)) {
Write-Output ("$datetimef Datadog Agent service not detected, run installation") | Out-File $log -Append
Write-Output ("$datetimef Source for install-datadog-agent.ps1 at DevOps.Aws\scriptsRun\utilities") | Out-File $log -Append
$instanceId = Invoke-Restmethod -uri http://169.254.169.254/latest/meta-data/instance-id
Write-Output ("$datetimef Assign InstanceId `"$instanceId`" as Datadog hostname") | Out-File $log -Append
msiexec.exe /qn /i "C:\ebextentions\datadog\ddagent-cli.amd64.msi" APIKEY="######################" HOSTNAME="$instanceId" /Lp+ $log | Out-Null
# check that agent service is now installed
$agentInstalled = sc.exe query | findstr DatadogAgent
if (!($agentInstalled)) {
Write-Output ("$datetimef Datadog Agent service not detected, run installation") | Out-File $log -Append
Write-Output ("$datetimef") | Out-File $log -Append
Throw ("$datetimef Datadog agent install failed") | Out-File $log -Append
} else {
Write-Output ("$datetimef Datadog agent installed successfully!") | Out-File $log -Append
}
} else {
Write-Output ("$datetimef Agent already exists, skipping install") | Out-File $log -Append
}
Write-Output ("$datetimef END install-datadog-agent.ps1") | Out-File $log -Append
Write-Output (" ") | Out-File $log -Append