How can I deploy application code to the Apache Tomcat AMIs?

The best way to deploy software to a Tomcat instance (not just ours!) on Amazon AWS is with an EC2 UserData script.

The following script will deploy your application to a subdirectory ("mysystem.com/application/") on your Tomcat server. Nému Hardened Tomcat comes with a simple DOD Warning and Consent banner that it will prompt the user to accept for before sending them to your application.

#!/bin/sh    
logger "Starting user-data deployment script"    
WAR_URL="https://s3.amazonaws.com/your-bucket-name/application.war"      
[ "x${WAR_URL}" = x ] &&    
        logger "No WAR file provided to instance" &&
exit      
WAR_FILENAME=`basename ${WAR_URL}`    
WAR_DIRNAME=`echo ${WAR_FILENAME} | sed 's/\.[Ww][Aa][Rr]$//g'`    
logger "Starting deployment: ${WAR_URL} -> /opt/tomcat/webapps/${WAR_DIRNAME}"    
curl -o /opt/tomcat/webapps/"${WAR_FILENAME}" "${WAR_URL}" &&
        mkdir -p /opt/tomcat/webapps/"${WAR_DIRNAME}" &&
        cd /opt/tomcat/webapps/"${WAR_DIRNAME}" &&
        unzip /opt/tomcat/webapps/"${WAR_FILENAME}" &&
        rm -f /opt/tomcat/webapps/"${WAR_FILENAME}" &&
        echo "/${WAR_DIRNAME}/" > /opt/tomcat/webapps/ROOT/application.url
chown -R tomcat:tomcat /opt/tomcat/webapps    
find /opt/tomcat/webapps -type f -exec chmod a+r {} \;    
find /opt/tomcat/webapps -type d -exec chmod a+rx {} \;    
rm -f "/opt/tomcat/webapps/${WAR_FILENAME}"    
chmod a+x /opt/tomcat    
restorecon -rv /opt/tomcat    
systemctl restart tomcat    
logger "Deployed application ${WAR_DIRNAME} to tomcat from: ${WAR_URL}"    

If your application needs to live in the root URL ("mysystem.com/"), use this script instead. Note that your application will need to provide a DOD Warning and Consent banner of its own.

#!/bin/sh    
logger "Starting user-data deployment script"    
WAR_URL="https://s3.amazonaws.com/your-bucket-name/application.war"      
[ "x${WAR_URL}" = x ] &&    
        logger "No WAR file provided to instance" &&
exit      
WAR_FILENAME=`basename ${WAR_URL}`    
WAR_DIRNAME="ROOT"
logger "Starting deployment: ${WAR_URL} -> /opt/tomcat/webapps/${WAR_DIRNAME}"    
curl -o /opt/tomcat/webapps/"${WAR_FILENAME}" "${WAR_URL}" &&
        mkdir -p /opt/tomcat/webapps/"${WAR_DIRNAME}" &&
        cd /opt/tomcat/webapps/"${WAR_DIRNAME}" &&
        unzip /opt/tomcat/webapps/"${WAR_FILENAME}" &&
        rm -f /opt/tomcat/webapps/"${WAR_FILENAME}"
chown -R tomcat:tomcat /opt/tomcat/webapps    
find /opt/tomcat/webapps -type f -exec chmod a+r {} \;    
find /opt/tomcat/webapps -type d -exec chmod a+rx {} \;    
rm -f "/opt/tomcat/webapps/${WAR_FILENAME}"    
chmod a+x /opt/tomcat    
restorecon -rv /opt/tomcat    
systemctl restart tomcat    
logger "Deployed application ${WAR_DIRNAME} to tomcat from: ${WAR_URL}"    

For a more complex application with multiple WAR files, simple modifications to the scripts above should be all it takes to get started. But if you have trouble trying to get it to work, our Support Team is standing by to help!


Did this solve your problem?