Skip to main content

Command Palette

Search for a command to run...

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

  1. Go to the AWS S3 Console.

  2. Create two buckets:

    • Source bucket: my-first-test-bucket-aisalkyn

    • Destination bucket: test-bucket-for-devopsstudents2


Step 2: Create IAM Role for Lambda

  1. Go to the IAM Console.

  2. Click "Create Role".

  3. Choose "AWS service" and select "Lambda".

  4. Click "Next".

  5. 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/*"
    }
  ]
}
  1. Attach the policy to the role.

  2. Name the role lambda-log-s3-role.

  3. 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

  1. Open a terminal.

  2. Navigate to the folder containing lambda_function.py.

  3. 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

  1. Go to Lambda Console.

  2. Select log-s3-upload.

  3. Go to Configuration > Triggers.

  4. Click "Add trigger".

  5. Select "S3", choose my-first-test-bucket-aisalkyn, and enable "All object create events".

  6. 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

  1. Go to AWS Console > CloudWatch > Log groups.

  2. Select /aws/lambda/log-s3-upload.

  3. Click the latest log stream.

  4. 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.