arundhaj

all that is technology

Using Terraform, creating Thumbnail Image in AWS Lambda with AWS S3 trigger

 

In this video, we will be writing an AWS Lambda function that creates a thumbnail image which is triggered whenever an image is uploaded to a S3 bucket with AWS S3 trigger and deployed using Terraform. Source code can be found here.

We'll first initialize the project

$ export AWS_PROFILE=arundhaj
$ cd CodePossibility
$ mkdir thumbnail_generation_lambda && cd thumbnail_generation_lambda

Then start writing the lambda function src\app.py. This function upon triggered, open the original file in the source bucket, create thumbnail and save it in the destination bucket.

We can then start writing the terraform script in main.tf, variables.tf and output.tf, to create IAM Policies, Roles, Lambda, S3 bucket, S3 trigger and Cloudwatch.

Terraform commands used are:

$ terraform init
$ terraform plan
$ terraform apply
$ terraform destory

AWS CLI commands used are:

$ aws iam list-policies --query 'Policies[?PolicyName == `thumbnail_s3_policy`]'
$ aws iam list-roles --query 'Roles[?RoleName == `thumbnail_lambda_role`]'
$ aws lambda list-functions --query 'Functions[?FunctionName == `thumbnail_generation_lambda`]'
$ aws s3 ls | grep cp-
$ aws s3 ls s3://cp-original-image-bucket
$ aws s3 ls s3://cp-thumbnail-image-bucket
$ aws logs describe-log-groups --query 'logGroups[?logGroupName == `/aws/lambda/thumbnail_generation_lambda`]'
$ aws logs tail /aws/lambda/thumbnail_generation_lambda --since 30s|5m|1h
$ aws s3 cp high_resolution_image.jpeg s3://cp-original-image-bucket
$ aws logs tail /aws/lambda/thumbnail_generation_lambda --since 30s|5m|1h

Hope this helps!

  Python

Comments