Skip to content

Commit

Permalink
aws stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jimomulloy committed Mar 13, 2023
1 parent b03d58c commit 99cd3fe
Show file tree
Hide file tree
Showing 71 changed files with 2,704 additions and 2 deletions.
21 changes: 21 additions & 0 deletions instrument-aws/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Adam Bien

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
154 changes: 153 additions & 1 deletion instrument-aws/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,154 @@
# MicroProfile with Quarkus as AWS Lambda Function deployed with Cloud Development Kit (CDK) v2 for Java

![The Instrument Block Diagram](https://github.com/jimomulloy/instrument/blob/main/images/instrumentblocks.drawio.png)
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.

# TL;DR

A Quarkus MicroProfile application:

```java

@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](https://quarkus.io/guides/amazon-lambda-http) for AWS REST APIs Gateway:

```xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-rest</artifactId>
</dependency>
```

or HTTP APIs Gateway (default configuration):

```xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http</artifactId>
</dependency>
```

...deployed with AWS Cloud Development Kit:

```java

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_11)
.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:

``` java
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();
}
}
}
```

## Prerequisites

## Java

1. Java / openJDK is installed
2. [Maven](https://maven.apache.org/) is installed

## AWS

Same installation as [aws-cdk-plain](https://github.com/AdamBien/aws-cdk-plain):

0. For max convenience use the [`default` profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html). A profile named `default` doesn't have to be specificed with the `--profile` flag or configured in CDK applications.
1. Install [AWS CDK CLI](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)
2. [`cdk boostrap --profile YOUR_AWS_PROFILE`](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)

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](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html).

Private APIs are only supported by [REST API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-apis.html).

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](https://adambien.blog/roller/abien/entry/hybrid_microprofile_deployments_with_quarkus).

See you at: [airhacks.live](https://airhacks.live)

# in action

## full build

Build the Quarkus project `lambda` and deploy it with `cdk` as AWS Lambda:

```
cd lambda
./buildAndDeployDontAsk.sh
```

## continuous and accelerated deployment

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.

## local deployment

You can run the `lambda` project as regular Quarkus application with:

`mvn compile quarkus:dev`

The application is available under: `http://localhost:8080/hello`

## Deploying MicroProfile / Quarkus Application as AWS Lambda with Java AWS CDK

[![Deploying MicroProfile / Quarkus Application as AWS Lambda with Java AWS CDK](https://i.ytimg.com/vi/NA0WjIgp4CQ/mqdefault.jpg)](https://www.youtube.com/embed/NA0WjIgp4CQ?rel=0)


## Accelarating deployments with CDK v2 Watch


Using `cdk watch` for faster deployments

[![Accelerating Deployment with CDK v2 Watch](https://i.ytimg.com/vi/SK7ic9wTYqU/mqdefault.jpg)](https://www.youtube.com/embed/SK7ic9wTYqU?rel=0)

See you at: [airhacks.live](https://airhacks.live)
6 changes: 6 additions & 0 deletions instrument-aws/buildAndDeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
set -e
echo "building functions"
cd lambda && mvn clean package
echo "building CDK"
cd ../cdk && mvn clean package && cdk deploy
6 changes: 6 additions & 0 deletions instrument-aws/buildAndDeployDontAsk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
set -e
echo "building functions"
cd lambda && mvn clean package
echo "building CDK"
cd ../cdk && mvn clean package && cdk deploy --all --require-approval=never
20 changes: 20 additions & 0 deletions instrument-aws/buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax
version: 0.2
phases:
install:
commands:
- echo "build started by ${CODEBUILD_INITIATOR}"
- echo "running on:"
- java -version
- mvn -version
- npm --version
- npm install -g aws-cdk
- cdk --version
build:
commands:
- echo "building AWS Lambda"
- cd ${CODEBUILD_SRC_DIR}/lambda && mvn --no-transfer-progress -DskipTests package
- echo "building cdk"
- cd ${CODEBUILD_SRC_DIR}/cdk && mvn --no-transfer-progress -DskipTests package
- echo "deploying application"
- cd ${CODEBUILD_SRC_DIR}/cdk && cdk deploy --all --require-approval=never
31 changes: 31 additions & 0 deletions instrument-aws/cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Slightly Streamlined AWS Cloud Development Kit (CDK) Boilerplate

This is a blank, slightly streamlined, project for Java development with CDK.

You will find the maven command in `cdk.json` file.

It is a [Maven](https://maven.apache.org/) based project, so you can open this project with any Maven compatible Java IDE to build and run tests.

## Installation

1. Install [AWS CDK CLI](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)
2. [`cdk boostrap --profile YOUR_AWS_PROFILE`](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)

## Useful commands

* `mvn package` compile and run tests
* `cdk ls` list all stacks in the app
* `cdk synth` emits the synthesized CloudFormation template
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk docs` open CDK documentation

Enjoy!

## in action

[![Infrastructure as Java Code (IaJC): Setting AWS System Manager Parameter](https://i.ytimg.com/vi/eTG7EV1ThqQ/mqdefault.jpg)](https://www.youtube.com/embed/eTG7EV1ThqQ?rel=0)



See you at: [airhacks.live](https://airhacks.live)
3 changes: 3 additions & 0 deletions instrument-aws/cdk/buildAndDeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
mvn clean package && cdk deploy
3 changes: 3 additions & 0 deletions instrument-aws/cdk/buildAndDeployDontAsk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
mvn clean package && cdk deploy --all --require-approval=never
16 changes: 16 additions & 0 deletions instrument-aws/cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"app": "mvn -e -q compile exec:java",
"watch":{
"include":[
"../lambda/target/function.zip",
"../lambda/src/main/resources/application.properties"
]
},
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
}
}
3 changes: 3 additions & 0 deletions instrument-aws/cdk/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
cdk destroy
72 changes: 72 additions & 0 deletions instrument-aws/cdk/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>airhacks</groupId>
<artifactId>cdk</artifactId>
<version>0.1</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>airhacks.CDKApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- AWS Cloud Development Kit -->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>2.66.1</version>
</dependency>
<!-- alpha versions for HttpApi gateway integration-->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-alpha</artifactId>
<version>2.66.1-alpha.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-integrations-alpha</artifactId>
<version>2.66.1-alpha.0</version>
</dependency>
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.4.257</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
</properties>
</project>
Loading

0 comments on commit 99cd3fe

Please sign in to comment.