Skip to content

Commit

Permalink
chore(v2): Merge down from main (#1574)
Browse files Browse the repository at this point in the history
* docs: HelloWorldStreamFunction in examples fails with sam (#1532)

* 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

* Adding validation step for IaC SAM

* Adding Terraform for Java projects IaC validator and linter

* Adding additional projects for SAM validation and matrix approach

* Refactoring stream function to process input logging example with a Lambda Function URL instead of APIGW.

* Demonstrating Java streaming response

* Refactoring stream function to process input logging example to return

* Update CONTRIBUTING.md

* fix: get trace id from system property when env var is not set (#1503)

* fix: check if XRAY Trace ID is present in System property

* chore: remove erroneous extra char in tests

* fix #1500 (#1506)

* feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT (#1510)

* chore: Addition of Warn Message If Invalid Annotation Key While Tracing #1511 (#1512)

* feat: ALC (#1514)

* handle AWS_LAMBDA_LOG configuration

* ALC documentation + code review

* update doc

* chore:Prep release 1.18.0 (#1515)

* chore:prep release 1.18.0

* update version

* update version in kotlin example

* maven local repo in gradle example

* update changelog

---------

Co-authored-by: scottgerring <[email protected]>

* chore: update version to next snapshot: 1-19.0-SNAPSHOT (#1516)

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* building only for LTS

* Add some more margin to the test pause (#1518)

* test: e2e tests with java 21 (#1517)

* e2e tests with java 21

* Run Java21 tests using the Java17 compiler

* Run all of the E2E tests in parallel, not just the first 3

* Try again

* .

* Let's try again

* Add some comment on Java21 to the repo

* Add caveat about lambda runtimes

* Clean up wording a little

---------

Co-authored-by: Scott Gerring <[email protected]>

* update doc for ALC (#1520)

* chore: Testing java21 aspectj pre-release (#1519)

* e2e tests with java 21

* use aspectj 1.9.21-SNAPSHOT

* Fix log4j2.xml missing in logging test for java21

* rollback double runtime

* remove comment

* keep aspectj 1.9.7 in parent for java8 compatibility

* use M1 instead of snapshot

* update documentation for aspectj

* update documentation for aspectj

---------

Co-authored-by: Jerome Van Der Linden <[email protected]>

* chore: Remove build cruft

* Adding context for using RequestStreamHandler

* removing pr_lint

* Update examples/powertools-examples-core/sam/src/main/java/helloworld/AppStream.java

Clarify usage of RequestStreamHandler.

Co-authored-by: Alexey Soshin <[email protected]>

---------

Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Scott Gerring <[email protected]>
Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Jérôme Van Der Linden <[email protected]>
Co-authored-by: Michele Ricciardi <[email protected]>
Co-authored-by: Alexey Soshin <[email protected]>
Co-authored-by: jdoherty <[email protected]>
Co-authored-by: Scott Gerring <[email protected]>
Co-authored-by: Jerome Van Der Linden <[email protected]>

* deps: bump aspectj to 1.9.21 for jdk21 (#1536)

* chore: SAM and Terraform IaC extracted from pr_build and simplified approach. (#1533)

* SAM and Terraform IaC extracted from pr_build and simplified approach.

* Update .github/workflows/pr_iac_lint.yml

Co-authored-by: Scott Gerring <[email protected]>

---------

Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Scott Gerring <[email protected]>

* chore: Remove empty CDK test (#1542)

* Remove CDK test

* Build core utilities so that sam validator can find them

* Remove CDK test

* Ok just build it all

* Remove the plan ...

* Remove terraform plan from iac lint

---------

Co-authored-by: Jérôme Van Der Linden <[email protected]>

* dependabot on v2 branch (#1548)

* chore: remove unecessary creds acquisition (#1572)

* Merge from main

---------

Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Jason Harris <[email protected]>
Co-authored-by: Jérôme Van Der Linden <[email protected]>
Co-authored-by: Michele Ricciardi <[email protected]>
Co-authored-by: Alexey Soshin <[email protected]>
Co-authored-by: jdoherty <[email protected]>
Co-authored-by: Jerome Van Der Linden <[email protected]>
  • Loading branch information
9 people authored Feb 22, 2024
1 parent 8151421 commit 1813e5f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 87 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ updates:
# Ignore Mockito 5.X.X as it does not support Java 8
- dependency-name: "org.mockito:mockito-*"
update-types: ["version-update:semver-major"]

- package-ecosystem: "maven"
directory: "/"
target-branch: "v2"
schedule:
interval: "weekly"
labels:
- "maven"
- "dependencies"
27 changes: 0 additions & 27 deletions .github/workflows/pr_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,6 @@ jobs:
if: ${{ matrix.java != '8' }}
working-directory: examples/powertools-examples-core-utilities/kotlin
run: ./gradlew build
- name: Setup Terraform
if: ${{ matrix.java == '11' }}
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 #v2.0.3
- name: Setup AWS credentials
if: ${{ matrix.java == '11' }}
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN_TO_ASSUME }}
aws-region: ${{ env.AWS_REGION }}
- name: Terraform validate
working-directory: examples/powertools-examples-core-utilities/terraform
if: ${{ matrix.java == '11' }}
run: |
terraform -version
terraform init -backend=false
terraform validate
terraform plan
- name: Setup Terraform lint
if: ${{ matrix.java == '11' }}
uses: terraform-linters/setup-tflint@a5a1af8c6551fb10c53f1cd4ba62359f1973746f # v3.1.1
- name: Terraform lint
working-directory: examples/powertools-examples-core-utilities/terraform
if: ${{ matrix.java == '11' }}
run: |
tflint --version
tflint --init
tflint -f compact
- name: Upload coverage to Codecov
uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1
if: ${{ matrix.java == '11' }} # publish results once
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/pr_iac_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Validate IaC

on:
push:
branches:
- main
- v2
pull_request:
branches:
- main
- v2
paths:
- 'examples/**'
jobs:
linter:
runs-on: ubuntu-latest
strategy:
matrix:
project: ["sam", "gradle", "kotlin"]
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Setup java JDK
uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2 # v3.11.0
with:
distribution: 'corretto'
java-version: 11
- name: Build Project
working-directory: .
run: |
mvn install -DskipTests
- name: Run SAM validator to check syntax of IaC templates - Java
working-directory: examples/powertools-examples-core/${{ matrix.project }}
run: |
sam build
sam validate --lint
- name: Setup Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 #v2.0.3
- name: Run Terraform validator to check syntax of IaC templates and produce a plan of changes
working-directory: examples/powertools-examples-core/terraform
run: |
mvn install
terraform -version
terraform init -backend=false
terraform validate
- name: Setup Terraform lint
uses: terraform-linters/setup-tflint@a5a1af8c6551fb10c53f1cd4ba62359f1973746f # v3.1.1
- name: Run Terraform lint to check for best practices, errors, deprecated syntax etc.
working-directory: examples/powertools-examples-core/terraform
run: |
tflint --version
tflint --init
tflint -f compact
3 changes: 0 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,6 @@ Use the following [dependency matrix](https://github.com/eclipse-aspectj/aspectj
| `11-17` | `1.9.20.1` |
| `21` | `1.9.21` |

_Note: 1.9.21 is not yet available and Java 21 not yet officially supported by aspectj, but you can already use the `1.9.21.M1`_


## Environment variables

!!! info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,43 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.nio.charset.StandardCharsets;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.metrics.Metrics;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class AppStream implements RequestStreamHandler {
private static final ObjectMapper mapper = new ObjectMapper();
private final static Logger log = LogManager.getLogger(AppStream.class);

@Override
@Logging(logEvent = true)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
Map map = mapper.readValue(input, Map.class);
// RequestStreamHandler can be used instead of RequestHandler for cases when you'd like to deserialize request body or serialize response body yourself, instead of allowing that to happen automatically
// Note that you still need to return a proper JSON for API Gateway to handle
// See Lambda Response format for examples: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
public void handleRequest(InputStream input, OutputStream output, Context context) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)))) {

System.out.println(map.size());
log.info("Received: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(reader)));

writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} ");
} catch (IOException e) {
log.error("Something has gone wrong: ", e);
}
}
}

Original file line number Diff line number Diff line change
@@ -1,48 +0,0 @@
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package cdk;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import software.amazon.awscdk.App;
import software.amazon.awscdk.assertions.Template;

public class CdkStackTest {

@Test
public void testStack() {
App app = new App();
CdkStack stack = new CdkStack(app, "test");

Template template = Template.fromStack(stack);

// There should be 2 lambda functions, one to handle regular input, and another for streaming
template.resourceCountIs("AWS::Lambda::Function", 2);

// API Gateway should exist
template.resourceCountIs("AWS::ApiGateway::RestApi", 1);

// API Gateway should have a path pointing to the regular Lambda
Map<String, String> resourceProperties = new HashMap<>();
resourceProperties.put("PathPart", "hello");
template.hasResourceProperties("AWS::ApiGateway::Resource", resourceProperties);

// API Gateway should have a path pointing to the streaming Lambda
resourceProperties = new HashMap<>();
resourceProperties.put("PathPart", "hellostream");
template.hasResourceProperties("AWS::ApiGateway::Resource", resourceProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,43 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.nio.charset.StandardCharsets;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.metrics.Metrics;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class AppStream implements RequestStreamHandler {
private static final ObjectMapper mapper = new ObjectMapper();
private final static Logger log = LogManager.getLogger(AppStream.class);

@Override
@Logging(logEvent = true)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
Map map = mapper.readValue(input, Map.class);
// RequestStreamHandler can be used instead of RequestHandler for cases when you'd like to deserialize request body or serialize response body yourself, instead of allowing that to happen automatically
// Note that you still need to return a proper JSON for API Gateway to handle
// See Lambda Response format for examples: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
public void handleRequest(InputStream input, OutputStream output, Context context) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)))) {

System.out.println(map.size());
log.info("Received: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(reader)));

writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} ");
} catch (IOException e) {
log.error("Something has gone wrong: ", e);
}
}
}

2 changes: 1 addition & 1 deletion powertools-e2e-tests/handlers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
<jdk>[21,)</jdk>
</activation>
<properties>
<aspectj.version>1.9.21.M1</aspectj.version>
<aspectj.version>1.9.21</aspectj.version>
</properties>
</profile>
</profiles>
Expand Down

0 comments on commit 1813e5f

Please sign in to comment.