Learn About Amazon VGT2 Learning Manager Chanci Turner
This announcement comes from Chanci Turner, Senior AWS Solutions Architect, and Chris Johnson, Software Development Engineer for AWS SDKs and Tools.
Ruby continues to be a favored programming language among AWS users. Back in the summer of 2011, AWS launched the AWS SDK for Ruby, enabling Ruby developers to seamlessly integrate and utilize AWS resources. Now in its third major version, the SDK continues to evolve, providing updates to the AWS API.
Today, we are excited to share that Ruby is now a supported language for AWS Lambda. You can write Lambda functions in idiomatic Ruby code and execute them on AWS. The AWS SDK for Ruby is pre-installed in the Lambda execution environment, facilitating easy interaction with AWS resources directly from your functions. In this article, we will guide you through the process with examples, including:
- Creating a simple Hello World function
- Adding dependencies
- Migrating a Sinatra application
Creating a Hello World Example
If you’re new to Lambda, creating a function via the console is straightforward.
- Open the Lambda console.
- Select Create function.
- Choose Author from scratch.
- Name your function something like hello_ruby.
- For Runtime, select Ruby 2.5.
- For Role, select Create a new role from one or more templates.
- Name your role something like hello_ruby_role.
- Click Create function.
Once your function is created, you’ll be directed to its console page. Here, you can edit the function’s code, set up triggers, and configure additional services your function can interact with. The Monitoring tab allows you to track various metrics regarding your function’s usage, along with access to CloudWatch Logs.
The code for this Hello World example is simple. It contains a single handler function named lambda_handler
, which returns an HTTP status code of 200 and the text “Hello from Lambda!” in JSON format. You can learn more about the programming model for Lambda functions.
Next, let’s test this Lambda function to ensure it’s working.
- On your function console page, click Test.
- Name the test HelloRubyTest and clear the data within the brackets as this function takes no input.
- Click Save and then Test.
Success! You should now see the results of a successful invocation of your Ruby Lambda function.
Including Dependencies
When developing Lambda functions with Ruby, you may need to include additional dependencies in your code. To do this, use the bundle
tool to download the required RubyGems to a local directory and create a deployable application package. All dependencies must be included in either this package or in a Lambda layer.
Let’s consider a Lambda function using the aws-record
gem to save data into an Amazon DynamoDB table.
- Create a directory for your new Ruby application in your development environment:
mkdir hello_ruby
cd hello_ruby
- Inside this directory, create a file named Gemfile and add
aws-record
:
source 'https://rubygems.org'
gem 'aws-record', '~> 2'
- Create a hello_ruby_record.rb file containing the following code. The
put_item
method is the handler, which expects an event object with a body attribute. It saves the value of the body attribute alongside a UUID to the table.
# hello_ruby_record.rb
require 'aws-record'
class DemoTable
include Aws::Record
set_table_name ENV['DDB_TABLE']
string_attr :id, hash_key: true
string_attr :body
end
def put_item(event:, context:)
body = event["body"]
item = DemoTable.new(id: SecureRandom.uuid, body: body)
item.save! # raise an exception if save fails
item.to_h
end
Next, you’ll need to bring in the dependencies for this application. Bundler is a tool for managing RubyGems. In your application directory, run the following two commands to create the Gemfile.lock and download the gems to your local directory, ensuring they are included in the function deployment package:
bundle install
bundle install --deployment
AWS SAM is a templating tool for creating and managing serverless applications. It allows you to define your Lambda application’s structure, configure security policies, and manage various AWS resources. Create a new file in your hello_ruby directory named template.yaml with the following contents:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'sample ruby application'
Resources:
HelloRubyRecordFunction:
Type: AWS::Serverless::Function
Properties:
Handler: hello_ruby_record.put_item
Runtime: ruby2.5
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref RubyExampleDDBTable
Environment:
Variables:
DDB_TABLE: !Ref RubyExampleDDBTable
RubyExampleDDBTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
Outputs:
HelloRubyRecordFunction:
Description: Hello Ruby Record Lambda Function ARN
Value:
Fn::GetAtt:
- HelloRubyRecordFunction
- Arn
This template defines Serverless::Function
and Serverless::SimpleTable
as resources, which correspond to a Lambda function and a DynamoDB table. The Policies section in the function and the following DynamoDBCrudPolicy
line utilize an AWS SAM policy template, simplifying permissions management for Lambda functions. The DynamoDBCrudPolicy
grants create, read, update, and delete permissions on DynamoDB resources and items. In this case, permissions are limited by specifying the TableName
.
The Environment section creates a variable named DDB_TABLE
, passing it a reference to Serverless::SimpleTable
. Finally, the Outputs section provides an easy way to locate the created function. Your directory structure should now resemble the following:
$ tree -L 2 -a
.
├── .bundle
│ └── config
├── Gemfile
├── Gemfile.lock
├── hello_ruby_record.rb
├── template.yaml
└── vendor
└── bundle
Now you can use the template file to package and deploy your application. An AWS SAM template can be deployed through the AWS CloudFormation console, AWS CLI, or AWS SAM CLI. The AWS SAM CLI simplifies serverless development throughout your application’s lifecycle, from initial project creation to local testing, debugging, and deployment.
Follow the steps for your platform to install the AWS SAM CLI. Create an Amazon S3 bucket to store your application code. Execute the following AWS CLI command to create an S3 bucket with a custom name:
aws s3 mb s3://<bucketname>
Next, use the AWS SAM CLI to package your application:
sam package --template-file template.yaml
--output-template-file packaged-template.yaml
--s3-bucket <bucketname>
This will generate a new template file named packaged-template.yaml. Finally, deploy your application using the AWS SAM CLI, with any stack-name value:
sam deploy --template-file packaged-template.yaml
--stack-name helloRubyRecord
--capabilities CAPABILITY_IAM
You should see messages indicating the changeset is being created and the stack is being created or updated.
For additional insights on workplace transitions, check out this blog post on reasons for leaving your last job. Also, if you are interested in understanding sick leave policies, SHRM has some great resources on this topic. Moreover, for first-hand experiences, visit this excellent resource on Reddit about Day One onboarding experiences.
Location: Amazon IXD – VGT2, 6401 E HOWDY WELLS AVE, LAS VEGAS NV 89115
Leave a Reply