Spring Boot Lambda Prerequisites

Spring Boot Lambda Prerequisites

Creating a Spring Boot Lambda on AWS

Learn how to build and deploy a simple spring boot based AWS lambda and then automate its deployment with Terraform.

full course
  1. Spring Boot Lambda Prerequisites
  2. Spring Boot Lambda Implementation
  3. Spring Boot Lambda API Implementation
  4. Terraform Setup and First Install
  5. Terraform Centralized State Management
  6. Automated Terraform Deploy Using Github Actions
  7. Confirming the Continuous Deployment Pipeline

In this course we’re going to build a very simple spring boot lambda, deploy it manually to AWS to make sure that it works and then automate the deployment using Terraform and github actions. This will allow us to automatically build and deploy changes from a commit to the main branch.

First off, you’ll need to setup the following things:

New GitHub Repository

Go ahead and create a new empty github repository. At the end you should have something like this:

clone it to your local workspace by getting the clone command from the green code button and execute it locally with the clone command.

$ git clone https://github.com/bullyrooks/helloworld-lambda.git
Cloning into 'helloworld-lambda'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 850 bytes | 44.00 KiB/s, done.

Initialize the Project with Initializr

The spring assistant plugin doesn’t work with the newer versions of intellij community unfortunately, so we can build a spring boot project from scratch the old fashioned way. Head over to initializr and create a new project like this:

Keep in mind that java 11 is the highest version of java that is supported by lambda at the moment. Also, I’m using maven and my examples will be using maven.

Generate the .zip and extract it into your project directory that you just cloned.

Create a New IntelliJ Project

Now make a new intellij project from existing sources, point it to your helloworld directory and build it from the maven archetype.

You should have a directory structure similar to this:

$ ls -al
total 59
drwxr-xr-x 1 brian 197609     0 Jul 23 11:32 .
drwxr-xr-x 1 brian 197609     0 Jul 23 11:00 ..
drwxr-xr-x 1 brian 197609     0 Jul 23 11:32 .git
-rw-r--r-- 1 brian 197609   474 Jul 23 11:32 .gitignore
drwxr-xr-x 1 brian 197609     0 Jul 23 11:31 .idea
drwxr-xr-x 1 brian 197609     0 Jul 23 11:06 .mvn
-rw-r--r-- 1 brian 197609   649 Jul 23  2021 HELP.md
-rw-r--r-- 1 brian 197609    19 Jul 23 11:00 README.md
-rw-r--r-- 1 brian 197609  6134 Jul 23 11:28 helloworld-lambda.iml
-rwxr-xr-x 1 brian 197609 10070 Jul 23  2021 mvnw
-rw-r--r-- 1 brian 197609  6608 Jul 23  2021 mvnw.cmd
-rw-r--r-- 1 brian 197609  1585 Jul 23  2021 pom.xml
drwxr-xr-x 1 brian 197609     0 Jul 23 11:06 src

now update the .gitignore file to make sure that we’re not going to pull in anything we don’t want. It should look like this:

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### Maven Build
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

switch over to a new branch:

$ git checkout -b initial
Switched to a new branch 'initial'

make sure it builds

$ mvn clean package
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.885 s
[INFO] Finished at: 2021-07-23T11:38:45-06:00
[INFO] ------------------------------------------------------------------------

check status and commit

$ git status
On branch initial
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .mvn/
        mvnw
        mvnw.cmd
        pom.xml
        src/

no changes added to commit (use "git add" and/or "git commit -a")

$ git add .
...
$ git commit -m "initial commit"
[initial 82c760e] initial commit
 10 files changed, 705 insertions(+)
 create mode 100644 .mvn/wrapper/MavenWrapperDownloader.java
 create mode 100644 .mvn/wrapper/maven-wrapper.jar
 create mode 100644 .mvn/wrapper/maven-wrapper.properties
 create mode 100644 mvnw
 create mode 100644 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/bullyrooks/helloworldlambda/HelloworldLambdaApplication.java
 create mode 100644 src/main/resources/application.properties
 create mode 100644 src/test/java/com/bullyrooks/helloworldlambda/HelloworldLambdaApplicationTests.java

$  git push --set-upstream origin initial
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': bullyrooks
Password for 'https://[email protected]':
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 4 threads
Compressing objects: 100% (17/17), done.
Writing objects: 100% (26/26), 52.49 KiB | 8.75 MiB/s, done.
Total 26 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'initial' on GitHub by visiting:
remote:      https://github.com/bullyrooks/helloworld-lambda/pull/new/initial
remote:
To https://github.com/bullyrooks/helloworld-lambda.git
 * [new branch]      initial -> initial
Branch 'initial' set up to track remote branch 'initial' from 'origin'.

Merge to Main

we already know this works, so lets merge to main for now so we can branch off main for our next session

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ git merge initial
Updating 629e815..82c760e
Fast-forward


$ git push

That’s it for the setup. Next we’re going to build a jar that can be deployed as a lambda.

0 comments on “Spring Boot Lambda PrerequisitesAdd yours →

Leave a Reply

Your email address will not be published. Required fields are marked *