Skip to content

Commit 65c44aa

Browse files
committed
CHANGES
Added variables.tf. Updated README with instructions to change values in variables.tf instead of lambda handler.py and main.tf. Updated README to exclude instruction for manually compressing lambda handler. Updated main.tf to use bucket name and AWS region from variables.tf, and also include environment variables for Lambda function. Updated handler.py to reference environments variables for AWS region and bucket name.
1 parent 3c664d5 commit 65c44aa

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

sqs-lambda-s3-terraform-python/README.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,21 @@ Learn more about this pattern at Serverless Land Patterns: [SQS to Lambda to S3]
2929
```
3030
cd sqs-lambda-s3-terraform-python
3131
```
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-
32+
33+
1. Pick a unique name for the target S3 bucket eg. `my-bucket-20250329`. Replace the bucket name and AWS region in `variables.tf`:
34+
7235
```
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
36+
variable aws_region_name {
37+
type = string
38+
default = "ap-south-1"
39+
description = "AWS Region"
40+
}
41+
42+
variable "s3_bucket_name" {
43+
type = string
44+
default = "my-bucket-20250329"
45+
description = "S3 Bucket name"
46+
}
7747
```
7848
7949
1. Deploy the AWS resources through Terraform:
@@ -98,7 +68,7 @@ The SQS queue is configured as a trigger for the Lambda function. Whenever a mes
9868
9969
## Testing
10070
101-
1. Create an IAM user which will be used for writing messages on the SQS queue
71+
1. Create an IAM user which will be used for writing messages on the SQS queue.
10272
10373
2. Add persmissions for the IAM user through the following inline policy:
10474

sqs-lambda-s3-terraform-python/handler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import boto3
22
from botocore.config import Config
3+
import os
34
import json
45

56
import boto3.s3
@@ -9,12 +10,13 @@ def lambda_handler(event, context):
910
print(context)
1011
file_name = 'request_' + json.loads(event['Records'][0]['body'])["uniqueID"] + '.json'
1112
request_body = event['Records'][0]['body']
12-
13-
config = Config(region_name='ap-south-1')
13+
aws_region = os.getenv('AWS_REGION')
14+
s3_bucket_ident = os.getenv('S3_BUCKET_NAME')
15+
config = Config(region_name=aws_region)
1416
s3_client = boto3.client('s3',config=config)
1517
resp = s3_client.put_object(
1618
Body=str(request_body).encode(encoding="utf-8"),
17-
Bucket='my-bucket-20250329',
19+
Bucket=s3_bucket_ident,
1820
Key=file_name
1921
)
2022

sqs-lambda-s3-terraform-python/main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ terraform {
1010
}
1111

1212
provider "aws" {
13-
region = "ap-south-1"
13+
region = var.aws_region_name
1414
}
1515

1616
data "archive_file" "lambda_handler_zip_file" {
@@ -27,6 +27,12 @@ resource "aws_lambda_function" "event-processor" {
2727
handler = "handler.lambda_handler"
2828
runtime = "python3.12"
2929
role = aws_iam_role.event-processor-exec-role.arn
30+
environment {
31+
variables = {
32+
AWS_REGION = var.aws_region_name
33+
S3_BUCKET_NAME = var.s3_bucket_name
34+
}
35+
}
3036
}
3137

3238
# Lambda execution role
@@ -149,7 +155,7 @@ resource "aws_sqs_queue_policy" "event-collector-policy" {
149155

150156
# S3 bucket
151157
resource "aws_s3_bucket" "event-storage" {
152-
bucket = "my-bucket-20250329"
158+
bucket = var.s3_bucket_name
153159
force_destroy = true
154160
tags = {
155161
Name = "event-storage"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable aws_region_name {
2+
type = string
3+
default = "ap-south-1"
4+
description = "AWS Region"
5+
}
6+
7+
variable "s3_bucket_name" {
8+
type = string
9+
default = "my-bucket-20250329"
10+
description = "S3 Bucket name"
11+
}

0 commit comments

Comments
 (0)