|
| 1 | +# Amazon API Gateway HTTP API to AWS Simple Queue Service (SQS) to Lambda |
| 2 | + |
| 3 | +This pattern creates an Amazon API Gateway HTTP API with a `send` route that send message to a SQS queue. The Amazon API Gateway HTTP API has basic CORS configured. Upon receiving message, SQS will trigger a Lambda function to process the message. The function will only `print` the message. The function is written in TypeScript. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/apigw-http-api-sqs-lambda-sls](https://serverlessland.com/patterns/apigw-http-api-sqs-lambda-sls). |
| 6 | + |
| 7 | +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](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git CLI](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed |
| 14 | +* [NodeJS](https://nodejs.org/en/download/) (LTS version) installed |
| 15 | +* [Serverless Framework CLI](https://www.serverless.com/framework/docs/getting-started) installed |
| 16 | + |
| 17 | +## Deployment Instructions |
| 18 | + |
| 19 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 20 | + |
| 21 | + ``` sh |
| 22 | + git clone https://github.com/aws-samples/serverless-patterns |
| 23 | + ``` |
| 24 | + |
| 25 | +1. Change directory to the pattern directory: |
| 26 | + |
| 27 | + ``` sh |
| 28 | + cd serverless-patterns/apigw-http-api-sqs-lambda-sls |
| 29 | + ``` |
| 30 | + |
| 31 | +1. From the command line, use npm to install the development dependencies: |
| 32 | + |
| 33 | + ``` sh |
| 34 | + npm install |
| 35 | + ``` |
| 36 | + |
| 37 | +1. From the command line, use Serverless Framework to deploy the AWS resources for the pattern as specified in the serverless.yml file: |
| 38 | + |
| 39 | + ``` sh |
| 40 | + serverless deploy --verbose |
| 41 | + ``` |
| 42 | + |
| 43 | + The above command will deploy resources to `us-east-1` region by default. You can override the target region with `--region <region>` CLI option, e.g. |
| 44 | + |
| 45 | + ``` sh |
| 46 | + serverless deploy --verbose --region us-west-2 |
| 47 | + ``` |
| 48 | + |
| 49 | +1. Note the `ApiEndpoint` output from the Serverless Framework deployment process. You will use this value for testing. |
| 50 | + |
| 51 | +## How it works |
| 52 | + |
| 53 | +The API Gateway handles the incoming API requests and sends the `$request.body.MessageBody` as a message to an SQS queue. A Lambda function is triggered with the posted message. |
| 54 | + |
| 55 | +## Testing |
| 56 | + |
| 57 | +### Sending a new test message to API Gateway endpoint |
| 58 | + |
| 59 | +To test the endpoint first send data using the following command. Be sure to update the endpoint with endpoint of your stack. |
| 60 | + |
| 61 | +``` sh |
| 62 | +curl --location --request POST 'ApiEndpoint output value' --header 'Content-Type: application/json' \ |
| 63 | +--data-raw '{ |
| 64 | + "MessageBody":"hello" |
| 65 | +}' |
| 66 | +``` |
| 67 | + |
| 68 | +### Expected output |
| 69 | + |
| 70 | +```xml |
| 71 | +<?xml version="1.0"?><SendMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"> |
| 72 | + <SendMessageResult> |
| 73 | + <MessageId>xxxxxx</MessageId> |
| 74 | + <MD5OfMessageBody>xxxxxx</MD5OfMessageBody> |
| 75 | + </SendMessageResult> |
| 76 | + <ResponseMetadata> |
| 77 | + <RequestId>xxxx</RequestId> |
| 78 | + </ResponseMetadata> |
| 79 | +</SendMessageResponse> |
| 80 | +``` |
| 81 | + |
| 82 | +### CloudWatch logs |
| 83 | + |
| 84 | +Open AWS CloudWatch Console and navigate to [/aws/lambda/apigw-http-api-sqs-lambda-sls-prod-logEvent](https://console.aws.amazon.com/cloudwatch/home#logsV2:log-groups/log-group/$252Faws$252Flambda$252Fapigw-http-api-sqs-lambda-sls-prod-logEvent) log group. |
| 85 | +You should be able to see a new Event Stream with the Received Event information, and the number of records received, logged into the stream. |
| 86 | + |
| 87 | +## Cleanup |
| 88 | + |
| 89 | +1. Delete the stack |
| 90 | + |
| 91 | + ```sh |
| 92 | + serverless remove --verbose |
| 93 | + ``` |
| 94 | + |
| 95 | +1. Confirm the stack has been deleted |
| 96 | + |
| 97 | + ```sh |
| 98 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'apigw-http-api-sqs-lambda-sls-prod')].StackStatus" |
| 99 | + ``` |
| 100 | + |
| 101 | + Expected output |
| 102 | + |
| 103 | + ```json |
| 104 | + [ |
| 105 | + "DELETE_COMPLETE" |
| 106 | + ] |
| 107 | + ``` |
| 108 | + |
| 109 | + NOTE: You might need to add `--region <region>` option to AWS CLI command if you AWS CLI default region does not match the one, that you used for the Serverless Framework deployment. |
| 110 | + |
| 111 | +---- |
| 112 | +Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 113 | + |
| 114 | +SPDX-License-Identifier: MIT-0 |
0 commit comments