Skip to content

Commit 5b29e96

Browse files
committed
Remove notificationwlambda's dependency on awssdkv1
1 parent 510f2ff commit 5b29e96

File tree

7 files changed

+124
-111
lines changed

7 files changed

+124
-111
lines changed

build.sbt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ lazy val common = project
122122
"com.gu" %% "mobile-logstash-encoder" % "1.1.8",
123123
"com.gu" %% "simple-configuration-ssm" % simpleConfigurationVersion,
124124
"software.amazon.awssdk" % "regions" % "2.25.13",
125+
"software.amazon.awssdk" % "s3" % "2.29.24",
125126
"org.postgresql" % "postgresql" % "42.7.7",
126127
"ch.qos.logback" % "logback-core" % logbackVersion,
127128
"ch.qos.logback" % "logback-classic" % logbackVersion,
@@ -423,9 +424,9 @@ lazy val notificationworkerlambda = lambda("notificationworkerlambda", "notifica
423424
"com.google.protobuf" % "protobuf-java" % "4.31.1",
424425
"com.google.protobuf" % "protobuf-java-util" % "4.31.1",
425426
"com.amazonaws" % "aws-lambda-java-events" % "2.2.9",
426-
"com.amazonaws" % "aws-java-sdk-sqs" % awsSdkVersion,
427-
"com.amazonaws" % "aws-java-sdk-s3" % awsSdkVersion,
428-
"com.amazonaws" % "amazon-sqs-java-messaging-lib" % "2.1.4",
427+
"software.amazon.awssdk" % "s3" % "2.29.24",
428+
"software.amazon.awssdk" % "sqs" % "2.29.24",
429+
"software.amazon.awssdk" % "cloudwatch" % "2.29.24",
429430
"com.squareup.okhttp3" % "okhttp" % okHttpVersion,
430431
"org.playframework" %% "play-json" % playJsonVersion,
431432
"com.google.oauth-client" % "google-oauth-client" % googleOAuthClient,
@@ -450,6 +451,15 @@ lazy val notificationworkerlambda = lambda("notificationworkerlambda", "notifica
450451
// gRPC modules not needed for FCM HTTP API
451452
ExclusionRule("io.grpc", "grpc-xds"), // 34.24MB
452453
ExclusionRule("io.grpc", "grpc-netty"), // 9.28MB
454+
455+
// Exclude AWS SDK v1 except lambda-java-events which is needed for Lambda runtime
456+
ExclusionRule("com.amazonaws", "aws-java-sdk-s3"),
457+
ExclusionRule("com.amazonaws", "aws-java-sdk-sqs"),
458+
ExclusionRule("com.amazonaws", "aws-java-sdk-cloudwatch"),
459+
ExclusionRule("com.amazonaws", "aws-java-sdk-dynamodb"),
460+
ExclusionRule("com.amazonaws", "aws-java-sdk-athena"),
461+
ExclusionRule("com.amazonaws", "aws-java-sdk-core"),
462+
ExclusionRule("com.amazonaws", "amazon-sqs-java-messaging-lib"),
453463
),
454464
)
455465

common/src/main/scala/aws/S3.scala

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package aws
22

3-
import java.io.{ByteArrayInputStream, InputStream}
4-
5-
import com.amazonaws.services.s3.AmazonS3
6-
import com.amazonaws.services.s3.model.{ObjectMetadata, PutObjectRequest, PutObjectResult, S3Object}
7-
import com.amazonaws.util.IOUtils
3+
import software.amazon.awssdk.services.s3.S3Client
4+
import software.amazon.awssdk.services.s3.model.{GetObjectRequest, PutObjectRequest, PutObjectResponse}
5+
import software.amazon.awssdk.core.sync.RequestBody
86
import exception.TopicCounterException
97
import models.TopicCount
108
import org.slf4j.{Logger, LoggerFactory}
@@ -15,18 +13,19 @@ import scala.util.{Failure, Success, Try}
1513

1614
trait S3[T] {
1715

18-
def s3Client: AmazonS3
16+
def s3Client: S3Client
1917
def bucketName: String
2018
def path: String
2119
def logger: Logger
2220

23-
def put(data: Seq[T])(implicit format: Format[T]) : PutObjectResult = {
24-
val (inputStream, contentLength) = jsonInputStreamAndContentLength(data)
25-
val metaData: ObjectMetadata = new ObjectMetadata()
26-
metaData.setContentType("application/json")
27-
metaData.setContentLength(contentLength)
28-
val putObjectRequest: PutObjectRequest = new PutObjectRequest(bucketName, path, inputStream, metaData)
29-
s3Client.putObject(putObjectRequest)
21+
def put(data: Seq[T])(implicit format: Format[T]) : PutObjectResponse = {
22+
val jsonAsBytes = Json.toBytes(Json.toJson(data))
23+
val putObjectRequest = PutObjectRequest.builder()
24+
.bucket(bucketName)
25+
.key(path)
26+
.contentType("application/json")
27+
.build()
28+
s3Client.putObject(putObjectRequest, RequestBody.fromBytes(jsonAsBytes))
3029
}
3130

3231
def fetch()(implicit format: Format[T], executionException: ExecutionContext) : Future[List[T]] = {
@@ -39,7 +38,7 @@ trait S3[T] {
3938
}
4039

4140
private def parseS3Object()(implicit format: Format[T]) : List[T] = {
42-
Json.fromJson[List[T]](Json.parse(asString(s3Client.getObject(bucketName, path)))) match {
41+
Json.fromJson[List[T]](Json.parse(asString())) match {
4342
case JsSuccess(list, __) =>
4443
logger.debug(s"Got ${list.length} topic counts from s3")
4544
list
@@ -50,23 +49,17 @@ trait S3[T] {
5049
}
5150
}
5251

53-
private def asString(s3Object: S3Object): String = {
54-
val s3ObjectContent = s3Object.getObjectContent
55-
try {
56-
IOUtils.toString(s3ObjectContent)
57-
}
58-
finally {
59-
s3ObjectContent.close()
60-
}
61-
}
62-
63-
private def jsonInputStreamAndContentLength(data: Seq[T])(implicit format: Format[T]): (InputStream, Long) = {
64-
val jsonAsBytes = Json.toBytes(Json.toJson(data))
65-
(new ByteArrayInputStream(jsonAsBytes), jsonAsBytes.length)
52+
private def asString(): String = {
53+
val getObjectRequest = GetObjectRequest.builder()
54+
.bucket(bucketName)
55+
.key(path)
56+
.build()
57+
val response = s3Client.getObjectAsBytes(getObjectRequest)
58+
response.asUtf8String()
6659
}
6760
}
6861

69-
class TopicCountsS3(override val s3Client: AmazonS3, override val bucketName: String, override val path: String) extends S3[TopicCount] {
62+
class TopicCountsS3(override val s3Client: S3Client, override val bucketName: String, override val path: String) extends S3[TopicCount] {
7063
override def logger: Logger = LoggerFactory.getLogger(this.getClass)
7164
}
7265

notificationworkerlambda/src/main/scala/com/gu/notifications/worker/TopicCounterLambda.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.gu.notifications.worker
22

33
import aws.TopicCountsS3
44
import cats.effect.{ContextShift, IO}
5-
import com.amazonaws.auth.AWSCredentialsProviderChain
6-
import com.amazonaws.regions.Regions
7-
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
5+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
6+
import software.amazon.awssdk.regions.Region
7+
import software.amazon.awssdk.services.s3.S3Client
88
import com.gu.notifications.worker.utils.{Aws, Logging}
99
import db.{DatabaseConfig, RegistrationService}
1010
import doobie.util.transactor.Transactor
@@ -17,11 +17,11 @@ class TopicCounterLambda extends Logging {
1717

1818
def env = Env()
1919

20-
lazy val credentials: AWSCredentialsProviderChain = Aws.credentialsProvider
20+
lazy val credentials: AwsCredentialsProvider = Aws.credentialsProviderV2
2121

22-
lazy val s3Client: AmazonS3 = AmazonS3ClientBuilder.standard
23-
.withRegion(Regions.EU_WEST_1)
24-
.withCredentials(credentials)
22+
lazy val s3Client: S3Client = S3Client.builder()
23+
.region(Region.EU_WEST_1)
24+
.credentialsProvider(credentials)
2525
.build()
2626

2727
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
@@ -41,6 +41,6 @@ class TopicCounterLambda extends Logging {
4141

4242
def runLocally(): Unit = {
4343
topicCounts.handleRequest()
44-
s3Client.shutdown()
44+
s3Client.close()
4545
}
4646
}

notificationworkerlambda/src/main/scala/com/gu/notifications/worker/cleaning/CleaningClient.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.gu.notifications.worker.cleaning
22

33
import cats.effect.IO
4-
import com.amazonaws.regions.Regions
5-
import com.amazonaws.services.sqs.{AmazonSQS, AmazonSQSClient}
4+
import software.amazon.awssdk.services.sqs.SqsClient
5+
import software.amazon.awssdk.services.sqs.model.SendMessageRequest
6+
import software.amazon.awssdk.regions.Region
67
import com.gu.notifications.worker.models.InvalidTokens
78
import com.gu.notifications.worker.utils.Aws
89
import fs2.{Chunk, Pipe}
@@ -15,15 +16,17 @@ trait CleaningClient {
1516

1617
class CleaningClientImpl(sqsUrl: String) extends CleaningClient {
1718

18-
val sqsClient: AmazonSQS = AmazonSQSClient
19-
.builder()
20-
.withCredentials(Aws.credentialsProvider)
21-
.withRegion(Regions.EU_WEST_1)
22-
.build
19+
val sqsClient: SqsClient = SqsClient.builder()
20+
.region(Region.EU_WEST_1)
21+
.credentialsProvider(Aws.credentialsProviderV2)
22+
.build()
2323

2424
private def sendTokensToQueue(tokens: List[String])(implicit logger: Logger): Unit = {
2525
val json = Json.stringify(Json.toJson(InvalidTokens(tokens)))
26-
sqsClient.sendMessage(sqsUrl, json)
26+
sqsClient.sendMessage(SendMessageRequest.builder()
27+
.queueUrl(sqsUrl)
28+
.messageBody(json)
29+
.build())
2730
logger.info(s"Sent ${tokens.size} tokens for deletion via SQS")
2831
}
2932

notificationworkerlambda/src/main/scala/com/gu/notifications/worker/tokens/SqsDeliveryService.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package com.gu.notifications.worker.tokens
33
import java.util.concurrent.TimeUnit
44

55
import cats.effect._
6-
import com.amazonaws.handlers.AsyncHandler
7-
import com.amazonaws.regions.Regions
8-
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder
9-
import com.amazonaws.services.sqs.model.{SendMessageRequest, SendMessageResult}
6+
import software.amazon.awssdk.services.sqs.SqsAsyncClient
7+
import software.amazon.awssdk.services.sqs.model.SendMessageRequest
8+
import software.amazon.awssdk.regions.Region
109
import com.gu.notifications.worker.utils.Aws
1110
import fs2.Stream
1211
import play.api.libs.json.Json
@@ -24,24 +23,25 @@ class SqsDeliveryServiceImpl[F[_]](queueUrl: String)(implicit ece: ExecutionCont
2423
F: Async[F],
2524
T: Timer[F]
2625
) extends SqsDeliveryService[F] {
27-
val amazonSQSAsyncClient = AmazonSQSAsyncClientBuilder.standard()
28-
.withCredentials(Aws.credentialsProvider)
29-
.withRegion(Regions.EU_WEST_1)
26+
val sqsAsyncClient = SqsAsyncClient.builder()
27+
.region(Region.EU_WEST_1)
28+
.credentialsProvider(Aws.credentialsProviderV2)
3029
.build()
3130

3231
def send(chunkedTokensBatch: ChunkedTokens)(oncomplete: Either[Throwable, Unit] => Unit): Unit = {
33-
val sendMessageRequest = new SendMessageRequest()
34-
.withMessageBody(Json.stringify(Json.toJson(chunkedTokensBatch)))
35-
.withQueueUrl(queueUrl)
32+
val sendMessageRequest = SendMessageRequest.builder()
33+
.messageBody(Json.stringify(Json.toJson(chunkedTokensBatch)))
34+
.queueUrl(queueUrl)
35+
.build()
3636

37-
val handler = new AsyncHandler[SendMessageRequest, SendMessageResult] {
38-
override def onError(exception: Exception): Unit = oncomplete(Left(new Exception(Json.stringify(Json.toJson(chunkedTokensBatch)), exception)))
39-
40-
override def onSuccess(request: SendMessageRequest, result: SendMessageResult): Unit = oncomplete(Right(()))
41-
}
42-
amazonSQSAsyncClient.sendMessageAsync(
43-
sendMessageRequest,
44-
handler)
37+
sqsAsyncClient.sendMessage(sendMessageRequest)
38+
.whenComplete { (_, error) =>
39+
if (error != null) {
40+
oncomplete(Left(new Exception(Json.stringify(Json.toJson(chunkedTokensBatch)), error)))
41+
} else {
42+
oncomplete(Right(()))
43+
}
44+
}
4545
}
4646

4747

notificationworkerlambda/src/main/scala/com/gu/notifications/worker/utils/Aws.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.gu.notifications.worker.utils
33
import utils.MobileAwsCredentialsProvider
44

55
object Aws {
6-
lazy val credentialsProvider = new MobileAwsCredentialsProvider
6+
lazy val credentialsProviderV2 = MobileAwsCredentialsProvider.mobileAwsCredentialsProviderv2
77
}

0 commit comments

Comments
 (0)