[Ruby] [AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)

3 minute read

Target

The image data saved in S3 is resized using the Lambda function and saved in another S3. The programming language uses Ruby.

Introduction

I am not very familiar with Lambda, so basically the purpose is to leave the basic operation of Lambda as a procedure. I tried a simple process of resizing (fixed size) the image put in S3 from the AWS console and saving it in another S3.

Assumption

Created two S3 buckets in advance.

Diagram

Untitled Diagram.png

Workflow

| Item number | Title | |:—-:|:————-| | 1 | Create and upload Lambda function | | 2 | Lambda environment variable setting | | 3 | Handler and Execution Role Settings | | 4 | Definition of Event Trigger | | 5 | Execution Role Settings | | 6 | Operation verification |

procedure

1. Creating and uploading a Lambda function

Select any function name and programming language to use for Lambda function. This time, I will write in Ruby 2.5. image.png

Create a zipped version of the Lambda function body to be deployed and an external library (mini_magick). Do the following on the working directory:

First, create a Gemfile

Gemfile



source'https://rubygems.org'
gem "mini_magick"

Install the gem under the working directory using bundle install --path vendor/bundle.


bundle install --path vendor/bundle

In addition, create a handler.rb that is the Lambda function body.

handler.rb



require'aws-sdk-s3'
require'base64'
require'mini_magick'

def resize_image(event:, context:)
  s3_client = Aws::S3::Client.new(
                           :region => ENV['REGION'],
                           :access_key_id => ENV['ACCESS_KEY'],
                           :secret_access_key => ENV['SECRET_ACCESS_KEY']
                          )
                          
  # Import images saved from S3 specified as event source
  key = event['Records'][0]['s3']['object']['key']
  image_file = s3_client.get_object(:bucket => ENV['BUCKET_BEFORE'], :key => key).body.read
  image = MiniMagick::Image.read(image_file)

  # Temporarily write resized image to /tmp in Lambda environment
  resized_tmp_file = "/tmp/#{key.delete("images/")}"
  image.resize("300x300").write(resized_tmp_file)

  # Execute upload
  s3_resource = Aws::S3::Resource.new()
  object = s3_resource.bucket(ENV['BUCKET_AFTER']).object(key).upload_file(resized_tmp_file)
end

Zip the handler.rb and the vender.

image.png

Upload the created zip file. Click “Upload .zip file” from the function code field action on the Lambda console tempsnip.png

Select handler.zip and select Save

tempsnip.png

2. Setting Lambda environment variables

Set the environment variables used in the Lambda function from the Lambda console.

tempsnip.png

3. Setting handler and execution role

It is said that the setting of which function in which file to call… is specified by the parameter called handler, Edit the contents according to the Lambda function created this time.

Click Edit in the basic settings on the Lambda console tempsnip.png

Enter a value in the format of executable file name (excluding extension).function name in the handler field and save it. tempsnip.png

4. Event trigger definition

Click Add trigger from the Lambda console tempsnip.png

The AWS service that will be the event trigger this time is S3 tempsnip.png

Make detailed settings for the trigger. This time, the event is triggered when the object is created in the images directory. tempsnip.png

It is possible for S3 to kick processing to this Lambda function by inserting the Enable trigger (setting Lambda function policy). Finally click on Add

tempsnip.png

5. Execution role settings

Furthermore, this time because it is necessary to access S3 from Lambda when saving the resized image, Grant an execution role with access to S3.

Click the “access permission” at the top of the Lambda console, then click the execution role name

tempsnip.png

Click on Attach policy tempsnip.png

After selecting AmazonS3FullAccess, attach policy tempsnip.png

6. Operation verification

Upload any image in the images directory of S3 specified as the event source. image.png

By the way, the uploaded image is here image.png

When I checked S3 specified as the image save destination after resizing, it was OK because the image was saved. image.png

The contents were also resized.image.png

Lambda logs can be viewed from Cloudwatch Logs. image.png

Articles I’ve referred to

Resize image API for one hour using AWS Lambda Ruby