Automate Spring Boot app deployment on AWS infrastructure using Github and AWS CodePipelines

Hasitha Kaushan
FAUN — Developer Community 🐾
6 min readMar 27, 2020

--

It is been a while since I last caught up in this COVID-19 pandemic. 😫 This time I am bringing how to automate your spring boot app deployment on AWS. In this article I am deriving how to configure AWS CodePipeline to deploy a spring boot application from GitHub to an AWS Elastic Beanstalk instance 💪 In order to continue this article, you will have to follow my previous articles i.e.

  1. Create your first Web App using Spring Boot with IntelliJ
  2. Deploy Spring Boot application into the AWS infrastructure.

I will be continuing from those articles and if you are not familiar with either spring boot or AWS Elastic BeanStalk, I do recommend you to follow them.

First of all, let’s get to know what is the automation of code deployment. It is simply called as CI / CD i.e. Continuous Integration and Continuous Delivery and Deployment. As nowadays the Agile approach is used, features have to be delivered in every 2 to 3 weeks. That will make the client happy as well as everything would get sorted 😌 In those instances you will have to deploy your code to the server every now and then. It is a difficult task. Then it comes the advantage of using ci/cd ❤. You simply have to commit your changes and the ci/cd configuration will automatically test your code and deploy it to the server 💪

Now let’s move on to the example I am going to describe. In this example, source code is in GitHub and once we commit a change, it will trigger a hook to AWS infrastructure. Then the build is going to be done using AWS CodeBuild. Build is defined using buildspec.yml which I will show later. After source code is built, artifacts are uploaded to an S3 bucket. There is a feature called AWS CodeDeploy which deploys the artifacts in the S3 bucket to the Elastic BeanStalk instance. This whole process is controlled using a service called AWS CodePipelines.

Now I am going to use my initial project, which I mentioned earlier. In addition to that, I am going to create a GitHub repository and commit and push my code into it.

Create new repo in GitHub

And then you need to initialize git on your project. Open your terminal and go to your project directory. And put the command

git init

Then you need to add you whole project using

git add .

Then you have to commit your code

git commit -m "initial commit"

Up next you have to make the connection to the remote repository using

git remote add origin <URL>

You can find your URL in the following section.

And finally, you have to push your code to the remote repository.

git push -u origin master

Hooray 💪 It’s done. If you have successfully pushed your code you will be able to see something like this.

I had not added the builtspec.yml which I mentioned earlier. Thus right now I am going to add that to the root of my project. This file will be the build configuration for AWS CodeBuild.

In this example, I am going to deploy an exploded WAR file. Thus Exploded WAR plugin needs to be added to the pom.xml file.

What is done by this plugin is that the artifacts will be exploded into a folder called deploy. Simply the WAR file will be exploded into the deploy folder.

Alrighty, let’s focus on buildspec.yml again.

version represents the build spec standard being used in the configuration. phases represent the stages that we want to instruct CodeBuild to run commands. phases can be installed, pre_build, build, and post_build. Here I have only used pre_build, build, and post_build. In the pre_build phase, I have only added a simple log.

mvn clean prepare-package war:exploded

This will explode the WAR file into the deploy package as I have added the plugin previously.

In my post_build phase, I have added a command to move the deploy folder from target directory to root.

mv target/deploy ./

artifacts represent a set of build output artifacts that CodeBuild uploads to the output bucket. In this case, artifacts will be in the deploy folder thus I state here.

Ok, now it’s done. Now let’s go to the AWS console and link this GitHub repo to CodePipeline.

Search for CodePipeline

Give a name for the pipeline and keep other stuffs as default.

The next step is to select a source. As we are using GitHub, it should be selected as the source provider.

After you successfully connected to GitHub you have to select the repository and the branch. As I mentioned earlier, I use webhooks to trigger my commits.

Make sure you are in the same region as you have created your BeanStalk application (which I demonstrated in the previous article). Otherwise, you will have to do this process again starting from creating the pipeline 😫

Back to the track. Next up, you have to add the build stage.

Here you have to provide AWS CodeBuild as the Build provider and select the region as I mentioned above. Then you need to create a project.

Give name for Project name. And then select an environment. I have selected Ubuntu. Runtime as Standard.

Keep other details as default.

If you have successfully created your build stage you will see something like the following 💪

Up next is the deploy stage. Here you have to select the Elastic BeanStalk as the Deploy provider. If you are in the correct region as in this article you’ve been following (I suppose), you can select your already configured application and the environment.

Oh ye!. You have done it. If you are happy with everything, create a pipeline after reviewing it. It will automatically trigger the hook. But wait on… We haven’t committed our latest changes to GitHub. Let’s commit them to root on your terminal commit them and push to the repository. If you have done everything right, the three stages; source, build, and deploy will be successful ❤. You just need to commit whatever you’ve done and this process will automatically deploy your application to an EC2 instance in AWS 💪

Commit getting triggered by the hook
Build stage getting successful
Deploy stage getting successful

That is that and hope this was helpful to you and if you like this, do put a clap and keep your comments coming in.

Happy Coding! ❤️

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

I am an Electrical Engineering undergraduate at University of Moratuwa. My passion is towards the Cloud Computing and IoT.