Jason
Jason

Reputation: 557

Can't call any powershell script from Cloudformation template (windows instance)

I created a Windows 2012 AMI and created an instance of that AMI using the CloudFormation template shown below.

In that JSON script I want to call a PowerShell script to disable a service (simple one). The EC2 Windows 2012 instance gets created. I made sure EC2Config service was running before I took AMI.But the PowerShell script doesn't get executed from the CFN template. Any idea why?

{
     "AWSTemplateFormatVersion": "2010-09-09",
     "Description": "EC2 Head Node Instance ",
     "Parameters": {
       "VPC": {
        "Description": "The default VPC",
        "Type": "AWS::EC2::VPC::Id"
    },
    "AvailabilityZone": {
        "Description": "Availablity Zone",
        "Type": "String"

    },
    "Region":{
        "Description": "Dev/Test/Prod regions",
        "Type": "String"
    },
    "AMI": {
        "Description": "AMI to start virtual server",
        "Type": "String",
        "Default": "ami-19273960",
        "MaxLength": 12,
        "MinLength": 12
    },      
    "Subnet": {
        "Description": "subnet to launch virtual server in",
        "Type": "AWS::EC2::Subnet::Id"
    }

},      
"Resources": {
    "EC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Metadata": {
            "AWS::Cloudformation::Init": {
                "configSets": {
                     "config": [
                     "rename",
                     "bootstrapDSC"
                      ]                 
                },
                "rename": {
                     "a-rename-computer" : {
                      "command": "powershell.exe -Command Rename-Computer -qrmawshead01 Server1 -Restart",
                      "waitAfterCompletion" : "forever"
                      }
                },
                "bootstrapDSC": {
                     "a-setpullmode" : {
                      "command": "powershell.exe -Command c:\\cfn\\scripts\\SetPullMode.ps1",
                              "waitAfterCompletion" :"0"
                      }  

                }

            }

        },
        "Properties": {
            "ImageId" : { "Ref": "AMI"},
            "SubnetId": {"Ref": "Subnet"},
            "AvailabilityZone": {"Ref": "AvailabilityZone"},
            "SecurityGroupIds" : [ "sg-b603b2cc" ],
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "Head Node in DEV region"
                }
            ]
            }
        }

},
"Outputs": {
    "InstanceId": {
        "Value": {"Ref": "EC2Instance"},
        "Description": "ID of virtual server"
    },

    "PublicIPAddress": {
        "Value": {"Fn::GetAtt": ["EC2Instance", "PublicIp"]},
        "Description": "public IP address of virtual server"
    }
  }
 }

Upvotes: 2

Views: 2184

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269111

While you have configured CloudFormation::Init in your template, it requires one additional step to activate.

The instance requires a User Data script that calls cfn-init.exe. This program then retrieves the configuration from the CloudFormation template and runs the requested commands.

For example:

  "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
    "<script>\n",

    "cfn-init.exe -v -s ", { "Ref" : "AWS::StackName" },
    " -r SharePointFoundation",
    " --region ", { "Ref" : "AWS::Region" }, "\n",

    "cfn-signal.exe -e %ERRORLEVEL% ", { "Fn::Base64" : { "Ref" : "SharePointFoundationWaitHandle" }}, "\n",

    "</script>"
    ]]}}

The signalling method also allows cfn-init to signal back success/failure to CloudFormation.

See: Bootstrapping AWS CloudFormation Windows Stacks

Upvotes: 2

Related Questions