AWS: Part II — Deploy Kotlin Spring Boot Reactive App on Elastic Beanstalk using CodePipeline

Sujit Kamthe
Being Professional
Published in
6 min readFeb 14, 2021

In the previous article (https://medium.com/beingprofessional/aws-part-i-deploy-kotlin-spring-boot-reactive-app-on-elastic-beanstalk-ad81c0796186) we saw how to deploy a Kotlin Spring Boot Reactive App on Elastic Beanstalk manually. In this article we will look at how the deployment can be automated.

Create a CodeCommit Repository

We will be using CodeCommit git repository as an SCM for our service. Let’s create a CodeCommit Repository

Generate git credentials for accessing the CodeCommit repository

The full documentation for working with CodeCommit repositories using IAM users is available on aws site: https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html

To be able to push and pull code from the CodeCommit repository we would need an IAM user.

Let’s create an IAM user. I already had a user created, so I will use the same.

Once a user is created, we need to attach a IAM permission to the user so that the user gets access to create CodeCommit repositories. For this you need to attach the ‘AWSCodeCommitPowerUser’ permission to the user.

Generate git credentials

Next generate git credentials from security credentials tab from the IAM user page.

Note: Don’t forget to download or note the credentials as the credentials will not be shown again once you close the popup.

Push the code to the CodeCommit repository

Create a CodeCommit Pipeline

Go to CodeCommit service in aws console and click on “Create Pipeline”

CodePipeline: Step 1

Enter name for the pipeline and keep other settings default. Click next.

CodePipeline: Step 2

Add a source for the code pipeline. The code would be fetched from the mentioned source. Since we have pushed our code in CodeCommit select AWS CodeCommit as the Source provider.

Select the repository name which we had created earlier, and then select the branch. Keep other settings to default.

CodePipeline: Step 3

In this build stage we will configure build provider so that the code pushed in CodeCommit repository is build and packaged as runnable jar which will be used by Elastic Beanstalk for deployment.

You can also run tests as part of build stage, which we will see soon.

Select AWS CodeBuild as the provider. Since there is no CodeBuild project created yet, you won’t be able to select a project name. To create a CodeBuild project click on Create project button which will opena a new page or popup.

On CodeBuild popup enter project name and description.

In environment select appropriate OS and runtime. Make sure you select the same OS and runtime as that of Beanstalk environment otherwise the deployment might fail. Keep everything else as default and click on “Continue to CodePipeline” button.

Once the CodeBuild project is created, it can be referred in the CodePipeline definition.

CodePipeline: Step 4

Next, we add a deploy stage in our CodePipeline. Select AWS Elastic Beanstalk as provider as we want to deploy our service on Elastic Beanstalk. Choose the already created Beanstalk application and environment.

CodePipeline: Step 5

Review and deploy the CodePipeline. This will trigger a CodePipeline build.

You might be surprised to see that, the CodePipeline fails in the Build stage.

If you click on details and carefully observer logs, it tells the reason for failure.

Phase context status code: YAML_FILE_ERROR Message: YAML file does not exist

Which is fair, because we have not created a buildspec.yaml file which defines what should happen in build phase.

Define buildspec.yaml

AWS CodeBuild allows us to define the build phase as a code using buildspec.yaml. We can create and put this file in the project codebase at the project root and CodeBuild will find it automatically using the convention and would run the spec mentioned in the buildspec.yaml.

So let’s add a new file named buildspec.yaml in the project root.

More about buildspec.yaml: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

To be able to successfully deploy our service on Elastic Beanstalk we need a packaged runnable jar file. So let’s add a step in buildspec.yaml to build a runnable jar file. We would also run tests as part of the build phase

buildspec.yaml

version: 0.2
phases:
build:
commands:
- echo building and running tests
- ./gradlew test bootJar

The above definition tells CodeBuild to add phase named build which will run tests and then would create a runnable jar.

We also need to specify the artifact generated by the CodeBuild pipeline so that CodePipeline knows how to find the artifact for deployment. So let’s add a artifacts block after the phases block in the buildspec.yaml

buildspec.yaml

version: 0.2
phases:
build:
commands:
- echo building and running tests
- ./gradlew test bootJar
artifacts:
files:
- build/libs/quiz-0.0.1-SNAPSHOT.jar
discard-paths: yes

Note: discard-paths: yes is an important configuration in the artifacts block. It tells the artifact generated should be put on the root path of the S3 bucket used to store the artifacts by ignoring all the paths before it (build/libs/*). If this configurations is skipped then the artifact will be put at a path `build/libs/` on the S3 bucket which cause deployment to fails as Elastic Beanstalk will try to find an artifact at the root location on the S3 bucket.

Commit and push the buildspec.yaml to the CodeCommit repository.

This will trigger another build on the CodePipeline. Wait for the build and deploy stage to complete. This time both build and deploy stages would be green.

Test the CodePipeline

Let’s make a change to QuizService and see if it deploys the change successfully on the Elastic Beanstalk environment. To test let’s add another Quiz to the list of Quizzes returned by the QuizService.getAllQuizzes()

Quiz(“Go Lang Quiz”, “Quiz about Go Lang”)

Make sure to fix test as well. Commit and push the changes.

This should trigger a new build in CodePipeline. Wait for the build to finish the deployment.

Once the deployment is successful, hit the /quizzes API to see if the newly added Quiz appears in the response.

So it validates that the CodePipeline we have set up is able to automatically build and deploy the changes from the CodeCommit repository to the Elastic Beanstalk environment.

We have created Elastic Beanstalk environment and CoePipeline using AWS console. In next articles, we will see how the Elastic Beanstalk environment and the CodePipeline can be created using IaaC (Cloudformation).

--

--