From 6fd0201ef979faf3dbf520f4cb187cf1df827254 Mon Sep 17 00:00:00 2001 From: Mark Sailes Date: Thu, 25 Jul 2024 13:00:41 +0100 Subject: [PATCH 1/2] Fixed the broken IAM permissions Updated to Java 21 --- apigw-lambda-snapstart/README.md | 7 +-- .../{ => OrderProducer}/pom.xml | 46 +++++++------------ .../{ => OrderProducer}/src/lambda-zip.xml | 0 .../com/example/consumer/OrderConsumer.java | 0 .../com/example/consumer/OrderSQSMessage.java | 0 .../java/com/example/model/OrderCreated.java | 0 .../com/example/producer/OrderProducer.java | 0 .../main/java/com/example/utils/SNSUtils.java | 12 ++--- lambda-sns-sqs-lambda-sam-java/README.md | 28 ++++------- lambda-sns-sqs-lambda-sam-java/template.yml | 8 ++-- 10 files changed, 34 insertions(+), 67 deletions(-) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/pom.xml (74%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/lambda-zip.xml (100%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/main/java/com/example/consumer/OrderConsumer.java (100%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/main/java/com/example/consumer/OrderSQSMessage.java (100%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/main/java/com/example/model/OrderCreated.java (100%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/main/java/com/example/producer/OrderProducer.java (100%) rename lambda-sns-sqs-lambda-sam-java/{ => OrderProducer}/src/main/java/com/example/utils/SNSUtils.java (79%) diff --git a/apigw-lambda-snapstart/README.md b/apigw-lambda-snapstart/README.md index 49738712b..c75f7ae41 100644 --- a/apigw-lambda-snapstart/README.md +++ b/apigw-lambda-snapstart/README.md @@ -74,12 +74,7 @@ To test the Lambda function you can POST a unicorn JSON payload to the '/unicorn ```bash curl --location --request POST $(aws cloudformation describe-stacks --stack-name unicorn-store --query "Stacks[0].Outputs[?OutputKey=='UnicornEndpoint'].OutputValue" --output text)'/unicorns' \ --header 'Content-Type: application/json' \ ---data-raw '{ -"name": "Something", -"age": "Older", -"type": "Animal", -"size": "Very big" -}' | jq +--data-raw '{"name": "Something","age": "Older","type": "Animal","size": "Very big"}' | jq ``` ## Measuring the results diff --git a/lambda-sns-sqs-lambda-sam-java/pom.xml b/lambda-sns-sqs-lambda-sam-java/OrderProducer/pom.xml similarity index 74% rename from lambda-sns-sqs-lambda-sam-java/pom.xml rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/pom.xml index 68aea78c6..dc0b278d4 100644 --- a/lambda-sns-sqs-lambda-sam-java/pom.xml +++ b/lambda-sns-sqs-lambda-sam-java/OrderProducer/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 com.example @@ -9,8 +7,9 @@ 1.0-SNAPSHOT - 11 - 11 + 21 + 21 + 21 UTF-8 @@ -20,7 +19,7 @@ software.amazon.awssdk bom - 2.17.107 + 2.23.20 pom import @@ -28,31 +27,25 @@ - - junit - junit - 4.13.2 - test - com.amazonaws aws-lambda-java-core - 1.2.1 + 1.2.3 com.amazonaws aws-lambda-java-events - 3.11.0 + 3.11.4 com.amazonaws aws-lambda-java-log4j2 - 1.5.1 + 1.6.0 org.slf4j slf4j-jdk14 - 1.7.33 + 2.0.12 software.amazon.awssdk @@ -62,24 +55,20 @@ software.amazon.awssdk sns - - software.amazon.awssdk - sqs - com.fasterxml.jackson.core jackson-core - 2.13.1 + 2.16.1 com.fasterxml.jackson.core jackson-annotations - 2.13.1 + 2.16.1 com.fasterxml.jackson.core jackson-databind - 2.13.4.2 + 2.16.1 @@ -88,15 +77,14 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.12.1 - 11 - 11 + 21 maven-assembly-plugin - 3.3.0 + 3.6.0 package @@ -116,7 +104,7 @@ io.github.zlika reproducible-build-maven-plugin - 0.10 + 0.16 package @@ -128,4 +116,4 @@ - \ No newline at end of file + diff --git a/lambda-sns-sqs-lambda-sam-java/src/lambda-zip.xml b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/lambda-zip.xml similarity index 100% rename from lambda-sns-sqs-lambda-sam-java/src/lambda-zip.xml rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/lambda-zip.xml diff --git a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/consumer/OrderConsumer.java b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/consumer/OrderConsumer.java similarity index 100% rename from lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/consumer/OrderConsumer.java rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/consumer/OrderConsumer.java diff --git a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/consumer/OrderSQSMessage.java b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/consumer/OrderSQSMessage.java similarity index 100% rename from lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/consumer/OrderSQSMessage.java rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/consumer/OrderSQSMessage.java diff --git a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/model/OrderCreated.java b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/model/OrderCreated.java similarity index 100% rename from lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/model/OrderCreated.java rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/model/OrderCreated.java diff --git a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/producer/OrderProducer.java b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/producer/OrderProducer.java similarity index 100% rename from lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/producer/OrderProducer.java rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/producer/OrderProducer.java diff --git a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/utils/SNSUtils.java b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/utils/SNSUtils.java similarity index 79% rename from lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/utils/SNSUtils.java rename to lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/utils/SNSUtils.java index fe8409975..1af5b9ad0 100644 --- a/lambda-sns-sqs-lambda-sam-java/src/main/java/com/example/utils/SNSUtils.java +++ b/lambda-sns-sqs-lambda-sam-java/OrderProducer/src/main/java/com/example/utils/SNSUtils.java @@ -1,28 +1,22 @@ package com.example.utils; -import com.amazonaws.services.lambda.runtime.events.SNSEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.*; - public class SNSUtils { + private static final SnsClient SNS_CLIENT = SnsClient.create(); private static final Logger LOGGER = LoggerFactory.getLogger(SNSUtils.class); public static String publishMessage(String message, String topicName){ - SnsClient snsClient = SnsClient.builder() - .region(Region.EU_CENTRAL_1) - .build(); - - String topicArn = getTopicArn(snsClient, topicName); + String topicArn = getTopicArn(SNS_CLIENT, topicName); PublishRequest request = PublishRequest.builder() .message(message) .topicArn(topicArn) .build(); - PublishResponse result = snsClient.publish(request); + PublishResponse result = SNS_CLIENT.publish(request); LOGGER.info("[SNS messageID]" + result.messageId()); diff --git a/lambda-sns-sqs-lambda-sam-java/README.md b/lambda-sns-sqs-lambda-sam-java/README.md index 0cf749523..3cf8eec8f 100644 --- a/lambda-sns-sqs-lambda-sam-java/README.md +++ b/lambda-sns-sqs-lambda-sam-java/README.md @@ -1,14 +1,15 @@ ## Description -###Producer-Consumer pattern using Lambda - SNS - SQS - Lambda -This pattern creates an two Lambda function, a producer and a consumer, an SNS topic and an SQS queue using SAM and Java 11. +### Producer-Consumer pattern using Lambda - SNS - SQS - Lambda -Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. +This pattern creates an two Lambda function, a producer and a consumer, an SNS topic and an SQS queue using SAM and Java 21. +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. ## Language: -###This is a Maven project which uses Java 11 and AWS SDK + +### This is a Maven project which uses Java 21 and AWS SDK ## Framework @@ -23,7 +24,6 @@ Topology topology - ## Description The SAM template contains all the information to deploy AWS resources(the Lambda functions, an SNS topic and an SNS queue) and also the permission required by these service to communicate. @@ -34,20 +34,13 @@ The producer Lambda function will be invoked with a JSON payload, see example, a An SQS queue which is subscribed to the SNS topic will receive the message. The Lambda consumer will be invoked and consume the message from the queue. - ## Deployment commands ```` -mvn clean package - -# create an S3 bucket where the source code will be stored: -aws s3 mb s3://hdeed22ad2ed - -# copy the source code located in the target folder: -aws s3 cp target/sourceCode.zip s3://hdeed22ad2ed +sam build # SAM will deploy the CloudFormation stack described in the template.yml file: -sam deploy --s3-bucket hdeed22ad2ed --stack-name orders-stack --capabilities CAPABILITY_IAM +sam deploy --guided --stack-name orders-stack ##INVOKE lambda function OrderProducer aws lambda invoke --function-name OrderProducer --cli-binary-format raw-in-base64-out --payload '{"userId":"ff334esq3dad", "total":"25.99"}' response.json @@ -86,10 +79,7 @@ aws cloudformation delete-stack --stack-name orders-stack * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed - - ## Author bio Name: Razvan Minciuna -Linkedin: https://www.linkedin.com/in/razvanminciuna/ -Description: Software Architect - +LinkedIn: https://www.linkedin.com/in/razvanminciuna/ +Description: Software Architect \ No newline at end of file diff --git a/lambda-sns-sqs-lambda-sam-java/template.yml b/lambda-sns-sqs-lambda-sam-java/template.yml index 0a71625c5..60adbdd01 100644 --- a/lambda-sns-sqs-lambda-sam-java/template.yml +++ b/lambda-sns-sqs-lambda-sam-java/template.yml @@ -4,7 +4,7 @@ Description: aws-lambda-sns-sqs-lambda (uksb-1tthgi812) (tag:lambda-sns-sqs-lamb Globals: Function: - Runtime: java11 + Runtime: java21 MemorySize: 512 Timeout: 25 @@ -14,7 +14,7 @@ Resources: Properties: FunctionName: OrderProducer Handler: com.example.producer.OrderProducer::handleRequest - CodeUri: target/sourceCode.zip + CodeUri: OrderProducer/ Policies: - Statement: - Effect: Allow @@ -31,7 +31,7 @@ Resources: Properties: FunctionName: OrderConsumer Handler: com.example.consumer.OrderConsumer::handleRequest - CodeUri: target/sourceCode.zip + CodeUri: OrderProducer/ Events: MySQSEvent: Type: SQS @@ -64,7 +64,7 @@ Resources: Principal: Service: "sns.amazonaws.com" Resource: !GetAtt OrdersSqsQueue.Arn - Action: SQS:SendMessage + Action: sqs:SendMessage Condition: ArnEquals: aws:SourceArn: !Ref OrderSNSTopic From c5ac6471938c3b832d87a9d25cb43b2e36192eba Mon Sep 17 00:00:00 2001 From: Udit Parikh Date: Sun, 1 Sep 2024 20:56:05 +0530 Subject: [PATCH 2/2] Update README.md --- lambda-sns-sqs-lambda-sam-java/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lambda-sns-sqs-lambda-sam-java/README.md b/lambda-sns-sqs-lambda-sam-java/README.md index 3cf8eec8f..89ec3b6a8 100644 --- a/lambda-sns-sqs-lambda-sam-java/README.md +++ b/lambda-sns-sqs-lambda-sam-java/README.md @@ -3,7 +3,7 @@ ### Producer-Consumer pattern using Lambda - SNS - SQS - Lambda -This pattern creates an two Lambda function, a producer and a consumer, an SNS topic and an SQS queue using SAM and Java 21. +This pattern creates two Lambda functions, a producer and a consumer, an SNS topic and an SQS queue using SAM and Java 21. Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. @@ -82,4 +82,4 @@ aws cloudformation delete-stack --stack-name orders-stack ## Author bio Name: Razvan Minciuna LinkedIn: https://www.linkedin.com/in/razvanminciuna/ -Description: Software Architect \ No newline at end of file +Description: Software Architect