Skip to content

feat(v2): Use AWS CRT as the default SDK client #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ When creating the `DynamoDBPersistenceStore`, you can set a custom [`DynamoDbCli
```java
DynamoDbClient.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.of(System.getenv(AWS_REGION_ENV)))
.build();
```
Expand Down Expand Up @@ -1073,7 +1073,7 @@ To unit test your function with DynamoDB Local, you can refer to this guide to [

// Initialize DynamoDBClient
client = DynamoDbClient.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.EU_WEST_1)
.endpointOverride(URI.create("http://localhost:" + port))
.credentialsProvider(StaticCredentialsProvider.create(
Expand Down Expand Up @@ -1151,7 +1151,7 @@ To unit test your function with DynamoDB Local, you can refer to this guide to [
public App() {
DynamoDbClientBuilder ddbBuilder = DynamoDbClient.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.httpClient(UrlConnectionHttpClient.builder().build());
.httpClient(AwsCrtHttpClient.builder().build());

if (System.getenv("AWS_SAM_LOCAL") != null) {
ddbBuilder.endpointOverride(URI.create("http://dynamo:8000"));
Expand Down
2 changes: 1 addition & 1 deletion docs/utilities/large_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ To interact with S3, the utility creates a default S3 Client :
=== "Default S3 Client"
```java
S3Client client = S3Client.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.of(System.getenv(AWS_REGION_ENV)))
.build();
```
Expand Down
4 changes: 2 additions & 2 deletions examples/powertools-examples-batch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.20.1</aspectj.version>
<sdk.version>2.21.1</sdk.version>
<sdk.version>2.22.0</sdk.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -50,7 +50,7 @@
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<artifactId>aws-crt-client</artifactId>
<version>${sdk.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult;
import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

public class DynamoDBWriter implements RequestHandler<ScheduledEvent, String> {
Expand All @@ -30,7 +30,7 @@ public class DynamoDBWriter implements RequestHandler<ScheduledEvent, String> {
public DynamoDBWriter() {
random = new SecureRandom();
DynamoDbClient dynamoDbClient = DynamoDbClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
.httpClientBuilder(AwsCrtHttpClient.builder())
.build();

enhancedClient = DynamoDbEnhancedClient.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.services.kinesis.KinesisClient;
import software.amazon.awssdk.services.kinesis.model.PutRecordsRequest;
import software.amazon.awssdk.services.kinesis.model.PutRecordsRequestEntry;
Expand All @@ -36,7 +36,7 @@ public class KinesisBatchSender implements RequestHandler<ScheduledEvent, String

public KinesisBatchSender() {
kinesisClient = KinesisClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.httpClient(AwsCrtHttpClient.create())
.build();
random = new SecureRandom();
objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.demo.batch.model.Product;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest;
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry;
Expand All @@ -35,7 +35,7 @@ public class SqsBatchSender implements RequestHandler<ScheduledEvent, String> {

public SqsBatchSender() {
sqsClient = SqsClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.httpClient(AwsCrtHttpClient.create())
.build();
random = new SecureRandom();
objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.lambda.powertools.parameters.secrets.SecretsParam;
import software.amazon.lambda.powertools.parameters.secrets.SecretsProvider;
import software.amazon.lambda.powertools.parameters.ssm.SSMParam;
import software.amazon.lambda.powertools.parameters.ssm.SSMProvider;
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;

public class ParametersFunction implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static final Logger log = LoggerFactory.getLogger(ParametersFunction.class);
Expand All @@ -49,9 +45,11 @@ public class ParametersFunction implements RequestHandler<APIGatewayProxyRequest

SSMProvider ssmProvider = SSMProvider
.builder()
.withTransformationManager(new TransformationManager())
.build();
SecretsProvider secretsProvider = SecretsProvider
.builder()
.withTransformationManager(new TransformationManager())
.build();

String simpleValue = ssmProvider.withMaxAge(30, SECONDS).get("/powertools-java/sample/simplekey");
Expand All @@ -67,43 +65,17 @@ public class ParametersFunction implements RequestHandler<APIGatewayProxyRequest

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {

log.info("\n=============== SSM Parameter Store ===============");
log.info("simplevalue={}, listvalue={}, b64value={}\n", simpleValue, listValue, b64value);
log.info("jsonobj={}\n", jsonObj);

log.info("allvalues (multiple):");
allValues.forEach((key, value) -> log.info("- {}={}\n", key, value));

log.info("\n=============== Secrets Manager ===============");
log.info("secretjson:");
secretJson.forEach((key, value) -> log.info("- {}={}\n", key, value));
log.info("secretjsonobj={}\n", secretJsonObj);

Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("X-Custom-Header", "application/json");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
String output = "{ \"message\": \"hello world\"}";

return response
.withStatusCode(200)
.withBody(output);
} catch (IOException e) {
return response
.withBody("{}")
.withStatusCode(500);
}
return response
.withStatusCode(200)
.withBody(output);
}

private String getPageContents(String address) throws IOException {
URL url = new URL(address);
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
<Appenders>
<Console name="JsonAppender" target="SYSTEM_OUT">
<LambdaJsonLayout compact="true" eventEol="true"/>
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
</Console>
</Appenders>
<Loggers>
<Logger name="JsonLogger" level="INFO" additivity="false">
<Logger name="org.demo.parameters" level="INFO" additivity="false">
<AppenderRef ref="JsonAppender"/>
</Logger>
<Root level="info">
Expand Down
4 changes: 3 additions & 1 deletion examples/powertools-examples-parameters/template.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
validation demo
parameters demo

Globals:
Function:
Expand All @@ -19,9 +19,11 @@ Resources:
Handler: org.demo.parameters.ParametersFunction::handleRequest
MemorySize: 512
Tracing: Active
Timeout: 60
Environment:
Variables:
LOG_LEVEL: INFO
JAVA_TOOL_OPTIONS: -XX:+TieredCompilation -XX:TieredStopAtLevel=1
Policies:
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Ref UserPwd
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<log4j.version>2.22.0</log4j.version>
<slf4j.version>2.0.7</slf4j.version>
<jackson.version>2.15.3</jackson.version>
<aws.sdk.version>2.21.0</aws.sdk.version>
<aws.sdk.version>2.23.9</aws.sdk.version>
<aws.xray.recorder.version>2.14.0</aws.xray.recorder.version>
<payloadoffloading-common.version>2.1.3</payloadoffloading-common.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -185,6 +185,11 @@
<artifactId>url-connection-client</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>io.burt</groupId>
<artifactId>jmespath-jackson</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion powertools-cloudformation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<artifactId>aws-crt-client</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;

/**
* Handler base class providing core functionality for sending responses to custom CloudFormation resources after
Expand All @@ -40,7 +40,7 @@ public abstract class AbstractCustomResourceHandler
* Creates a new Handler that uses the default HTTP client for communicating with custom CloudFormation resources.
*/
protected AbstractCustomResourceHandler() {
this.client = UrlConnectionHttpClient.create();
this.client = AwsCrtHttpClient.create();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.lambda.powertools.idempotency.Idempotency;
Expand All @@ -36,7 +36,7 @@ public class Function implements RequestHandler<Input, String> {
public Function() {
this(DynamoDbClient
.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.of(System.getenv("AWS_REGION")))
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -40,7 +40,7 @@ public class Function implements RequestHandler<SQSEvent, SQSBatchResponse> {
public Function() {
if (client == null) {
client = DynamoDbClient.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.of(System.getenv("AWS_REGION")))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -49,7 +49,7 @@ public class Function implements RequestHandler<SQSEvent, SQSBatchResponse> {
public Function() {
this(DynamoDbClient
.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.httpClient(AwsCrtHttpClient.builder().build())
.region(Region.of(System.getenv("AWS_REGION")))
.build());
}
Expand Down
2 changes: 1 addition & 1 deletion powertools-e2e-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<artifactId>aws-crt-client</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.junit.jupiter.api.Timeout;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -52,7 +52,7 @@
import software.amazon.lambda.powertools.utilities.JsonConfig;

public class BatchE2ET {
private static final SdkHttpClient httpClient = UrlConnectionHttpClient.builder().build();
private static final SdkHttpClient httpClient = AwsCrtHttpClient.builder().build();
private static final Region region = Region.of(System.getProperty("AWS_DEFAULT_REGION", "eu-west-1"));
private static Infrastructure infrastructure;
private static String functionName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -38,7 +38,7 @@
public class LargeMessageE2ET {

private static final Logger LOG = LoggerFactory.getLogger(LargeMessageE2ET.class);
private static final SdkHttpClient httpClient = UrlConnectionHttpClient.builder().build();
private static final SdkHttpClient httpClient = AwsCrtHttpClient.builder().build();
private static final Region region = Region.of(System.getProperty("AWS_DEFAULT_REGION", "eu-west-1"));

private static final S3Client s3Client = S3Client.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -49,7 +49,7 @@
public class LargeMessageIdempotentE2ET {

private static final Logger LOG = LoggerFactory.getLogger(LargeMessageIdempotentE2ET.class);
private static final SdkHttpClient httpClient = UrlConnectionHttpClient.builder().build();
private static final SdkHttpClient httpClient = AwsCrtHttpClient.builder().build();
private static final Region region = Region.of(System.getProperty("AWS_DEFAULT_REGION", "eu-west-1"));

private static final S3Client s3Client = S3Client.builder()
Expand Down
Loading