It’s pretty common to see DevOps folks roll their eyes when they are asked to install Java and Tomcat on Windows Server in AWS. For some reason, folks have decided this is hard. I don’t think it’s all that hard. And I have the CloudFormation template pattern (below) to prove it. 🙂
Here’s a description of what’s going on in this CloudFormation template snippet along with some pointers:
- The CloudFormation pattern assumes you stage the Tomcat and Java installation executables on S3 and that the template has access to the S3 bucket. You might consider updating Java and Tomcat once the initial versions get old via a CloudFormation change set
- The Java and Tomcat installations are placed in the default subdirectories with default installation options on Windows Server
- The all-important
CATALINA_HOME
andJAVA_HOME
environment variables are set to the default installation directories - I assume you have at least PowerShell 5 installed. See this CloudFormation skeleton if you need a pattern to install PowerShell 5. I actually use the code from that template pattern in every Windows Server 2012 R2 EC2 instance — and recommend you do, too
- The PowerShell scripts that are built in a CloudFormation template can be, ahem, a bit fussy to create and execute. Be certain to include a terminating semi-colon at the end of each statement. I don’t believe the ending newline (
\n
) is absolutely required in each script. But I like it, so it stayed in during my testing. InSet-Java-Tomcat8-Paths-Homes.ps1
the semicolon is required in the PowerShell script to join the old and new paths. In this case, the semicolon is not a PowerShell statement delimiter. Do not replace the single quotes around the semicolon in this script with double quotes - During testing, sometimes Tomcat would install as a Windows service; sometimes it wouldn’t even though the documentation says it installs by default as a service on Windows. Just be sure, I added a
\service.bat install
statement. You’ll see an error in the cfn-init.log if Tomcat was installed as a service, but you can ignore it.
Enjoy.
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Cloud Formation template snippet to install Java JDK and Tomcat on Windows Server 2012 R2. (c) 2017 Air11 Technology LLC -- licensed under the Apache OpenSource 2.0 license, https://opensource.org/licenses/Apache-2.0", "Metadata": {}, "Parameters": {}, "Rules": {}, "Mappings": {}, "Resources": { "JavaTomcatEC2Instance": { "Type": "AWS::EC2::Instance", "Metadata": { "AWS::CloudFormation::Init": { "configSets": { "config": [ "setup" ] }, "setup": { "Install-Java-Tomcat-set-env-variables-paths": { "files": { "c:\\cfn\\modules\\jdk-8u121-windows-x64.exe": { "source": "https://someS3bucket.s3.amazonaws.com/jdk-8u121-windows-x64.exe" }, "c:\\cfn\\modules\\apache-tomcat-8.5.11.exe": { "source": "http://someS3bucket.s3.amazonaws.com.s3.amazonaws.com/apache-tomcat-8.5.11.exe" }, "c:\\cfn\\scripts\\Install-Java-JDK.ps1": { "content": { "Fn::Join": [ "", [ "Set-Location C:\\cfn\\modules;", ".\\jdk-8u121-windows-x64.exe /s ADDLOCAL=\"ToolsFeature,SourceFeature,PublicjreFeature\"", "\n" ] ] } }, "c:\\cfn\\scripts\\Install-Tomcat8.ps1": { "content": { "Fn::Join": [ "", [ "Set-Location C:\\cfn\\modules;", ".\\apache-tomcat-8.5.11.exe /S", "\n" ] ] } }, "c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1": { "content": { "Fn::Join": [ "", [ "$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH).Path;", "$addedFolder = 'C:\\Program Files\\Java\\jdk1.8.0_121\\bin; C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';", "$newPath = $oldPath +';'+$addedFolder;", "Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment' -Name PATH -Value $newPath ;", "[Environment]::SetEnvironmentVariable('CATALINA_HOME', 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\', 'Machine');", "[Environment]::SetEnvironmentVariable('JAVA_HOME', 'C:\\Program Files\\Java\\jdk1.8.0_121\\', 'Machine');", "Restart-Computer -Force;", "\n" ] ] } }, "c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1": { "content": { "Fn::Join": [ "", [ "Set-Location 'C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\bin';", ".\\service.bat install;", "Set-Service Tomcat8 -StartupType Automatic;", "Start-Service Tomcat8;", "\n" ] ] } } }, "commands": { "a-Install-Java-JDK": { "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Java-JDK.ps1", "waitAfterCompletion": "30" }, "b-Install-Tomcat8": { "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8.ps1", "waitAfterCompletion": "30" }, "c-Set-Java-Tomcat8-Paths-Homes": { "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Set-Java-Tomcat8-Paths-Homes.ps1", "waitAfterCompletion": "forever" }, "d-Install-Tomcat8-Service": { "command": "powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Install-Tomcat8-Service.ps1", "waitAfterCompletion": "30" } } } } } } } } }
Leave a Reply