In your JSON or YAML template, change UserData to cfn-init and specify the ascending configset.
JSON example:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "cfn-init example using configsets",
"Parameters": {
"AMI": {
"Type": "AWS::EC2::Image::Id"
}
},
"Resources": {
"WindowsInstance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"ascending": [
"config1",
"config2",
"config3"
]
},
"config1": {
"files": {
"C:\\setup\\setenvironment.ps1": {
"content": {
"Fn::Join": [
"",
[
"$Folder = 'C:\\Program Files\\Server\\packages\\bin.20182.18.0826.0815\\'\n",
"$OldPath = [System.Environment]::GetEnvironmentVariable('path')\n",
"$NewPath = $OldPath + ';' + $Folder\n",
"[System.Environment]::SetEnvironmentVariable('path',$NewPath,'Machine')"
]
]
}
}
}
},
"config2": {
"commands": {
"0-restart": {
"command": "powershell.exe -Command Restart-Computer",
"waitAfterCompletion": "forever"
}
}
},
"config3": {
"commands": {
"01-setenvironment": {
"command": "powershell.exe -ExecutionPolicy Unrestricted C:\\setup\\setenvironment.ps1",
"waitAfterCompletion": "0"
},
"02-signal-resource": {
"command": {
"Fn::Join": [
"",
[
"cfn-signal.exe -e %ERRORLEVEL% --resource WindowsInstance --stack ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
}
]
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Ref": "AMI"
},
"InstanceType": "t2.medium",
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"cfn-init.exe -v -s ",
{
"Ref": "AWS::StackId"
},
" -r WindowsInstance",
" --configsets ascending",
" --region ",
{
"Ref": "AWS::Region"
},
"</script>"
]
]
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Count": "1",
"Timeout": "PT30M"
}
}
}
}
}
YAML example:
AWSTemplateFormatVersion: '2010-09-09'
Description: cfn-init example using configsets
Parameters:
AMI:
Type: 'AWS::EC2::Image::Id'
Resources:
WindowsInstance:
Type: 'AWS::EC2::Instance'
Metadata:
AWS::CloudFormation::Init:
configSets:
ascending:
- config1
- config2
- config3
config1:
files:
C:\setup\setenvironment.ps1:
content: !Sub |
$Folder = 'C:\Program Files\Server\packages\bin.20182.18.0826.0815\'
$OldPath = [System.Environment]::GetEnvironmentVariable('path')
$NewPath = $OldPath + ';' + $Folder
[System.Environment]::SetEnvironmentVariable('path',$NewPath,'Machine')
config2:
commands:
0-restart:
command: powershell.exe -Command Restart-Computer
waitAfterCompletion: forever
config3:
commands:
01-setenvironment:
command: powershell.exe -ExecutionPolicy Unrestricted C:\setup\setenvironment.ps1
waitAfterCompletion: '0'
02-signal-resource:
command: !Sub >
cfn-signal.exe -e %ERRORLEVEL% --resource WindowsInstance --stack ${AWS::StackName} --region ${AWS::Region}
Properties:
ImageId: !Ref AMI
InstanceType: t2.medium
UserData:
Fn::Base64: !Sub |
<script>
cfn-init.exe -v -s ${AWS::StackId} -r WindowsInstance --configsets ascending --region ${AWS::Region}
</script>
CreationPolicy:
ResourceSignal:
Count: 1
Timeout: PT30M
In the preceding templates, the signal doesn't run in UserData, and you can't retrieve the exit code provided by the cfn-init process. By default, CloudFormation fails to create or update a stack without a signal from UserData or Metadata. Then, the stack returns a "timeout exceeded" error.
Tip: To troubleshoot any failures, use the logs at c:\cfn\log in the Windows instance.