In this tutorial will take a look at fork-lifting your bare metal architecture into the cloud by installing Jenkins on an Ec2 instance and letting it build a war file that we can play on a local Tomcat server. Many people have realized the incalculable benefits of cloud computing but are often stymied with details of the migration into the cloud. Hopefully this demonstration will answer some of your questions and show just how easy it is to be massively productive in the cloud.
There are many web application frameworks out these days. I've always liked the versatility of Grails because it's such a natural use of the JVM. To this end, let's spin up an Ec2 instance, install Grails 3 on it and build a *.war file that can play in Tomcat. In this example I'm going to use t2 small instance and make sure that port 8080 is open in the security group. This is the default port for Tomcat and we don't want to do anything too crazy here. Once the instance is online shell into it and let's install grails. We are going to install Grails via SDkman. To install SDKman on your instance as well as the Grails framework just run the following commands
curl -s "https://get.sdkman.io" | bash
sdk install grails
To speed things along I have a git repo that will help us get up and running. Let's run
sudo yum install git
git clone https://github.com/jdav999/mine.git
In the git repo I have included a Tomcat tarball as well as the Jenkins.war file. Move the tarball up one directory and run
tar xvfz apache-tomcat-7.0.77.tar.gz to expand the Tomcat. Now copy the Jenkins.war file in to webapps directory of Tomcat and from in the bin directory of Tomcat run
sh startup.sh to start the server. Now point your browser to http://public-ip:8080/jenkins and you'll be here
Setting Up your Jenkins Job
One of the first things we want to do here is setup basic Jenkins plugins. Mostly we just want the git client plugin. This is easy to install if you go to Manage Jenkins => Manage plugins => Available tab and search for git client plugin. At this point you configure Jenkins to pull code from github. You can't completely do this with my code because my github ssh keys won't include the ones for your instance. However, you can certainly do this for your own code. Once you correctly install the plugin you see the option for git in your job configuration like this.
Be sure that you put you git ssh clone url in the configuration. It will be of the form
[email protected]:jdav999/mine.git. Once you do this you likely encounter this error.
This is a common Jenkins error but to fix it all you need to do is generate ssh keys and put them in your
.jenkins folder. To this end generate the keys from the command line with
ssh-keygen. This will generate a .ssh folder in your home directory. Successful key generation will look like this
Simply copy the entire .ssh folder to your .jenkins folder and the previous error will vanish. You'll also want to copy the contents of the generated id_rsa.pub file to you ssh keys in github. This will allow Jenkins to pull down the latest changes for the build. Now we'll just add a build step to run a shell script to signal the grails framework to build a *.war file and move it to the webapps directory of our tomcat server. It will look like this
Now we can launch a build and follow the console output.
The build succeeds, but the real artifact of success will be our running grails app. To confirm everything went as expected point your browser to
http://public-ip:8080/mine-0.1/ and you'll see the running app.
Landing the Plane
We have done some really exciting things here bringing together application development and operations on the common thread of AWS. However, there are several ways you can take this knowledge to the next level. You can use the elastic beanstalk service in AWS or even just the vanilla Jenkins Pipeline Plugin. There is even an elastic beanstalk Jenkins plugin. What you do is up to you and your needs. I hope this post opens your mind to the possibilities. Have fun experimenting and I'll see you in the next post.