|
| 1 | +# Amazon SQS to Amazon S3 integration using AWS Lambda |
| 2 | + |
| 3 | +This pattern creates an SQS queue, a Lambda function, an S3 bucket along with event source mapping for the Lambda function and appropriate permissions to enable the interfacing between these resources. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [SQS to Lambda to S3](https://serverlessland.com/patterns/sqs-lambda-s3) |
| 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 | +* **AWS Resources**<br> |
| 12 | + Creation of AWS resources requires the following: |
| 13 | + * [AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) - An AWS account is required for creating the various resources. If you do not already have one, then create an account and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 14 | + * [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) - This is required for cloning this repo. |
| 15 | + * [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) - Terraform is an IaC (Infrastructure as Code) software tool used for creating and managing AWS resources using a declarative configuration language. |
| 16 | + |
| 17 | +* **Test Setup**<br> |
| 18 | + In order to test this integration, the following are required: |
| 19 | + * [Python](https://wiki.python.org/moin/BeginnersGuide/Download) is required to run the test script. |
| 20 | + * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) is a prerequisite for using boto3 module in the test script. |
| 21 | + |
| 22 | +## Deployment Instructions |
| 23 | + |
| 24 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 25 | + ``` |
| 26 | + git clone https://github.com/aws-samples/serverless-patterns |
| 27 | + ``` |
| 28 | +1. Change directory to the pattern directory: |
| 29 | + ``` |
| 30 | + cd sqs-lambda-s3-terraform-python |
| 31 | + ``` |
| 32 | +1. Pick a unique name for the target S3 bucket eg. `my-bucket-20250329`. Replace the bucket name in following files: |
| 33 | +
|
| 34 | + * Lambda handler - `handler.py` |
| 35 | + |
| 36 | + ``` |
| 37 | + resp = s3_client.put_object( |
| 38 | + Body=str(request_body).encode(encoding="utf-8"), |
| 39 | + Bucket='my-bucket-20250329', |
| 40 | + Key=file_name |
| 41 | + ) |
| 42 | + ``` |
| 43 | + * Terraform configuration - `main.tf` |
| 44 | + |
| 45 | + ``` |
| 46 | + # S3 bucket |
| 47 | + resource "aws_s3_bucket" "event-storage" { |
| 48 | + bucket = "my-bucket-20250329" |
| 49 | + force_destroy = true |
| 50 | + tags = { |
| 51 | + Name = "event-storage" |
| 52 | + } |
| 53 | + } |
| 54 | + ``` |
| 55 | +1. Update the AWS region in the following files with the region, in which the resources will be created: |
| 56 | +
|
| 57 | + * Lambda handler - `handler.py` |
| 58 | + |
| 59 | + ``` |
| 60 | + config = Config(region_name='ap-south-1') |
| 61 | + ``` |
| 62 | + * Terraform configuration - `main.tf` |
| 63 | + |
| 64 | + ``` |
| 65 | + provider "aws" { |
| 66 | + region = "ap-south-1" |
| 67 | + } |
| 68 | + ``` |
| 69 | +
|
| 70 | +1. Compress the lambda handler to generate a zip file: |
| 71 | + |
| 72 | + ``` |
| 73 | + cp handler.py handler_1.py |
| 74 | + gzip -S .zip handler.py |
| 75 | + mv handler.py.zip sqs-lambda-s3.zip |
| 76 | + mv handler_1.py handler.py |
| 77 | + ``` |
| 78 | +
|
| 79 | +1. Deploy the AWS resources through Terraform: |
| 80 | + |
| 81 | + ``` |
| 82 | + terraform init -upgrade |
| 83 | + terraform fmt |
| 84 | + terraform validate |
| 85 | + terraform apply -auto-approve |
| 86 | + ``` |
| 87 | +
|
| 88 | +## How it works |
| 89 | +
|
| 90 | +The AWS resources created as a part of this integration are as follows: |
| 91 | +
|
| 92 | +* Amazon SQS queue |
| 93 | +* AWS Lambda function |
| 94 | +* Amazon S3 bucket |
| 95 | +* IAM policies and roles |
| 96 | +
|
| 97 | +The SQS queue is configured as a trigger for the Lambda function. Whenever a message is posted to the SQS queue, the Lambda function is invoked synchronously. This is useful in scenarios, where the message requires some pre-processing before storage. |
| 98 | +
|
| 99 | +## Testing |
| 100 | +
|
| 101 | +1. Create an IAM user which will be used for writing messages on the SQS queue |
| 102 | +
|
| 103 | +2. Add persmissions for the IAM user through the following inline policy: |
| 104 | + |
| 105 | + ``` |
| 106 | + { |
| 107 | + "Version": "2012-10-17", |
| 108 | + "Statement": [ |
| 109 | + { |
| 110 | + "Sid": "VisualEditor0", |
| 111 | + "Effect": "Allow", |
| 112 | + "Action": [ |
| 113 | + "sqs:DeleteMessage", |
| 114 | + "sqs:TagQueue", |
| 115 | + "sqs:UntagQueue", |
| 116 | + "sqs:ReceiveMessage", |
| 117 | + "sqs:SendMessage" |
| 118 | + ], |
| 119 | + "Resource": "arn:aws:sqs:[AWS_REGION]:[AWS_ACCOUNT]:event-collector-queue" |
| 120 | + } |
| 121 | + ] |
| 122 | + } |
| 123 | + ``` |
| 124 | + Replace `[AWS_REGION]` and `[AWS_ACCOUNT]` in the above policy before attaching with the IAM user. |
| 125 | +
|
| 126 | +1. Generate an access key pair (access key and secret access key) for IAM user in the AWS CLI. The key pair will be used while running the test script. |
| 127 | +
|
| 128 | +1. Update the AWS region in the test script `send_sqs_event.py` with the region, in which the SQS queue will be created: |
| 129 | + |
| 130 | + ``` |
| 131 | + config = Config(region_name='ap-south-1') |
| 132 | + ``` |
| 133 | +
|
| 134 | +1. Run the test script: |
| 135 | + |
| 136 | + ``` |
| 137 | + python send_sqs_event.py |
| 138 | + ``` |
| 139 | +
|
| 140 | +1. Enter the Access Key and Secret Access Key when prompted: |
| 141 | +
|
| 142 | + ``` |
| 143 | + Enter Access Key: ******************** |
| 144 | + Enter Secret Access Key: **************************************** |
| 145 | + ``` |
| 146 | +
|
| 147 | +1. Check the S3 bucket to see if a new JSON object has been created: |
| 148 | +
|
| 149 | + ``` |
| 150 | + aws s3 ls --region ap-south-1 my-bucket-20250329 |
| 151 | + ``` |
| 152 | +
|
| 153 | +## Cleanup |
| 154 | + |
| 155 | +1. Delete the AWS resources through Terraform: |
| 156 | +
|
| 157 | + ``` |
| 158 | + terraform apply -destroy -auto-approve |
| 159 | + ``` |
| 160 | +
|
| 161 | +## Resources |
| 162 | +
|
| 163 | +* [Amazon Simple Queue Service (Amazon SQS)](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html) |
| 164 | +* [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) |
| 165 | +* [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) |
| 166 | +
|
| 167 | +---- |
| 168 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 169 | +
|
| 170 | +SPDX-License-Identifier: MIT-0 |
0 commit comments