@Kiitan of Increments Inc. will be in charge of the 21st day of Increments × cyma (Ateam Inc.) Advent Calendar 2020.
Amazon Web Services (AWS) announced AWS Lambda container support at the annual event "AWS re: Invent 2020" that is being held online. In short, the image pushed to the ECR repository can now be used in Lambda functions. So I immediately touched it.
When a video is uploaded to S3, we will create a gif and create a Lambda function that will upload it to S3.
The file structure this time is as follows.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
└── app.rb
For app.rb, S3 just gets the video from, converts it to a gif, and uploads it again.
When saving a file by Lambda's method, you need to be careful because you do not have write permission unless it is under tmp /
.
app.rb
require 'aws-sdk-s3'
require 'streamio-ffmpeg'
module LambdaFunction
class Handler
def self.process(event:, context:)
s3_client = Aws::S3::Client.new(
:region => ENV['REGION'],
:access_key_id => ENV['ACCESS_KEY'],
:secret_access_key => ENV['SECRET_ACCESS_KEY']
)
input_bucket = event['Records'][0]['s3']['bucket']['name']
input_key = event['Records'][0]['s3']['object']['key']
file_name = input_key.split("/").last
file_path = "/tmp/#{file_name}"
#Get the video uploaded from S3/tmp/Save to
movie_file = s3_client.get_object(:bucket => input_bucket, :key => input_key).body.read
File.write(file_path, movie_file)
#Load video with ffmpeg and convert it to gif
movie = FFMPEG::Movie.new(file_path)
#Cut the start 6 seconds and convert it to gif
options = {frame_rate: '10', duration: '00:00:6.000'}
movie.transcode("#{file_path}.gif", options)
#S3 gif/Upload the gif created below
movie_object = s3_client.put_object({
:bucket => input_bucket,
:key => "gif/#{file_name}.gif",
:body => File.new("#{file_path}.gif"),
})
end
end
end
source 'https://rubygems.org'
ruby '2.7.2'
gem 'streamio-ffmpeg'
gem 'aws-sdk'
Next, regarding Dockerfile, not all base images can be used, and the Lambda Runtime API for Lambda must be implemented.
<!-Container for Lambda ruby-> in ECR Public gallery
This time, we will use amazon/aws-lambda-ruby: 2.7
as a base image.
There is nothing to be careful about other than the base image, it is a very simple Dockerfile that just downloads ffmpeg and installs gem.
FROM amazon/aws-lambda-ruby:2.7
RUN yum -y install wget tar xz xz-devel
RUN wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz \
&& tar Jxvf ./ffmpeg-git-amd64-static.tar.xz \
&& cp ./ffmpeg*amd64-static/ffmpeg /usr/local/bin/ \
&& cp ./ffmpeg*amd64-static/ffprobe /usr/local/bin/
COPY Gemfile Gemfile.lock ./
RUN bundle install --path vendor/bundle
COPY app.rb ./
CMD ["app.LambdaFunction::Handler.process"]
Pushing to ECR is not difficult, and you will not get lost because the repository is created and the push command on the repository screen is carefully written on the display.
aws ecr get-login-password --region REGION | docker login --username AWS --password-
stdin XXX.dkr.ecr.XXX.amazonaws.com
docker build -t ffmpeg-lambda .
docker tag ffmpeg-lambda:latest XXX.dkr.ecr.XXX.amazonaws.com/ffmpeg-lambda:latest
docker push XXX.dkr.ecr.XXX.amazonaws.com/ffmpeg-lambda:latest
Let's make Lambda. Select the container image and specify the container image you created earlier to complete the creation.
After that, create and set the trigger as usual
If you execute it as it is, it fails because the default timeout setting is 3 seconds. It was necessary to extend the timeout time from the basic settings.
If you upload the video to S3 and have a gif, you're done!
As for the impression, the implementation was completed very easily.
In addition, uploading of the conventional zip package is now allowed up to 10GB of image size compared to the maximum of 250MB, making it more flexible than before. I think that the range of available scenes has expanded because it can be installed with yum etc.
The 22nd day of Increments x cyma (Ateam Inc.) Advent Calendar 2020 @ O93 of Ateam Co., Ltd. is in charge!
Recommended Posts