Resize the image data saved in S3 using the Lambda function and save it in another S3. The programming language uses Ruby.
I'm 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 (fixing size) the image put into S3 from the AWS console and saving it in another S3.
You have created two S3 buckets in advance.
Select any function name and programming language to use with your Lambda function.
This time, I will write it in Ruby2.5
.
Create a zipped version of the Lambda function body to be deployed and the external library (mini_magick). Perform the following operations 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 handler.rb
which is the main body of the Lambda function.
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 the image saved from S3 specified as the 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)
#Resized image in Lambda environment/Temporary write to tmp
resized_tmp_file = "/tmp/#{key.delete("images/")}"
image.resize("300x300").write(resized_tmp_file)
#Upload execution
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 vender
.
Upload the created zip file.
Click Upload .zip file
from the function code field action on the Lambda console
Select handler.zip
andSave
Set the environment variables used in the Lambda function from the Lambda console.
It is said that the setting of which function to call in which file is specified by a parameter called handler. Edit the content according to the Lambda function created this time.
Click Edit
for preferences on your Lambda console
Enter a value in the format of executable file name (excluding extension) .function name
in the handler field and save it.
Click Add Trigger
from the Lambda console
The AWS service used as the event trigger this time is S3
Make detailed settings for the trigger. This time, the event will be started when the object is created in the images directory.
By putting Trigger Enabled
, S3 can kick the process to this Lambda function (Lambda function policy setting).
Finally click Add
Furthermore, this time it is necessary to access S3 from Lambda when saving the resized image, so Grant an execute role with access to S3.
Click access rights
at the top of the Lambda console, then click the execution role name
Click Attach Policy
ʻAfter selecting AmazonS3FullAccess,
Attach Policy `
Upload any image in the S3 images directory specified as the event source.
By the way, the uploaded image is here
When I checked S3 specified as the image save destination after resizing, the image was saved, so it's OK.
The contents were also resized.
You can check Lambda logs from Cloudwatch Logs.
Image resizing API created in 1 hour using AWS Lambda Ruby
Recommended Posts