Skip to content

Commit 03024be

Browse files
jasoniharrisJason HarrisscottgerringJason Harrisjeromevdl
authored
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]>
1 parent 628a2ca commit 03024be

File tree

1 file changed

+25
-4
lines changed
  • examples/powertools-examples-core/sam/src/main/java/helloworld

1 file changed

+25
-4
lines changed

Diff for: examples/powertools-examples-core/sam/src/main/java/helloworld/AppStream.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,43 @@
1717
import com.amazonaws.services.lambda.runtime.Context;
1818
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.io.OutputStream;
23-
import java.util.Map;
24+
import java.nio.charset.StandardCharsets;
25+
26+
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
2428
import software.amazon.lambda.powertools.logging.Logging;
2529
import software.amazon.lambda.powertools.metrics.Metrics;
2630

31+
import java.io.InputStreamReader;
32+
import java.io.BufferedReader;
33+
import java.io.BufferedWriter;
34+
import java.io.OutputStreamWriter;
35+
import java.io.PrintWriter;
36+
2737
public class AppStream implements RequestStreamHandler {
2838
private static final ObjectMapper mapper = new ObjectMapper();
39+
private final static Logger log = LogManager.getLogger(AppStream.class);
2940

3041
@Override
3142
@Logging(logEvent = true)
3243
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
33-
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
34-
Map map = mapper.readValue(input, Map.class);
44+
// 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
45+
// Note that you still need to return a proper JSON for API Gateway to handle
46+
// See Lambda Response format for examples: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
47+
public void handleRequest(InputStream input, OutputStream output, Context context) {
48+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
49+
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)))) {
3550

36-
System.out.println(map.size());
51+
log.info("Received: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(reader)));
52+
53+
writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} ");
54+
} catch (IOException e) {
55+
log.error("Something has gone wrong: ", e);
56+
}
3757
}
3858
}
59+

0 commit comments

Comments
 (0)