Upload and Download Files From Aws Ruby on Rails

It's five o'clock on a Fri afternoon. There are no new issues reports and everything is looking shine. Your programme of a relaxing weekend is in sight when y'all get a call—the website you look later on isn't responding.

Yikes.

AWS Lambda minimizes the take a chance of this truly terrifying event from happening by taking care of server maintenance while yous focus on coding robust applications.

AWS Lambda is a function-as-a-service platform that stores and executes your lawmaking when triggered by an Amazon Spider web Service (AWS) effect. These events range from making an API call, to saving a file, to updating a database. You only pay for the execution of the role. There's no other associated running costs or server maintenance, and y'all don't need to worry well-nigh server load, considering AWS will handle scaling. That gives y'all a consistent experience, no thing how much your function is used.

This post introduces AWS Lambda, discusses when you should and shouldn't use information technology, and takes an in-depth look at how to create your first Reddish function using the AWS console.

The origins of AWS Lambda

At the 2014 AWS re:Invent briefing, Amazon introduced Lambda to the world. Dr. Tim Wagner, general manager of AWS Lambda at the time, explained the benefits of using Lambda over EC2, the popular compute engine. Renting virtual machines in the course of EC2 instances from AWS brings flexibility and choice, but the tradeoff is that there's an unnecessary toll when idle. Plus, at that place's the additional complication maintaining the infrastructure even when it'south not in use.

The benefits of Lambda

The benefits of AWS Lambda, as explained by Tim Wagner at re:Invent 2014, are

  • Reduced complexity. At that place'southward a fixed operating system, fixed software language, and a fixed version.
  • No infrastructure to manage. AWS owns and manages the infrastructure. Yous only pay for the fourth dimension your code runs.
  • Implicit scaling. AWS will handle scaling, no matter how much you use your functions.

An caption of AWS events

An AWS issue is a JSON bulletin containing the origin and associated outcome information, depending on the service. For case, if you add a file to the Amazon Unproblematic Storage Service (S3), this will create an effect containing the origin every bit the Unproblematic Storage Service, the location of the file, and data that an item was added.

The services that can enhance events that tin can trigger a function are

  • DynamoDB
  • Simple Queue Service
  • Elastic Load Balancing (Application Load Balancer)
  • Cognito
  • Lex
  • Alexa
  • API Gateway
  • CloudFront ([email protected])
  • Kinesis Data Firehose
  • AWS Elementary Storage Service
  • Amazon Uncomplicated Notification Service
  • Simple Electronic mail Service
  • CloudFormation
  • CloudWatch Logs
  • CloudWatch Events
  • CodeCommit
  • Config

Events trigger a Lambda part execution

Events trigger a Lambda function to execute. It'south executed either straight or is added to a queue to exist executed in the future. If you'd like the total list of how services create events, y'all tin cheque out the AWS documentation. In brusque, a Lambda part subscribes to the notification-specific event from a specific source and will ignore all other events from any other source.

Programming linguistic communication support

AWS Lambda currently supports Cherry-red, Java, Get, PowerShell, Node.js, C#, Ruddy and Python. AWS also periodically introduces support for new programming languages

Use cases where Lambda excels

Lambda excels when the task has one or more of the post-obit characteristics:

  • It'due south isolated. Tasks that perform just one part—for example, updating a database or storing a file—are bang-up.
  • It's asynchronous. We're talking nigh low priority tasks that don't expect an immediate response. This could be sending an electronic mail or SMS.
  • It broadcasts.That means it'due south updating lots of different systems from i activity, like creating a repository on GitHub.
  • There are big peaks in demand. Lambda works well when, during unlike times of the year or mean solar day, the need is different. This could occur during peak holiday season.
  • There'southward low throughput.This occurs when a job happens rarely, such as a sensor that only reports on change (like a door).
  • It'southward a image or mock.Lambda shines when you're working with a proof of concept before committing more than time to a project.
  • Information technology'due south stateless.Nothing needs to be stored in memory.

Use cases where Lambda is not then useful

Lambda doesn't excel where the job has the following characteristics:

  • It's long-running. Functions are express to xv minutes, and then any tasks that exceed that will fail.
  • Information technology has a consequent load. It may exist cheaper to use a dedicated instance if the load is predictable.
  • It's intensive. Functions can only allocate 3GB of retentivity, so any intensive tasks will neglect.
  • Information technology requires a specific operating organization or linguistic communication. If tasks are tied to a specific operating system and accept limited language support, Lambda might not aid.
  • Information technology has a stateful system. Here, we're talking about tasks that rely on a state to be stored between functions—for example, storing and retrieving files locally on the server.

A use case for writing a Lambda office

Imagine y'all're building a solution for users to store files. You cull AWS S3 as the location of your files because it's like shooting fish in a barrel to manage and scale. The users can name the images annihilation they want, merely you want to shorten the names to fit with the user interface blueprint of a new app.

Renaming files is an platonic task for a Lambda office considering it'southward a small isolated role, and information technology doesn't accept to be run immediately. Plus, it's stateless.

The starting signal for your Crimson Lambda function

The starting point for creating this function is a Lambda concept known as a handler. A handler is a method written in a Cherry-red file. In Ruby, file names are unremarkably all lower case and separated by an underscore. For this instance, we'll apply the default file proper name when creating a new Lambda,lambda_function.rb.

Thelambda_function.rb file contains a method, and its default name islambda_handler. This method contains the logic, and information technology volition be run on an upshot trigger.

The first named argument for thelambda_handler method is theconsequence. The JSON AWS effect converts into a Ruby-red object and is assigned to this argument.

The 2nd method argument is thecontext. Thecontext is assigned to a hash, which contains methods that provide information virtually the role, such as the name and any limits. A full list of all the datacontext holds can be constitute in the AWS docs.

This code shows mylambda_function.rb file and the method,lambda_handler.

#lambda_function.rb def lambda_handler(issue:, context:) end

This the base of operations of a Lambda function and the minimum required for the Lambda to execute.

Reading information from an consequence

The main method that handles the processing of our upshot, namedlambda_handler above, acceptsissueas an argument. This parameter is a Scarlet hash converted from a JSON string containing the origin of the event and whatever contextual information. For our case, this will contain the proper name of the bucket and cardinal of the file stored in S3.

The saucepan name and fundamental in S3 uniquely reference the epitome nosotros're storing. To retrieve these values from the event hash, update thelambda_handler method to reference thebucketname andkey values from the event. See the code below for how this looks in thelambda_handler function:

def lambda_handler(event:, context:)     first_record = event["Records"].offset     bucket_name = first_record["s3"]["bucket"]["name"]     file_name= first_record["s3"]["object"]["central"]  terminate

The above code shows retrieving the first record of theresult. At that place will just be i record in total when calculation a file to an S3 bucket. Thebucket_name andfile_name are and so extracted from the record.

Shorten the name of the file

To shorten the proper name of the file, nosotros'll use standard Cherry-red libraries to extract the first 21 characters of the original file name.

One of the keen aspects of using Carmine for writing Lambda functions is the simplicity of cord manipulation. The below code will assign the first 21 characters of the variablefile_name to the variableshort_name:

short_name = file_name[0..20]

This code uses the fact that a string tin exist accessed the same manner as arrays and selects the range from zero to 20.

Using the AWS SDK

Lambda functions are already configured to utilise the AWS SDK for Ruby, and so no gems need to be installed before nosotros can utilize the library. To reference the SDK, add together acrave statement to the top of yourlambda_function.rbfile. The below code shows therequire argument at the peak of thelambda_function.rb file:

require "aws-sdk-s3"

The code above loads the precious stone containing helper functions to communicate with the S3 service.

Moving your file from one bucket to another

To move your files from 1 saucepan to another, reference the original epitome and call themove_tofunction on that object. The lawmaking below shows how to use the built-in SDK functions to move the file:

# Go reference to the file in S3 client = Aws::S3::Client.new(region: 'eu-west-1') s3 = Aws::S3::Resources.new(client: customer) object = s3.bucket(bucket_name).object(file_name)  # Move the file to a new saucepan object.move_to(saucepan: 'upload-image-short-name', key: short_name)

This code will create a new client in theeuropean union-west-1 region, create a news3 resources, and utilise the resource to reference a specific object: in this case, a file. The adjacent action is to move the object to a new location. Themove_tomethod calls the S3 service to copy the original file to the new location. Then, it deletes the original.

Putting the lawmaking together

When put together, the above code volition achieve our goal of moving a file from one bucket to another, shortening the proper noun to 21 characters.

require "aws-sdk-s3"  def lambda_handler(outcome:, context:)     # Become the record     first_record = result["Records"].first     bucket_name = first_record["s3"]["bucket"]["name"]     file_name = first_record["s3"]["object"]["cardinal"]      # shorten the name     short_name = file_name[0..20]      # Get reference to the file in S3     customer = Aws::S3::Client.new(region: 'european union-west-1')     s3 = Aws::S3::Resource.new(client: client)     object = s3.bucket(bucket_name).object(file_name)      # Move the file to a new bucket     object.move_to(saucepan: 'upload-epitome-short-name', cardinal: short_name) end

This a working Lambda part containing the code from the previous sections of this post.

Now that we have our role, we can configure AWS to run it in a Lambda when nosotros upload a file. To attain this we demand

  • An AWS account.
  • Two buckets—one for the initial upload and the other for storing the files once the proper name is changed.
  • A Lambda role triggered by upload of a file.

Setting up an AWS account

If you don't already accept an AWS account, y'all'll need to prepare one up with Amazon. They take a generous free tier, which will cover creating this office and testing information technology. Amazon has a bang-up guide on how to create and activate an AWS account, and so follow this guide if you don't already have one.

Create two S3 buckets

Create two buckets: i that will allow initial upload of the files and another to store the files once they have been renamed. Amazon has a corking guide on how to create a bucket, if you wind upwardly needing aid.

Creating your Lambda office using the AWS console

Y'all can create a function by writing the code directly into the AWS console or by writing the code on your computer and so uploading to AWS. The rest of this post covers creating your office via the AWS console.

It's important to annotation that functions are located in a specific region. A region relates to a specific datacenter owned and ran past AWS. Services can't communicate between regions hands, so make sure the Lambda is in the same region as the S3 buckets.

If you're choosing a region for the beginning time, cull the region closest to you. Yous should besides consider that some regions don't have all the features that others do. Concurrency Labs has a swell blog mail called "Save yourself a lot of pain (and money) by choosing your AWS Region wisely," and it'll help you lot choose.

Create your function using the console

To create a Lambda function through the AWS console, navigate tohttps://console.aws.amazon.com/lambda in your browser. By default, this will open the console in your default region. My default region iseu-west-ii, located in the nearest location to me, London.

SelectCreate part to create a new Ruby function in theeu-west-two region.

AWS Lambda Ruby Function

The in a higher place prototype shows the values y'all should select to create a new Cerise function successfully. Once you have selected these values, clickCreate function. This will display the designer view shown below:

Designer View

This is a visual representation of your Lambda workflow. On the left is the trigger and on the right is the flow from the trigger to the output of the function.

Trigger your Lambda function from an result

To set up a trigger for your Lambda function, select S3 from the designer view. This will open up the configuration carte for the S3 trigger, equally shown below:

AWS S3 Trigger

The above trigger view shows the configuration options for S3. Select the bucket that contains the uploaded images in theSaucepandropdown and pressAdd together .Your Lambda function will now run whenever an prototype is uploaded to that bucket.

Configure your Lambda office

Select the Lambda function box from the designer view. This will open theFunction lawmaking view, shown below the designer, already populated with an example function. Copy the lawmaking we created earlier in this blog post into thelambda_handler method in thelambda_function.rb file:

Lambda Function Ruby

Give Lambda permissions to write to S3

To allow your function to copy the file from one saucepan to another the function requires some extra privileges. Select the Lambda office and scroll down to the configuration department shown beneath:

Lambda Execution Role

The higher up image shows the Lambda configuration department, focused onExecution role.Click the link namedView the RenameFile-role-<unique value> . The link will redirect you to the AWS IAM service. Click Attach policy.

AWS IAM Attach Policy

The above shows the attach policy view of the IAM service.

Now, search fors3. This will show all the policies relevant to S3. SelectAmazonS3FullAccess and press theAttach policy push. You should at present meet an actress box on the designer view, and Lambda volition now have admission to all buckets in S3.

AWS S3 Buckets

Examination your function

Upload a file with a name longer than 21 characters to your image bucket. The file will soon be deleted and transferred to the small names bucket and be 21 characters long. And there you become—examination passed!

Writing Lambda functions in Ruby

Crimson is a great language, and I'm really happy AWS is now supporting Crimson functions in Lambda. Once you're comfortable with the basics and desire more avant-garde functions, Serverless Framework is a great next step. Serverless Framework allows yous to package up gems to utilize in your role outside of those provided by Lambda. It also helps with versioning and organizing your lawmaking, along with storing the configuration for your functions.

Some other tool that could speed up your development is Ruby on Jets, an easy-to-use Rails-like framework to create API endpoints and recurring jobs. These tools provide an abstraction on top of Lambda, and the generated functions can always exist verified via the AWS console.

CloudWatch is the default monitoring tool created past AWS to continue rails of errors in your Lambda functions. Information technology's a adept tool, just it'south very labor intensive considering looking through the CloudWatch logs is a manual process. Using a tool like Retrace could help monitor how and when each of your functions are being executed and runway whatever errors together with context in a convenient dashboard.

Lambda is non the solution to every problem, and overuse might lead to unnecessary complexity. Take each use instance one at a time and decide if Lambda is the right solution for you.

  • About the Author
  • Latest Posts

everettcareter1994.blogspot.com

Source: https://stackify.com/aws-lambda-with-ruby-a-complete-getting-started-guide/

0 Response to "Upload and Download Files From Aws Ruby on Rails"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel