MicroProfile with Quarkus as AWS Lambda Function deployed with Cloud Development Kit (CDK) v2 for Java
A lean starting point for building, testing and deploying Quarkus MicroProfile applications deployed as AWS Lambda behind API Gateway. The business logic, as well as, the Infrastructure as Code deployment are implemented with Java.
A Quarkus MicroProfile application:
@Path("hello")
@ApplicationScoped
public class GreetingResource {
@Inject
Greeter greeter;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return this.greeter.greetings();
}
@POST
@Consumes(MediaType.TEXT_PLAIN)
public void hello(String message) {
this.greeter.greetings(message);
}
}
...with an additional dependency / extension for AWS REST APIs Gateway:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-rest</artifactId>
</dependency>
or HTTP APIs Gateway (default configuration):
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http</artifactId>
</dependency>
...deployed with AWS Cloud Development Kit:
Function createFunction(String functionName,String functionHandler,
Map<String,String> configuration, int memory, int maximumConcurrentExecution, int timeout) {
return Function.Builder.create(this, functionName)
.runtime(Runtime.JAVA_21)
.code(Code.fromAsset("../lambda/target/function.zip"))
.handler(functionHandler)
.memorySize(memory)
.functionName(functionName)
.environment(configuration)
.timeout(Duration.seconds(timeout))
.reservedConcurrentExecutions(maximumConcurrentExecution)
.build();
}
You choose between HTTP APIs gateway and REST APIs gateway with the httpAPIGatewayIntegration
variable:
public class CDKApp {
public static void main(final String[] args) {
var app = new App();
var appName = "quarkus-apigateway-lambda-cdk";
Tags.of(app).add("project", "MicroProfile with Quarkus on AWS Lambda");
Tags.of(app).add("environment","development");
Tags.of(app).add("application", appName);
var httpAPIGatewayIntegration = true;
new CDKStack(app, appName, true);
app.synth();
}
}
}
- Java / openJDK is installed
- Maven is installed
Same installation as aws-cdk-plain:
- For max convenience use the
default
profile. A profile nameddefault
doesn't have to be specificed with the--profile
flag or configured in CDK applications. - Install AWS CDK CLI
cdk boostrap --profile YOUR_AWS_PROFILE
This template ships with AWS HTTP APIs Gateway. REST APIs Gateway is also supported. You can switch between both by using the corresponding extension (see Choosing between HTTP APIs and REST APIs.
Private APIs are only supported by REST API Gateway.
You can also build AWS Lambda function.zip
and executable Quarkus JAR by extracting the extension into a Maven profile. Checkout: https://adambien.blog/roller/abien/entry/hybrid_microprofile_deployments_with_quarkus.
See you at: airhacks.live
Build the Quarkus project lambda
and deploy it with cdk
as AWS Lambda:
cd lambda
./buildAndDeployDontAsk.sh
To continuously deploy the AWS Lambda at any changes, perform:
cd cdk
cdk watch
Now on every: mvn package
in lambda
directory / project the JAX-RS application is re-deployed automatically.
You can run the lambda
project as regular Quarkus application with:
mvn compile quarkus:dev
The application is available under: http://localhost:8080/hello
Using cdk watch
for faster deployments
See you at: airhacks.live