2

I have a CloudFormation template that launches Windows instances with Java and Tomcat running as a service. I have to set the value of Maximum memory pool size for Tomcat in registry. I prepared a PowerShell command and it works fine from the command line. Then I tried 3 different ways of running them as a part of a CloudFormation template and none of those worked.

Neither Windows EventLog nor files in C:\Program Files\Amazon\Ec2ConfigService\Logs provided me any clue about why it had not worked.

1

"LaunchConfig" : {
    "Type" : "AWS::AutoScaling::LaunchConfiguration",
    "Properties" : {
        --- some props removed ---,
        "UserData" : {
            "Fn::Base64" : {
                "Fn::Join" : [
                    "",
                    [
                        "<powershell>\n",
                            "Set-ItemProperty -Path \"HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java\" -Name JvmMx -Value 5000 \n",
                            "Restart-Service Tomcat8 \n",
                        "</powershell>"
                    ]
                ]
            }
        }
    }
}

2

"LaunchConfig" : {
    "Type" : "AWS::AutoScaling::LaunchConfiguration",
    "Properties" : {
        --- some props removed ---,
        "UserData" : {
            "Fn::Base64" : {
                "Fn::Join" : [
                    "",
                    [
                        "<script>\n",
                            "PowerShell -Command \"& {Set-ItemProperty -Path 'HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java' -Name JvmMx -Value 5000}\" \n",
                            "PowerShell -Command \"& {Restart-Service Tomcat8}\" \n",
                        "</script>"
                    ]
                ]
            }
        }
    }
}

3

"LaunchConfig" : {
    "Type" : "AWS::AutoScaling::LaunchConfiguration",
    "Properties" : {
        --- some props removed ---,
        "UserData" : {
            "Fn::Base64" : {
                "Fn::Join" : [
                    "",
                    [
                        "<script>\n",
                            "cfn-init.exe -v -s ", {"Ref" : "AWS::StackId"}, " -r LaunchConfig --region ", {"Ref" : "AWS::Region"}, "\n",
                        "</script>"
                    ]
                ]
            }
        }
    },
    "Metadata" : {
        "AWS::CloudFormation::Init" : {
            "config" : {
                "commands" : {
                    "1-heapsize" : {
                        "command" : "PowerShell -Command \"& {Set-ItemProperty -Path 'HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java' -Name JvmMx -Value 5000}\""
                    },
                    "2-restart" : {
                        "command" : "PowerShell -Command \"& {Restart-Service Tomcat8}\""
                    }
                }
            }
        }
    }
}

1 Answer 1

6

User Data in the EC2 instance has to be enabled for the cfn-init and other user-data injected script to be applied / run.

You need to open the EC2ConfigurationService - check the Enable User Data, stop the instance and take an AMI and please use the newly created AMI.

enter image description here

Detailed information can be found at the Amazon Documentation Link - http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/UsingConfig_WinAMI.html

Sign up to request clarification or add additional context in comments.

1 Comment

I was banging my head against a wall for quite sometime to figure this out - long ago. Currently - I am having problem of User Data - getting executed sometime and not getting executed other time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.