Skip to content

Commit

Permalink
Merge pull request #2395 from msailes/producer-consumer
Browse files Browse the repository at this point in the history
Fixed the broken IAM permissions and updated to Java 21
  • Loading branch information
julianwood authored Sep 3, 2024
2 parents e4929b9 + c5ac647 commit 6bf1db5
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 66 deletions.
7 changes: 1 addition & 6 deletions apigw-lambda-snapstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>aws-lambda-sns-sqs-lambda</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -20,39 +19,33 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.107</version>
<version>2.23.20</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.0</version>
<version>3.11.4</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j2</artifactId>
<version>1.5.1</version>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.33</version>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand All @@ -62,24 +55,20 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sqs</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.1</version>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.1</version>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
<version>2.16.1</version>
</dependency>
</dependencies>

Expand All @@ -88,15 +77,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.12.1</version>
<configuration>
<source>11</source>
<target>11</target>
<release>21</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -116,7 +104,7 @@
<plugin>
<groupId>io.github.zlika</groupId>
<artifactId>reproducible-build-maven-plugin</artifactId>
<version>0.10</version>
<version>0.16</version>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -128,4 +116,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -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());

Expand Down
26 changes: 8 additions & 18 deletions lambda-sns-sqs-lambda-sam-java/README.md
Original file line number Diff line number Diff line change
@@ -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 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.

## 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

Expand All @@ -23,7 +24,6 @@ Topology

<img src="pattern-topology.png" alt="topology" width="80%"/>


## 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.
Expand All @@ -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
Expand Down Expand Up @@ -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/
LinkedIn: https://www.linkedin.com/in/razvanminciuna/
Description: Software Architect

8 changes: 4 additions & 4 deletions lambda-sns-sqs-lambda-sam-java/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,7 +14,7 @@ Resources:
Properties:
FunctionName: OrderProducer
Handler: com.example.producer.OrderProducer::handleRequest
CodeUri: target/sourceCode.zip
CodeUri: OrderProducer/
Policies:
- Statement:
- Effect: Allow
Expand All @@ -31,7 +31,7 @@ Resources:
Properties:
FunctionName: OrderConsumer
Handler: com.example.consumer.OrderConsumer::handleRequest
CodeUri: target/sourceCode.zip
CodeUri: OrderProducer/
Events:
MySQSEvent:
Type: SQS
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6bf1db5

Please sign in to comment.