CloudFormation is a great tool for spinning up EC2 instances and the like.
Here is an example of using a wait condition for waiting until a Windows 2012 R2 Base service has started. Please excuse the designer JSON. I was using it to validate the JSON correctness.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"YourInstanceId": {
"Type": "AWS::EC2::Instance",
"Properties": {
"DisableApiTermination": "false",
"InstanceInitiatedShutdownBehavior": "stop",
"ImageId": "ami-dfccd1ef",
"KeyName": "YourKeyName",
"Monitoring": "false",
"Tags": [
{
"Key": "Name",
"Value": "NameOfYourInstance"
}
],
"NetworkInterfaces": [
{
"DeleteOnTermination": "true",
"DeviceIndex": 0,
"SubnetId": "yourSubnetId",
"GroupSet": [
"yourSecurityGroupId"
],
"AssociatePublicIpAddress": "true"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<powershell>\n",
"Start-Service BITS \n",
"$arrService = Get-Service BITS \n",
"$returnCode = 1 \n",
"if ($arrService.Status -eq 'running'){ $returnCode = 0 } \n",
"while($arrService.Status -ne 'running') \n",
"{ \n",
"Start-Sleep -s 5 \n",
"$arrService = Get-Service BITS \n",
"if ($arrService.Status -eq 'running'){ $returnCode = 0 } \n",
"} \n",
"cfn-signal.exe -e $returnCode ",
{
"Fn::Base64": {
"Ref": "WaitHandle"
}
},
"\n",
"</powershell> \n",
"<persist>true</persist>"
]
]
}
}
}
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn" : "instancei1c92e0da",
"Properties": {
"Handle" : { "Ref" : "WaitHandle" },
"Timeout" : "300"
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "b0f248b8-89a7-4b6b-881a-ef79dd677322"
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "1f0d4b0b-1971-4e5d-9b2c-5472c764dc92"
}
}
}
},
"Description": ""
}
The BITS service was used here since it's a built-in Windows service. It could be whatever service that you would like to start and verify that it started.
CloudFormation seems good overall. It would be nice to have a local validator for it and / or a way to test the templates locally first.
I'd like to explore the Vagrant AWS provider when I have time (heard that there might be bugs though regarding Windows). Please feel free to comment and share your experiences. Cheers.
No comments:
Post a Comment