Skip to content
Merged
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
127 changes: 127 additions & 0 deletions APIGateway-SQS-ReceiveMessages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
## AWS API Gateway to AWS SQS

This pattern creates a REST API Gateway that directly integrates with AWS SQS to read messages.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/

**Important**: This application uses various AWS services that incur costs beyond the Free Tier usage. Please review the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

- An [AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) with appropriate IAM permissions to make service calls and manage AWS resources
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed
- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Clone the GitHub repository:
```git clone https://github.com/aws-samples/serverless-patterns```

2. Navigate to the pattern directory:
```cd APIGateway-SQS-ReceiveMessages```

3. Deploy using AWS SAM:
```sam deploy --guided```

During the prompts:
- Enter a stack name
- Select your desired AWS Region
- Allow SAM CLI to create IAM roles with required permissions

After initial deployment with sam deploy --guided, subsequent deployments can use sam deploy with the saved configuration (samconfig.toml).

Note the outputs from the deployment process, as they contain resource names and/or ARNs needed for testing.

## How it works

This pattern creates an Amazon API Gateway REST API endpoint that directly connects to Amazon SQS using service integrations. Users can retrieve messages by calling the GET method of the invoke URL (API Gateway) provided in the Stack Output.

The invoke URL supports query string parameters such as MaxNumberOfMessages=5, VisibilityTimeout=15, and AttributeName=All to customize the response.

For detailed parameter definitions, refer to the [AWS API Reference Documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html).

## Usage Example and Consideration

This pattern is ideal for scenarios where you need to retrieve messages from SQS via HTTPS without using AWS SDK. Common use cases include:
Web applications that need to poll SQS queues for new messages, Mobile applications requiring secure access to SQS messages, Third-party integrations where direct AWS SDK access isn't practical, Legacy system integrations that support only HTTP/HTTPS protocols, Development environments where simplified queue access is preferred.

Please review [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html) and [SQS Quotas](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html) for service limits before implementation.

## Testing

Follow these steps to test the integration:

1. First, send test messages to the queue:
```chmod +x send_message_to_queue.sh```

```./send_message_to_queue.sh {queueURL} {number of messages}```

Example:
```./send_message_to_queue.sh https://sqs.us-east-1.amazonaws.com/210987654321/myQueue 3```

2. Then, retrieve messages using the API Gateway endpoint:

Basic retrieval:
```curl --location --request GET '{ApiEndpoint output value}'```

Advanced retrieval with parameters:
```curl --location --request GET '{ApiEndpoint output value}?MaxNumberOfMessages=5&VisibilityTimeout=15&AttributeName=All'```

Parameter details are available in the [AWS API Reference Documentation](
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html).

## Expected Output

When Queue is Empty:
```json
{
"ReceiveMessageResponse": {
"ReceiveMessageResult": {
"messages": null
},
"ResponseMetadata": {
"RequestId": "RequestId"
}
}
}
```

When Queue has Messages:
```json
{
"ReceiveMessageResponse": {
"ReceiveMessageResult": {
"messages": [
{
"Attributes": null,
"Body": "messageBody",
"MD5OfBody": "MD5OfBody",
"MD5OfMessageAttributes": null,
"MessageAttributes": null,
"MessageId": "Queue Message ID",
"ReceiptHandle": "ReceiptHandle"
}
]
},
"ResponseMetadata": {
"RequestId": "requestID"
}
}
}
```

## Cleanup

Delete the stack using SAM:
```
sam delete
```

Confirm when prompted to delete the stack and its resources.

Note: You can also use `sam delete --no-prompts` to skip confirmation steps.

---

Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
89 changes: 89 additions & 0 deletions APIGateway-SQS-ReceiveMessages/apigateway-sqs-receivemessages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"title": "APIGateway-SQS-ReceiveMessages",
"description": "Create a REST API Gateway to receive Messages from SQS queue.",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This serverless pattern creates an Amazon API Gateway REST API endpoint that directly integrates with Amazon Simple Queue Service (SQS) to read messages from a queue.",
"Users can simply call the GET method of the invoke URL to interact with the SQS queue. The invoke URL supports query string parameters such as MaxNumberOfMessages, VisibilityTimeout, and AttributeName to customize the output.",
"This pattern is particularly useful for frontend applications that need to interact with SQS via HTTPS protocol to read messages from the queue. It eliminates the need for AWS SDK integration, as requests to SQS can be made through simple HTTP calls.When implementing this pattern, please consider the AWS API Gateway and AWS SQS quotas to ensure compliance with the service limits."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/APIGateway-SQS-ReceiveMessages",
"templateURL": "serverless-patterns/APIGateway-SQS-ReceiveMessages",
"projectFolder": "APIGateway-SQS-ReceiveMessages",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Working on integration of AWS Services with API Gateway",
"link": "https://repost.aws/knowledge-center/api-gateway-proxy-integrate-service"
},
{
"text": "API Gateway Documentation",
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
},
{
"text": "AWS SQS Documentation",
"link": "https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html"
},
{
"text": "AWS API Gateway Quotas",
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html"
},
{
"text": "AWS SQS Quotas",
"link": "https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>"
]
},
"authors": [
{
"name": "Yogesh Nain",
"image": "link-to-your-photo.jpg",
"bio": "Yogesh is Cloud Support Engineer and SME of Lambda, API Gateway at Amazon Web Services.",
"linkedin": "yogesh-nain-a54133170"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "apigw",
"label": "API Gateway REST API"
},
"icon2": {
"x": 80,
"y": 50,
"service": "sqs",
"label": "Amazon SQS"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": ""
}
}
}
72 changes: 72 additions & 0 deletions APIGateway-SQS-ReceiveMessages/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"title": "APIGateway-SQS-ReceiveMessages",
"description": "Create a REST API Gateway to receive Messages from SQS queue.",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This pattern creates an Amazon API gateway REST API endpoint. The endpoint uses service integrations to directly connect to Amazon SQS that reads messages from the SQS.",
"Users can simply call the GET Method of invoke URL( API Gateway) that is returned as part of the Stack Output.",
"Invoke URL can also be used with query string parameters like MaxNumberOfMessages=5 VisibilityTimeout=15 AttributeName=All to get the desired output.",
"Useful with Frontend application that would like to interact with sqs via https protocol to read messages from the SQS queue Avoid any useage of AWS SDK since request to SQS can be made via simple http request.",
"Please also consider looking at AWS API Gateway and AWS SQS quotas for different limits supported by the services when working with this pattern."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/APIGateway-SQS-ReceiveMessages",
"templateURL": "serverless-patterns/APIGateway-SQS-ReceiveMessages",
"projectFolder": "APIGateway-SQS-ReceiveMessages",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Working on integration of AWS Services with API Gateway",
"link": "https://repost.aws/knowledge-center/api-gateway-proxy-integrate-service"
},
{
"text": "API Gateway Documentation",
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
},
{
"text":"AWS SQS Documentation",
"link":"https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html"
},
{
"text":"AWS API Gateway Quotas",
"link":"https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html"
},
{
"text":"AWS SQS Quotas",
"link":"https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>"
]
},
"authors": [
{
"name": "Yogesh Nain",
"image": "link-to-your-photo.jpg",
"bio": "Yogesh is Cloud Support Engineer and SME of Lambda, API Gateway at Amazon Web Services.",
"linkedin": "https://www.linkedin.com/in/yogesh-nain-a54133170/"
}
]
}
54 changes: 54 additions & 0 deletions APIGateway-SQS-ReceiveMessages/send_message_to_queue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
# Configuration
MESSAGE_PREFIX="Test message"
NUM_MESSAGES=5
DELAY_SECONDS=0.1

# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Check if queue URL is provided
if [ -z "$1" ]; then
echo -e "${RED}Error:${NC} Queue URL is required"
echo -e "${GREEN}Usage:${NC} $0 <queue-url> [number-of-messages]"
exit 1
fi

QUEUE_URL="$1"

# Check if number of messages is provided as second argument
if [ ! -z "$2" ]; then
NUM_MESSAGES=$2
fi

echo "Sending $NUM_MESSAGES messages to SQS queue: $QUEUE_URL"

for i in $(seq 1 $NUM_MESSAGES)
do
MESSAGE="$MESSAGE_PREFIX #$i: This is the $i message sent at $(date '+%Y-%m-%d %H:%M:%S')"

# Capture both stdout and stderr
output=$(aws sqs send-message \
--queue-url "$QUEUE_URL" \
--message-body "$MESSAGE" \
--message-attributes "{
\"SequenceNumber\": {
\"DataType\": \"Number\",
\"StringValue\": \"$i\"
}
}" 2>&1)

# Check if command was successful
if [ $? -ne 0 ]; then
echo -e "${RED}Error:${NC} $output"
exit 1
fi

echo "Sent message $i"
sleep $DELAY_SECONDS
done

echo "Completed sending $NUM_MESSAGES messages"

10 changes: 10 additions & 0 deletions APIGateway-SQS-ReceiveMessages/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*/

'use strict'

exports.handler = async (event) => {
// Lambda handler code
console.log(JSON.stringify(event, 0, null))
}
Loading