Skip to content

Commit c004aac

Browse files
committed
2 parents f005eb8 + a20913f commit c004aac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3337
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
## AWS API Gateway to AWS SQS
2+
3+
This pattern creates a REST API Gateway that directly integrates with AWS SQS to read messages.
4+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/
5+
6+
**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.
7+
8+
## Requirements
9+
10+
- 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
11+
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
12+
- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed
13+
- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
14+
15+
## Deployment Instructions
16+
17+
1. Clone the GitHub repository:
18+
```git clone https://github.com/aws-samples/serverless-patterns```
19+
20+
2. Navigate to the pattern directory:
21+
```cd APIGateway-SQS-ReceiveMessages```
22+
23+
3. Deploy using AWS SAM:
24+
```sam deploy --guided```
25+
26+
During the prompts:
27+
- Enter a stack name
28+
- Select your desired AWS Region
29+
- Allow SAM CLI to create IAM roles with required permissions
30+
31+
After initial deployment with sam deploy --guided, subsequent deployments can use sam deploy with the saved configuration (samconfig.toml).
32+
33+
Note the outputs from the deployment process, as they contain resource names and/or ARNs needed for testing.
34+
35+
## How it works
36+
37+
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.
38+
39+
The invoke URL supports query string parameters such as MaxNumberOfMessages=5, VisibilityTimeout=15, and AttributeName=All to customize the response.
40+
41+
For detailed parameter definitions, refer to the [AWS API Reference Documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html).
42+
43+
## Usage Example and Consideration
44+
45+
This pattern is ideal for scenarios where you need to retrieve messages from SQS via HTTPS without using AWS SDK. Common use cases include:
46+
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.
47+
48+
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.
49+
50+
## Testing
51+
52+
Follow these steps to test the integration:
53+
54+
1. First, send test messages to the queue:
55+
```chmod +x send_message_to_queue.sh```
56+
57+
```./send_message_to_queue.sh {queueURL} {number of messages}```
58+
59+
Example:
60+
```./send_message_to_queue.sh https://sqs.us-east-1.amazonaws.com/210987654321/myQueue 3```
61+
62+
2. Then, retrieve messages using the API Gateway endpoint:
63+
64+
Basic retrieval:
65+
```curl --location --request GET '{ApiEndpoint output value}'```
66+
67+
Advanced retrieval with parameters:
68+
```curl --location --request GET '{ApiEndpoint output value}?MaxNumberOfMessages=5&VisibilityTimeout=15&AttributeName=All'```
69+
70+
Parameter details are available in the [AWS API Reference Documentation](
71+
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html).
72+
73+
## Expected Output
74+
75+
When Queue is Empty:
76+
```json
77+
{
78+
"ReceiveMessageResponse": {
79+
"ReceiveMessageResult": {
80+
"messages": null
81+
},
82+
"ResponseMetadata": {
83+
"RequestId": "RequestId"
84+
}
85+
}
86+
}
87+
```
88+
89+
When Queue has Messages:
90+
```json
91+
{
92+
"ReceiveMessageResponse": {
93+
"ReceiveMessageResult": {
94+
"messages": [
95+
{
96+
"Attributes": null,
97+
"Body": "messageBody",
98+
"MD5OfBody": "MD5OfBody",
99+
"MD5OfMessageAttributes": null,
100+
"MessageAttributes": null,
101+
"MessageId": "Queue Message ID",
102+
"ReceiptHandle": "ReceiptHandle"
103+
}
104+
]
105+
},
106+
"ResponseMetadata": {
107+
"RequestId": "requestID"
108+
}
109+
}
110+
}
111+
```
112+
113+
## Cleanup
114+
115+
Delete the stack using SAM:
116+
```
117+
sam delete
118+
```
119+
120+
Confirm when prompted to delete the stack and its resources.
121+
122+
Note: You can also use `sam delete --no-prompts` to skip confirmation steps.
123+
124+
---
125+
126+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
127+
SPDX-License-Identifier: MIT-0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"title": "APIGateway-SQS-ReceiveMessages",
3+
"description": "Create a REST API Gateway to receive Messages from SQS queue.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"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.",
11+
"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.",
12+
"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."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/APIGateway-SQS-ReceiveMessages",
18+
"templateURL": "serverless-patterns/APIGateway-SQS-ReceiveMessages",
19+
"projectFolder": "APIGateway-SQS-ReceiveMessages",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Working on integration of AWS Services with API Gateway",
27+
"link": "https://repost.aws/knowledge-center/api-gateway-proxy-integrate-service"
28+
},
29+
{
30+
"text": "API Gateway Documentation",
31+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
32+
},
33+
{
34+
"text": "AWS SQS Documentation",
35+
"link": "https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html"
36+
},
37+
{
38+
"text": "AWS API Gateway Quotas",
39+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html"
40+
},
41+
{
42+
"text": "AWS SQS Quotas",
43+
"link": "https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html"
44+
}
45+
]
46+
},
47+
"deploy": {
48+
"text": [
49+
"sam deploy --guided"
50+
]
51+
},
52+
"testing": {
53+
"text": [
54+
"See the GitHub repo for detailed testing instructions."
55+
]
56+
},
57+
"cleanup": {
58+
"text": [
59+
"Delete the stack: <code>sam delete</code>"
60+
]
61+
},
62+
"authors": [
63+
{
64+
"name": "Yogesh Nain",
65+
"image": "link-to-your-photo.jpg",
66+
"bio": "Yogesh is Cloud Support Engineer and SME of Lambda, API Gateway at Amazon Web Services.",
67+
"linkedin": "yogesh-nain-a54133170"
68+
}
69+
],
70+
"patternArch": {
71+
"icon1": {
72+
"x": 20,
73+
"y": 50,
74+
"service": "apigw",
75+
"label": "API Gateway REST API"
76+
},
77+
"icon2": {
78+
"x": 80,
79+
"y": 50,
80+
"service": "sqs",
81+
"label": "Amazon SQS"
82+
},
83+
"line1": {
84+
"from": "icon1",
85+
"to": "icon2",
86+
"label": ""
87+
}
88+
}
89+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"title": "APIGateway-SQS-ReceiveMessages",
3+
"description": "Create a REST API Gateway to receive Messages from SQS queue.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"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.",
11+
"Users can simply call the GET Method of invoke URL( API Gateway) that is returned as part of the Stack Output.",
12+
"Invoke URL can also be used with query string parameters like MaxNumberOfMessages=5 VisibilityTimeout=15 AttributeName=All to get the desired output.",
13+
"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.",
14+
"Please also consider looking at AWS API Gateway and AWS SQS quotas for different limits supported by the services when working with this pattern."
15+
]
16+
},
17+
"gitHub": {
18+
"template": {
19+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/APIGateway-SQS-ReceiveMessages",
20+
"templateURL": "serverless-patterns/APIGateway-SQS-ReceiveMessages",
21+
"projectFolder": "APIGateway-SQS-ReceiveMessages",
22+
"templateFile": "template.yaml"
23+
}
24+
},
25+
"resources": {
26+
"bullets": [
27+
{
28+
"text": "Working on integration of AWS Services with API Gateway",
29+
"link": "https://repost.aws/knowledge-center/api-gateway-proxy-integrate-service"
30+
},
31+
{
32+
"text": "API Gateway Documentation",
33+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
34+
},
35+
{
36+
"text":"AWS SQS Documentation",
37+
"link":"https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html"
38+
},
39+
{
40+
"text":"AWS API Gateway Quotas",
41+
"link":"https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html"
42+
},
43+
{
44+
"text":"AWS SQS Quotas",
45+
"link":"https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html"
46+
}
47+
]
48+
},
49+
"deploy": {
50+
"text": [
51+
"sam deploy"
52+
]
53+
},
54+
"testing": {
55+
"text": [
56+
"See the GitHub repo for detailed testing instructions."
57+
]
58+
},
59+
"cleanup": {
60+
"text": [
61+
"Delete the stack: <code>sam delete</code>"
62+
]
63+
},
64+
"authors": [
65+
{
66+
"name": "Yogesh Nain",
67+
"image": "link-to-your-photo.jpg",
68+
"bio": "Yogesh is Cloud Support Engineer and SME of Lambda, API Gateway at Amazon Web Services.",
69+
"linkedin": "https://www.linkedin.com/in/yogesh-nain-a54133170/"
70+
}
71+
]
72+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
# Configuration
3+
MESSAGE_PREFIX="Test message"
4+
NUM_MESSAGES=5
5+
DELAY_SECONDS=0.1
6+
7+
# Color codes
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
NC='\033[0m' # No Color
11+
12+
# Check if queue URL is provided
13+
if [ -z "$1" ]; then
14+
echo -e "${RED}Error:${NC} Queue URL is required"
15+
echo -e "${GREEN}Usage:${NC} $0 <queue-url> [number-of-messages]"
16+
exit 1
17+
fi
18+
19+
QUEUE_URL="$1"
20+
21+
# Check if number of messages is provided as second argument
22+
if [ ! -z "$2" ]; then
23+
NUM_MESSAGES=$2
24+
fi
25+
26+
echo "Sending $NUM_MESSAGES messages to SQS queue: $QUEUE_URL"
27+
28+
for i in $(seq 1 $NUM_MESSAGES)
29+
do
30+
MESSAGE="$MESSAGE_PREFIX #$i: This is the $i message sent at $(date '+%Y-%m-%d %H:%M:%S')"
31+
32+
# Capture both stdout and stderr
33+
output=$(aws sqs send-message \
34+
--queue-url "$QUEUE_URL" \
35+
--message-body "$MESSAGE" \
36+
--message-attributes "{
37+
\"SequenceNumber\": {
38+
\"DataType\": \"Number\",
39+
\"StringValue\": \"$i\"
40+
}
41+
}" 2>&1)
42+
43+
# Check if command was successful
44+
if [ $? -ne 0 ]; then
45+
echo -e "${RED}Error:${NC} $output"
46+
exit 1
47+
fi
48+
49+
echo "Sent message $i"
50+
sleep $DELAY_SECONDS
51+
done
52+
53+
echo "Completed sending $NUM_MESSAGES messages"
54+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* SPDX-License-Identifier: MIT-0
3+
*/
4+
5+
'use strict'
6+
7+
exports.handler = async (event) => {
8+
// Lambda handler code
9+
console.log(JSON.stringify(event, 0, null))
10+
}

0 commit comments

Comments
 (0)