lambdaProject
Project: AWS Lambda + S3 Zipper (Ohio region - us-east-2)
Objective:
Trigger a Lambda function when a file is uploaded to an S3 bucket. The function compresses the file and uploads it to another bucket.
Step 1: Prepare Buckets
Go to the AWS S3 Console.
Create two buckets:
Source bucket:
my-first-test-bucket-aisalkynDestination bucket:
test-bucket-for-devopsstudents2
Step 2: Create IAM Role for Lambda
Go to the IAM Console.
Click "Create Role".
Choose "AWS service" and select "Lambda".
Click "Next".
Click "Create policy" to add the inline policy below.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReadFromSourceBucket",
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::my-first-test-bucket-aisalkyn/*"
},
{
"Sid": "AllowWriteToDestBucket",
"Effect": "Allow",
"Action": ["s3:PutObject"],
"Resource": "arn:aws:s3:::test-bucket-for-devopsstudents2/*"
}
]
}
Attach the policy to the role.
Name the role
lambda-log-s3-role.Finish creating the role.
Step 3: Write the Lambda Function
Create a file named lambda_function.py with the following content:
import boto3
import zipfile
import io
import os
s3 = boto3.client('s3')
def lambda_handler(event, context):
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = event['Records'][0]['s3']['object']['key']
destination_bucket = os.environ['DEST_BUCKET']
print(f"Zipping {source_key} from {source_bucket}...")
file_obj = s3.get_object(Bucket=source_bucket, Key=source_key)
file_content = file_obj['Body'].read()
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'a', zipfile.ZIP_DEFLATED) as zip_file:
zip_file.writestr(source_key, file_content)
zip_buffer.seek(0)
zipped_key = source_key + '.zip'
s3.upload_fileobj(zip_buffer, destination_bucket, zipped_key)
print(f"Uploaded zipped file as {zipped_key} to {destination_bucket}")
return {
'statusCode': 200,
'body': f"Zipped and uploaded {zipped_key}"
}
Step 4: Zip the Code
Open a terminal.
Navigate to the folder containing
lambda_function.py.Run:
zip function.zip lambda_function.py
Step 5: Deploy Lambda
Replace YOUR_ACCOUNT_ID with your actual AWS account ID.
aws lambda create-function \
--function-name log-s3-upload \
--runtime python3.12 \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-log-s3-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip \
--environment Variables={DEST_BUCKET=test-bucket-for-devopsstudents2} \
--region us-east-2
Step 6: Add S3 Trigger to Lambda
Go to Lambda Console.
Select
log-s3-upload.Go to Configuration > Triggers.
Click "Add trigger".
Select "S3", choose
my-first-test-bucket-aisalkyn, and enable "All object create events".Save.
Step 7: Upload Test File to S3
echo "This is a test file from Lambda demo" > test.txt
aws s3 cp test.txt s3://my-first-test-bucket-aisalkyn/
Step 8: Verify Lambda Execution in CloudWatch
Go to AWS Console > CloudWatch > Log groups.
Select
/aws/lambda/log-s3-upload.Click the latest log stream.
Check for output confirming successful zip and upload.
Step 9: Generate Pre-signed URL (optional)
aws s3 presign s3://test-bucket-for-devopsstudents2/test.txt.zip --region us-east-2
Open the returned URL in a browser to download the zipped file.
