Skip to content

Commit e4e2cd1

Browse files
jreijnjeromevdlscottgerring
authored
chore(v2): document use of aws-crt-client (#1092) (#1605)
* chore(v2): document use of aws-crt-client (#1092) * Update docs/FAQs.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update docs/FAQs.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update docs/FAQs.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * chore(v2): improve documentation based on feedback (#1092) * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update docs/FAQs.md Co-authored-by: Scott Gerring <[email protected]> * Update FAQs.md and set version --------- Co-authored-by: Jérôme Van Der Linden <[email protected]> Co-authored-by: Scott Gerring <[email protected]>
1 parent 59f9ba4 commit e4e2cd1

File tree

4 files changed

+155
-57
lines changed

4 files changed

+155
-57
lines changed

docs/FAQs.md

+98-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Frequently Asked Questions
66

77
## How can I use Powertools for AWS Lambda (Java) with Lombok?
88

9-
Poweretools uses `aspectj-maven-plugin` to compile-time weave (CTW) aspects into the project. In case you want to use `Lombok` or other compile-time preprocessor for your project, it is required to change `aspectj-maven-plugin` configuration to enable in-place weaving feature. Otherwise the plugin will ignore changes introduced by `Lombok` and will use `.java` files as a source.
9+
Powertools uses `aspectj-maven-plugin` to compile-time weave (CTW) aspects into the project. In case you want to use `Lombok` or other compile-time preprocessor for your project, it is required to change `aspectj-maven-plugin` configuration to enable in-place weaving feature. Otherwise the plugin will ignore changes introduced by `Lombok` and will use `.java` files as a source.
1010

1111
To enable in-place weaving feature you need to use following `aspectj-maven-plugin` configuration:
1212

@@ -29,7 +29,7 @@ To enable in-place weaving feature you need to use following `aspectj-maven-plug
2929

3030
## How can I use Powertools for AWS Lambda (Java) with Kotlin projects?
3131

32-
Poweretools uses `aspectj-maven-plugin` to compile-time weave (CTW) aspects into the project. When using it with Kotlin projects, it is required to `forceAjcCompile`.
32+
Powertools uses `aspectj-maven-plugin` to compile-time weave (CTW) aspects into the project. When using it with Kotlin projects, it is required to `forceAjcCompile`.
3333
No explicit configuration should be required for gradle projects.
3434

3535
To enable `forceAjcCompile` you need to use following `aspectj-maven-plugin` configuration:
@@ -47,3 +47,99 @@ To enable `forceAjcCompile` you need to use following `aspectj-maven-plugin` con
4747
</configuration>
4848
```
4949

50+
## How can I use Powertools for AWS Lambda (Java) with the AWS CRT HTTP Client?
51+
52+
Powertools uses the `url-connection-client` as the default HTTP client. The `url-connection-client` is a lightweight HTTP client, which keeps the impact on Lambda cold starts to a minimum.
53+
With the [announcement](https://aws.amazon.com/blogs/developer/announcing-availability-of-the-aws-crt-http-client-in-the-aws-sdk-for-java-2-x/) of the `aws-crt-client` a new HTTP client has been released, which offers faster SDK startup time and smaller memory footprint.
54+
55+
Unfortunately, replacing the `url-connection-client` dependency with the `aws-crt-client` will not immediately improve the lambda cold start performance and memory footprint,
56+
as the default version of the dependency contains native system libraries for all supported runtimes and architectures (Linux, MacOS, Windows, AMD64, ARM64, etc). This makes the CRT client portable, without the user having to consider _where_ their code will run, but comes at the cost of JAR size.
57+
58+
### Configuring dependencies
59+
60+
Using the `aws-crt-client` in your project requires the exclusion of the `url-connection-client` transitive dependency from the powertools dependency.
61+
62+
```xml
63+
<dependency>
64+
<groupId>software.amazon.lambda</groupId>
65+
<artifactId>powertools-parameters</artifactId>
66+
<version>2.0.0</version>
67+
<exclusions>
68+
<exclusion>
69+
<groupId>software.amazon.awssdk</groupId>
70+
<artifactId>url-connection-client</artifactId>
71+
</exclusion>
72+
</exclusions>
73+
</dependency>
74+
```
75+
Next, add the `aws-crt-client` and exclude the "generic" `aws-crt` dependency (contains all runtime libraries).
76+
Instead, set a specific classifier of the `aws-crt` to use the one for your target runtime: either `linux-x86_64` for a Lambda configured for x86 or `linux-aarch_64` for Lambda using arm64.
77+
78+
!!! note "You will need to add a separate maven profile to build and debug locally when your development environment does not share the target architecture you are using in Lambda."
79+
By specifying the specific target runtime, we prevent other target runtimes from being included in the jar file, resulting in a smaller Lambda package and improved cold start times.
80+
81+
```xml
82+
83+
<dependencies>
84+
<dependency>
85+
<groupId>software.amazon.awssdk</groupId>
86+
<artifactId>aws-crt-client</artifactId>
87+
<version>2.23.21</version>
88+
<exclusions>
89+
<exclusion>
90+
<groupId>software.amazon.awssdk.crt</groupId>
91+
<artifactId>aws-crt</artifactId>
92+
</exclusion>
93+
</exclusions>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>software.amazon.awssdk.crt</groupId>
98+
<artifactId>aws-crt</artifactId>
99+
<version>0.29.9</version>
100+
<classifier>linux-x86_64</classifier>
101+
</dependency>
102+
</dependencies>
103+
```
104+
105+
### Explicitly set the AWS CRT HTTP Client
106+
After configuring the dependencies, it's required to explicitly specify the AWS SDK HTTP client.
107+
Depending on the Powertools module, there is a different way to configure the SDK client.
108+
109+
The following example shows how to use the Lambda Powertools Parameters module while leveraging the AWS CRT Client.
110+
111+
```java hl_lines="11-16 19-20 22"
112+
import static software.amazon.lambda.powertools.parameters.transform.Transformer.base64;
113+
114+
import com.amazonaws.services.lambda.runtime.Context;
115+
import com.amazonaws.services.lambda.runtime.RequestHandler;
116+
import software.amazon.awssdk.services.ssm.SsmClient;
117+
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
118+
import software.amazon.lambda.powertools.parameters.ssm.SSMProvider;
119+
120+
public class RequestHandlerWithParams implements RequestHandler<String, String> {
121+
122+
// Get an instance of the SSMProvider with a custom HTTP client (aws crt).
123+
SSMProvider ssmProvider = SSMProvider
124+
.builder()
125+
.withClient(
126+
SsmClient.builder()
127+
.httpClient(AwsCrtHttpClient.builder().build())
128+
.build()
129+
)
130+
.build();
131+
132+
public String handleRequest(String input, Context context) {
133+
// Retrieve a single param
134+
String value = ssmProvider
135+
.get("/my/secret");
136+
// We might instead want to retrieve multiple parameters at once, returning a Map of key/value pairs
137+
// .getMultiple("/my/secret/path");
138+
139+
// Return the result
140+
return value;
141+
}
142+
}
143+
```
144+
The `aws-crt-client` was considered for adoption as the default HTTP client in Lambda Powertools for Java as mentioned in [Move SDK http client to CRT](https://github.com/aws-powertools/powertools-lambda-java/issues/1092),
145+
but due to the impact on the developer experience it was decided to stick with the `url-connection-client`.

docs/utilities/batch.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,19 @@ Handlers can be provided when building the batch processor and are available for
530530
For instance for DynamoDB:
531531

532532
```java
533-
BatchMessageHandler<DynamodbEvent, StreamsEventResponse> handler = new BatchMessageHandlerBuilder()
534-
.withDynamoDbBatchHandler()
535-
.withSuccessHandler((m) -> {
536-
// Success handler receives the raw message
537-
LOGGER.info("Message with sequenceNumber {} was successfully processed",
538-
m.getDynamodb().getSequenceNumber());
539-
})
540-
.withFailureHandler((m, e) -> {
541-
// Failure handler receives the raw message and the exception thrown.
542-
LOGGER.info("Message with sequenceNumber {} failed to be processed: {}"
543-
, e.getDynamodb().getSequenceNumber(), e);
544-
})
545-
.buildWithMessageHander(this::processMessage);
533+
BatchMessageHandler<DynamodbEvent, StreamsEventResponse> handler = new BatchMessageHandlerBuilder()
534+
.withDynamoDbBatchHandler()
535+
.withSuccessHandler((m) -> {
536+
// Success handler receives the raw message
537+
LOGGER.info("Message with sequenceNumber {} was successfully processed",
538+
m.getDynamodb().getSequenceNumber());
539+
})
540+
.withFailureHandler((m, e) -> {
541+
// Failure handler receives the raw message and the exception thrown.
542+
LOGGER.info("Message with sequenceNumber {} failed to be processed: {}"
543+
, e.getDynamodb().getSequenceNumber(), e);
544+
})
545+
.buildWithMessageHander(this::processMessage);
546546
```
547547

548548
!!! info

docs/utilities/large_messages.md

+43-42
Original file line numberDiff line numberDiff line change
@@ -97,48 +97,49 @@ of amazon-sns-java-extended-client-lib.
9797
Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes.
9898

9999
=== "Maven Java 11+"
100-
```xml hl_lines="3-7 16 18 24-27"
101-
<dependencies>
102-
...
103-
<dependency>
104-
<groupId>software.amazon.lambda</groupId>
105-
<artifactId>powertools-large-messages</artifactId>
106-
<version>{{ powertools.version }}</version>
107-
</dependency>
108-
...
109-
</dependencies>
110-
...
111-
<!-- configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
112-
<build>
113-
<plugins>
114-
...
115-
<plugin>
116-
<groupId>dev.aspectj</groupId>
117-
<artifactId>aspectj-maven-plugin</artifactId>
118-
<version>1.13.1</version>
119-
<configuration>
120-
<source>11</source> <!-- or higher -->
121-
<target>11</target> <!-- or higher -->
122-
<complianceLevel>11</complianceLevel> <!-- or higher -->
123-
<aspectLibraries>
124-
<aspectLibrary>
125-
<groupId>software.amazon.lambda</groupId>
126-
<artifactId>powertools-large-messages</artifactId>
127-
</aspectLibrary>
128-
</aspectLibraries>
129-
</configuration>
130-
<executions>
131-
<execution>
132-
<goals>
133-
<goal>compile</goal>
134-
</goals>
135-
</execution>
136-
</executions>
137-
</plugin>
138-
...
139-
</plugins>
140-
</build>
141-
```
100+
101+
```xml hl_lines="3-7 16 18 24-27"
102+
<dependencies>
103+
...
104+
<dependency>
105+
<groupId>software.amazon.lambda</groupId>
106+
<artifactId>powertools-large-messages</artifactId>
107+
<version>{{ powertools.version }}</version>
108+
</dependency>
109+
...
110+
</dependencies>
111+
...
112+
<!-- configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
113+
<build>
114+
<plugins>
115+
...
116+
<plugin>
117+
<groupId>dev.aspectj</groupId>
118+
<artifactId>aspectj-maven-plugin</artifactId>
119+
<version>1.13.1</version>
120+
<configuration>
121+
<source>11</source> <!-- or higher -->
122+
<target>11</target> <!-- or higher -->
123+
<complianceLevel>11</complianceLevel> <!-- or higher -->
124+
<aspectLibraries>
125+
<aspectLibrary>
126+
<groupId>software.amazon.lambda</groupId>
127+
<artifactId>powertools-large-messages</artifactId>
128+
</aspectLibrary>
129+
</aspectLibraries>
130+
</configuration>
131+
<executions>
132+
<execution>
133+
<goals>
134+
<goal>compile</goal>
135+
</goals>
136+
</execution>
137+
</executions>
138+
</plugin>
139+
...
140+
</plugins>
141+
</build>
142+
```
142143

143144
=== "Maven Java 1.8"
144145

docs/utilities/parameters.md

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ To simplify the use of the library, you can chain all method calls before a get.
544544
.withTransformation(json) // json is a static import from Transformer.json
545545
.withDecryption() // enable decryption of the parameter value
546546
.get("/my/param", MyObj.class); // finally get the value
547+
```
547548

548549
### Create your own Provider
549550
You can create your own custom parameter store provider by implementing a handful of classes:

0 commit comments

Comments
 (0)