Skip to content

Commit 6da9fbc

Browse files
authored
Merge branch 'aws-samples:main' into achves-feature-apigw-lambda-transcribe-sam-js
2 parents 8aa8be1 + 5c54b45 commit 6da9fbc

File tree

863 files changed

+6193
-1441
lines changed

Some content is hidden

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

863 files changed

+6193
-1441
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"title": "Amazon API Gateway to Amazon SQS",
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+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: 'REST API Gateway integration to get messages from SQS (tag:
4+
apigw-sqs-rest, ReceiveMessage)'
5+
6+
Resources:
7+
# API Gateway
8+
AppApi:
9+
Type: AWS::ApiGateway::RestApi
10+
Properties:
11+
Name: apigw-rest-api-http-integration
12+
Description: REST API Integration with SQS
13+
14+
# GET Method with POST AWS Service integration to SQS
15+
RootMethodGet:
16+
Type: AWS::ApiGateway::Method
17+
Properties:
18+
RestApiId: !Ref AppApi
19+
ResourceId: !GetAtt AppApi.RootResourceId
20+
HttpMethod: GET
21+
AuthorizationType: NONE
22+
RequestParameters:
23+
method.request.querystring.VisibilityTimeout: false
24+
method.request.querystring.WaitTimeSeconds: false
25+
method.request.querystring.MaxNumberOfMessages: false
26+
method.request.querystring.AttributeName: false
27+
Integration:
28+
Credentials: !GetAtt APISQSGatewayRole.Arn
29+
IntegrationHttpMethod: POST
30+
IntegrationResponses:
31+
- StatusCode: '200'
32+
PassthroughBehavior: WHEN_NO_TEMPLATES
33+
RequestParameters:
34+
integration.request.header.Content-Type: '''application/x-www-form-urlencoded'''
35+
integration.request.querystring.Action: '''ReceiveMessage'''
36+
integration.request.querystring.AttributeName: method.request.querystring.AttributeName
37+
integration.request.querystring.MaxNumberOfMessages: method.request.querystring.MaxNumberOfMessages
38+
integration.request.querystring.VisibilityTimeout: method.request.querystring.VisibilityTimeout
39+
integration.request.querystring.WaitTimeSeconds: method.request.querystring.WaitTimeSeconds
40+
Type: AWS
41+
Uri: !Join
42+
- ''
43+
- - 'arn:aws:apigateway:'
44+
- !Ref AWS::Region
45+
- ':sqs:path/'
46+
- !Ref AWS::AccountId
47+
- /
48+
- !Sub ${MySqsQueue.QueueName}
49+
MethodResponses:
50+
- ResponseModels:
51+
application/json: Empty
52+
StatusCode: '200'
53+
Deployment:
54+
Type: AWS::ApiGateway::Deployment
55+
DependsOn:
56+
- RootMethodGet
57+
Properties:
58+
RestApiId: !Ref AppApi
59+
60+
Stage:
61+
Type: AWS::ApiGateway::Stage
62+
Properties:
63+
StageName: Prod
64+
RestApiId: !Ref AppApi
65+
DeploymentId: !Ref Deployment
66+
67+
# SQS Queue
68+
69+
MySqsQueue:
70+
Type: AWS::SQS::Queue
71+
72+
OrderServiceQueuePolicy:
73+
Type: AWS::SQS::QueuePolicy
74+
Properties:
75+
Queues:
76+
- !Ref MySqsQueue
77+
PolicyDocument:
78+
Version: '2012-10-17'
79+
Statement:
80+
- Sid: order-api-send-msg-sqs
81+
Effect: Allow
82+
Principal:
83+
Service: apigateway.amazonaws.com
84+
Action:
85+
- SQS:ReceiveMessage
86+
Resource: !GetAtt MySqsQueue.Arn
87+
88+
89+
# Roles
90+
91+
APISQSGatewayRole:
92+
Type: AWS::IAM::Role
93+
Properties:
94+
AssumeRolePolicyDocument:
95+
Version: 2012-10-17
96+
Statement:
97+
- Effect: Allow
98+
Principal:
99+
Service:
100+
- apigateway.amazonaws.com
101+
Action:
102+
- sts:AssumeRole
103+
Policies:
104+
- PolicyName: ApiGatewayLogsPolicy
105+
PolicyDocument:
106+
Version: 2012-10-17
107+
Statement:
108+
- Effect: Allow
109+
Action:
110+
- sqs:ReceiveMessage
111+
Resource:
112+
- !GetAtt MySqsQueue.Arn
113+
114+
Outputs:
115+
# API Gateway Invoke URL
116+
AppApiEndpoint:
117+
Description: API Endpoint
118+
Value: !Sub https://${AppApi}.execute-api.${AWS::Region}.amazonaws.com/Prod
119+
120+
QueueName:
121+
Description: SQS Name
122+
Value: !Sub ${MySqsQueue.QueueName}
123+
124+
QueueURL:
125+
Description: SQS Queue URL
126+
Value: !Ref MySqsQueue
127+

0 commit comments

Comments
 (0)