Skip to content

Commit

Permalink
docs: Adding Kotlin example. (#1454)
Browse files Browse the repository at this point in the history
* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Flattening structure back to original to make merging easier for v2

* Adding build for Kotlin Gradle

* Adding build for Kotlin Gradle - Restructuring Java examples to v1 approach

* Correcting paths

* Adding SNAPSHOT support and local capability for Maven. Testing using Java 1.8

* Reviewed and updated against PR comments.

* Un-commenting examples

---------

Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Scott Gerring <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2023
1 parent 34fc20e commit aed398b
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 112 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/pr_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ jobs:
cache: 'maven'
- name: Build with Maven
run: mvn -B install --file pom.xml
- name: Build Gradle Example
- name: Build Gradle Example - Java
if: ${{ matrix.java == '8' }} # Gradle example can only be built on Java 8
run: |
cd examples/powertools-examples-core/gradle
./gradlew build
- name: Build Gradle Example - Kotlin
run: |
cd examples/powertools-examples-core/kotlin
./gradlew build
- name: Setup Terraform
if: ${{ matrix.java == '11' }}
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 #v2.0.3
Expand Down
6 changes: 4 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ Each example can be copied from its subdirectory and used independently of the r

## Examples

* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules with different build tools
* [SAM](./powertools-examples-core/sam)
* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules with different build tools and languages
* [CDK](./powertools-examples-core/cdk)
* [Gradle](./powertools-examples-core/gradle)
* [SAM](./powertools-examples-core/sam)
* [Serverless](./powertools-examples-core/serverless)
* [Kotlin](./powertools-examples-core/kotlin)
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
Expand Down
36 changes: 23 additions & 13 deletions examples/powertools-examples-core/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Powertools for AWS Lambda (Java) - Core Utilities Example
# Powertools for AWS Lambda (Java) - Core Utilities Example

This project demonstrates the Lambda for Powertools Java module - including
[logging](https://docs.powertools.aws.dev/lambda/java/core/logging/),
[tracing](https://docs.powertools.aws.dev/lambda/java/core/tracing/), and
[metrics](https://docs.powertools.aws.dev/lambda/java/core/metrics/).

We provide examples for the following infrastructure-as-code tools:
The example application is the same, and you can now also use Kotlin!

## Java
* [AWS SAM](sam/)
* [AWS CDK](cdk/)
* [Serverless framework](serverless/)
Expand All @@ -16,11 +17,21 @@ We also provide an example showing the integration of SAM, Powertools, and Gradl

* [AWS SAM with a Gradle build](gradle/)

For each of the tools, the example application is the same, and consists of the following files:
- App.java - Code for the application's Lambda function.
- AppTests.java - Unit tests for the application code.
- events - Invocation events that you can use to invoke the function.

- [App.java](sam/src/main/java/helloworld/App.java) - Code for the application's Lambda function.
- [AppTests.java](sam/src/test/java/helloworld/AppTest.java) - Unit tests for the application code.
- [events](sam/events/event.json) - Invocation events that you can use to invoke the function.
Configuration files and deployment process for each tool are described in corresponding README files.

## Kotlin

- [Gradle](kotlin/)

Example application consists of the following files:

- App.kt - Code for the application's Lambda function.
- AppTests.kt - Unit tests for the application code.
- events - Invocation events that you can use to invoke the function.

Configuration files and deployment process for each tool are described in corresponding README files.

Expand All @@ -32,14 +43,13 @@ Once the app is deployed, you can invoke the endpoint like this:
curl https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/hello/
```

The response itself isn't particularly interesting - you will get back some information about your IP address. If
The response itself isn't particularly interesting - you will get back some information about your IP address. If
you go to the Lambda Console and locate the lambda you have deployed, then click the "Monitoring" tab you will
be able to find:

* **View X-Ray traces** - Display the traces captured by the traces module. These include subsegments for the
different function calls within the example
* **View Cloudwatch logs** - Display the structured logging output of the example
- **View X-Ray traces** - Display the traces captured by the traces module. These include subsegments for the
different function calls within the example
- **View Cloudwatch logs** - Display the structured logging output of the example

Likewise, from the CloudWatch dashboard, under **Metrics**, **all metrics**, you will find the namespaces `Another`
and `ServerlessAirline`. The values in each of these are published by the code in
[App.java](sam/src/main/java/helloworld/App.java).
Likewise, from the CloudWatch dashboard, under **Metrics**, **all metrics**, you will find the namespaces `Another`
and `ServerlessAirline`. The values in each of these are published by the code in the respective application's Lambda function.
6 changes: 3 additions & 3 deletions examples/powertools-examples-core/cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ For general information on the deployed example itself, you can refer to the par

## Configuration
CDK uses the following project structure:
- [app](./app) - stores the source code of your application, which is similar between all examples
- [infra](./infra) - stores the definition of your infrastructure
- [cdk.json](./infra/cdk.json) - tells the CDK Toolkit how to execute your app
- [app](app) - stores the source code of your application, which is similar between all examples
- [infra](infra) - stores the definition of your infrastructure
- [cdk.json](infra/cdk.json) - tells the CDK Toolkit how to execute your app
- [CdkApp](./infra/src/main/java/cdk/CdkApp.java) - bootstraps your stack, taking AWS `account` and `region` as input
- [CdkStack](./infra/src/main/java/cdk/CdkStack.java) - defines the Lambda function to be deployed as well as API Gateway for it.

Expand Down
184 changes: 92 additions & 92 deletions examples/powertools-examples-core/gradle/gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
38 changes: 38 additions & 0 deletions examples/powertools-examples-core/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Powertools for AWS Lambda (Kotlin) - Core Utilities Example

This project demonstrates the Lambda for Powertools Kotlin module deployed using [Serverless Application Model](https://aws.amazon.com/serverless/sam/) with
[Gradle](https://gradle.org/) running the build. This example is configured for Java 1.8 only; in order to use a newer version, check out the Gradle
configuration guide [in the main project README](../../../README.md).

You can also use `sam init` to create a new Gradle-powered Powertools application - choose to use the **AWS Quick Start Templates**,
and then **Hello World Example with Powertools for AWS Lambda**, **Java 17** runtime, and finally **gradle**.

For general information on the deployed example itself, you can refer to the parent [README](../README.md)

## Configuration
SAM uses [template.yaml](template.yaml) to define the application's AWS resources.
This file defines the Lambda function to be deployed as well as API Gateway for it.

The build of the project is managed by Gradle, and configured in [build.gradle.kts](build.gradle.kts)
.

## Deploy the sample application
To get started, you can use the included template with SAM to run the build and deploy to your AWS environment:

```bash
sam build && sam deploy --guided
```

Once this is done to deploy the example, check out the instructions for getting started with SAM in
[the examples directory](../../README.md)

## Additional notes

You can watch the trace information or log information using the SAM CLI:
```bash
# Tail the logs
sam logs --tail $MY_STACK

# Tail the traces
sam traces --tail
```
39 changes: 39 additions & 0 deletions examples/powertools-examples-core/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
id("io.freefair.aspectj.post-compile-weaving") version "6.6.3"
kotlin("jvm") version "1.9.10"
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation("com.amazonaws:aws-lambda-java-core:1.2.2")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.13.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2.2")
implementation("com.amazonaws:aws-lambda-java-events:3.11.0")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2")
aspect("software.amazon.lambda:powertools-tracing:1.18.0-SNAPSHOT")
aspect("software.amazon.lambda:powertools-logging:1.18.0-SNAPSHOT")
aspect("software.amazon.lambda:powertools-metrics:1.18.0-SNAPSHOT")
testImplementation("junit:junit:4.13.2")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

tasks.compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

tasks.compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

// If using JDK 11 or higher, use the following instead:
//kotlin {
// jvmToolchain(11)
//}
Loading

0 comments on commit aed398b

Please sign in to comment.