Skip to content

Commit

Permalink
Update AWS SDK to v2
Browse files Browse the repository at this point in the history
The previous SDK used has been deprecated. As part of this change, we also need to include Log4J explicitly.

https://aws.amazon.com/de/blogs/developer/the-aws-sdk-for-java-1-x-is-in-maintenance-mode-effective-july-31-2024/
  • Loading branch information
MrSerth authored and Dome-GER committed Oct 15, 2024
1 parent 463b081 commit 1af6fcc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
11 changes: 8 additions & 3 deletions deploy/aws/java11Exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-apigatewaymanagementapi</artifactId>
<version>1.12.772</version>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apigatewaymanagementapi</artifactId>
<version>2.28.19</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
Expand Down
27 changes: 15 additions & 12 deletions deploy/aws/java11Exec/src/main/java/poseidon/App.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package poseidon;

import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApi;
import com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClientBuilder;
import com.amazonaws.services.apigatewaymanagementapi.model.PostToConnectionRequest;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.apigatewaymanagementapi.ApiGatewayManagementApiClient;
import software.amazon.awssdk.services.apigatewaymanagementapi.model.PostToConnectionRequest;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
Expand All @@ -12,8 +13,7 @@
import com.google.gson.JsonObject;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.net.URI;
import java.nio.file.Files;
import java.util.Base64;
import java.util.Map;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
private static final Gson gson = new Gson();

// gwClient is used to send messages back via the WebSocket connection.
private AmazonApiGatewayManagementApi gwClient;
private ApiGatewayManagementApiClient gwClient;

// connectionID helps to identify the WebSocket connection that has called this function.
private String connectionID;
Expand All @@ -77,8 +77,10 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayV2WebSocketEve
APIGatewayV2WebSocketEvent.RequestContext ctx = input.getRequestContext();
String[] domains = ctx.getDomainName().split("\\.");
String region = domains[domains.length-3];
this.gwClient = AmazonApiGatewayManagementApiClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://" + ctx.getDomainName() + "/" + ctx.getStage(), region))
this.gwClient = ApiGatewayManagementApiClient.builder()
.endpointOverride(URI.create("https://" + ctx.getDomainName() + "/" + ctx.getStage()))
.region(Region.of(region))
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
this.connectionID = ctx.getConnectionId();
this.disableOutput = input.getHeaders() != null && input.getHeaders().containsKey(disableOutputHeaderKey) && Boolean.parseBoolean(input.getHeaders().get(disableOutputHeaderKey));
Expand Down Expand Up @@ -189,9 +191,10 @@ private void sendMessage(WebSocketMessageType type, String data, Integer exitCod
if (this.disableOutput) {
System.out.println(gson.toJson(msg));
} else {
this.gwClient.postToConnection(new PostToConnectionRequest()
.withConnectionId(this.connectionID)
.withData(ByteBuffer.wrap(gson.toJson(msg).getBytes(StandardCharsets.UTF_8))));
this.gwClient.postToConnection(PostToConnectionRequest.builder()
.connectionId(this.connectionID)
.data(SdkBytes.fromUtf8String(gson.toJson(msg)))
.build());
}
}
}
16 changes: 16 additions & 0 deletions deploy/aws/java11Exec/src/main/java/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Configuration status="WARN">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Lambda"/>
</Root>
<Logger name="software.amazon.awssdk" level="WARN"/>
<Logger name="software.amazon.awssdk.request" level="DEBUG"/>
</Loggers>
</Configuration>

0 comments on commit 1af6fcc

Please sign in to comment.