|
| 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 |
0 commit comments