Carlos Chacin

Software Engineering Experiences


Java 17 + AWS Lambda

Posted at June 06, 2023 /

Header

AWS recently announced Java 17 support for Lambdas, In this blog post, we’ll explore how to create a Java 17 AWS Lambda Function without IDE, build tool or any dependency.

Fist let’s create the lambda function code:

public class Function {

  public record Input(int x, int y) {}

  public record Output(long result) {}

  public Output add(Input input) {
    return new Output(input.x + input.y);
  }
}

In this example, we have an add method that takes an Input java record with 2 fields: x and y, and returns an Output java record with the sum of x and y assigned to the result field.

This is the only requirement for a AWS Lambda Function:

An instance method that takes one single parameter and returns something an object or Void.

That’s it. Now we can proceed to compile and package our AWS Lambda Function:

Compile:

$ javac Function.java

Package:

$ zip function.zip *.class

Next, let’s navigate to the AWS Lambda Console. 2

Click on the Create function button.

3

Assign a lambda name and select Java 17 as the runtime.

4

Click on the Upload from button.

5

Click Upload button.

6

Choose the function.zip file that we generated.

7

Click Edit on the Code properties section.

8

Change the Handler to Function::add and click Save.

9

Go to the Test tab.

10

Change the JSON payload to:

{
  "x": 123,
  "y": 333
}

Click the Test button, then expand the Details section to see the result.

11

That’s it! You have successfully created and tested your Java 17 AWS Lambda function. The output should be:

{
  "result": 456
}

12

This demonstrates the simplicity and power of using Java 17 with AWS Lambda. With just a few steps, you can leverage the latest Java version to build efficient and scalable serverless applications.

Feel free to explore further and unleash the full potential of Java 17 on AWS Lambda. Happy coding!

Read more ...

🏆 Default 🔗 Maven and ☕ Java settings per project

Posted at September 23, 2021 /

github-actions

I generally use many different options on maven projects to set up things like memory lower and upper limits, fail at the end, the process in batch, use x number of threads, etc. In addition to this, sometimes I need to pass flags to the JVM like add modules, garbage collector flags, etc., and it is difficult to remember and also error-prone to have something like this:

$ JAVA_OPTS="-Xms512m -Xmx1024m -Djava.awt.headless=true" \
  mvn -B -T 4 -fae -P ci verify

or even worst with the extended version of the flags:

$ JAVA_OPTS="-Xms512m -Xmx1024m -Djava.awt.headless=true" \
mvn --batch-mode --threads 4 -fail-at-end --activate-profiles ci verify

Fortunately for us, since maven 3.3.1, we now can setup this per project, including the flags in these two files relatives to the project directory:

Read more ...

☕🚢 Maven Build in < 20 lines of yaml 🕟

Posted at September 22, 2021 /

github-actions

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

Create the directory

$ mkdir -p .github/workflows

Create the yaml file

$ touch .github/workflows/maven.yml

Copy this content

name: Maven Build
on:
  push:
    branches:
      - main
jobs:
  build:
    name: "Maven Build"
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout Sources"
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
      - name: "Set up JDK"
        uses: actions/setup-java@v2
        with:
          distribution: "temurin"
          java-version: 11
          cache: "maven"
      - name: "Build with Maven"
        run: mvn verify

Commit and push to Github

$ git add .github && \
  git commit -m "maven build action"

Enjoy your builds

Go to the actions tab for your repository over github.com:

https://github.com/${USER}/${PROJECT}/actions/workflows/maven-build.yml

maven-build

Read more ...

💾 Java Records 💿 with Jackson 2.12

Posted at March 04, 2021 /

java-records-jackson

In the previous article about Java 14 Records, we saw how to start creating Records to avoid writing much boilerplate code that the compiler would generate for us.

Now the next steps are to see how we can serialize records to JSON and deserialize JSON to records to be able to use them as a request/response representation for microservices.

In this case, we would use the Jackson 2.12+.

Read more ...

🌒 JakartaEE JSON-B 🐝 Retrofit2 Converter

Posted at September 23, 2020 /

java-records-jsonb

Retrofit is pluggable allowing different serialization formats and their libraries to be used for converting Java types to their HTTP representation and parsing HTTP entities back into Java types.

These are called converters, and Retrofit includes a few first-party modules for popular frameworks

Just for fun, I created a Retrofit2 Converter.Factory for JakartaEE Json-B.

Read more ...