Create a Windows instance with Name tag containing current datetime and user data that will download a file from Amazon S3 by using PowerShell.

– create_instance.sh

#!/bin/bash

DATE=`date +"%y%m%d_%H%M%S"`

TAGS="ResourceType=instance,Tags=[{Key=Name,Value=server_${DATE}},{Key=Managed,Value=''}] ResourceType=volume,Tags=[{Key=Name,Value=server_${DATE}}]"

instanceId=`aws ec2 run-instances --iam-instance-profile Name=CodeDeployS3Role --image-id ami-12345678 --count 1 --instance-type t2.micro --key-name server-key --security-group-ids sg-12345678 --subnet-id subnet-12345678 --associate-public-ip-address --user-data file:///home/ec2-user/user_data.txt --tag-specifications ${TAGS} --query 'Instances[0].InstanceId' --output text`

echo $instanceId

aws elb register-instances-with-load-balancer --load-balancer-name server-lb --instances $instanceId

– user_data.txt

<powershell>
net stop DatadogAgent

$localIp = (Invoke-RestMethod -Method Get -Uri http://169.254.169.254/latest/meta-data/local-ipv4)
$externalIp = (Invoke-RestMethod -Method Get -Uri  http://169.254.169.254/latest/meta-data/public-ipv4)
$hostname = (Invoke-RestMethod -Method Get -Uri http://169.254.169.254/latest/meta-data/local-hostname)

echo $localIp | Out-File -FilePath c:\ip_new.txt -Encoding ASCII
echo $externalIp | Out-File -FilePath c:\ip_new.txt -Encoding ASCII -Append

mkdir C:\deploy

Read-S3Object -AccessKey ACCESS_KEY -SecretKey SECRET_KEY -Region us-east-1 -BucketName BUCKET_NAME -Key dist/file.zip -File C:\deploy\latest.zip

& "C:\Program Files\Java\jdk1.8.0_131\bin\jar.exe" xf C:\deploy\latest.zip

Remove-Item -Force -Recurse C:\Bin\Release
Remove-Item -Force -Recurse C:\Bin\Support
Copy-Item -Force -Recurse C:\deploy\Release -Destination C:\Bin\Release
Copy-Item -Force -Recurse C:\deploy\Support -Destination C:\Bin\Support

Remove-Item -Force -Recurse C:\deploy

(Get-Content C:\ProgramData\Datadog\datadog.conf).replace('[HOSTNAME]', $hostname) | Set-Content C:\ProgramData\Datadog\datadog.conf
(Get-Content C:\ProgramData\Datadog\conf.d\tcp_check.yaml).replace('[HOSTNAME]', $hostname) | Set-Content C:\ProgramData\Datadog\conf.d\tcp_check.yaml

net start DatadogAgent
</powershell>