Code Monkey home page Code Monkey logo

ruby-lambda-example's Introduction

Ruby on Lambda

This repo shows you how to deploy a Ruby Lambda function using Terraform.

There are three different examples in this repository. All examples are deployed together.

  • Hello World is just a very simple and basic Ruby function.
  • Countries scrapes a list of country names from a website and stores them in a DynamoDB Table.
  • Sinatra shows how to deploy a little Sinatra app as a serverless function.
  • on-rails even deploys a full Ruby on Rails application.

Setup

You need to have Terraform and the AWS CLI installed. Also, for building the gems, you need to have Docker installed.

Then you obviously also need an AWS account. You can create one for free. And the following example can also be run within the free tier. (Unless you run it a million times, but that's up to you.)

Setup your AWS credentials as explained in the AWS CLI documentation. Then initialize Terraform:

cd infra
terraform init

How To

After setting everything up, you should be able to just run the deploy script:

bin/deploy

The script will copy the src directory into the build directory, run bundle install, package your stuff into a ZIP file, and uploads it as a lambda function.

If everything worked out, the following command should be successful:

aws lambda invoke --function-name hello-world response.json

Explanations

The most difficult thing about running Ruby apps as Lambda functions is the building of the gems layer.

The easiest thing would be to just install gems into vendor/bundle as usual, zip the hole app directory, and upload it as a function. That's easy and it usually works. But the package can become quickly larger then the maximum package size of 50 MB. So, we do need to package the gems as a separate Lambda layer.

The GEM_PATH in a Lambda environment looks like this:

GEM_PATH=/var/task/vendor/bundle/ruby/3.2.0:/opt/ruby/gems/3.2.0:/var/runtime:/var/runtime/ruby/3.2.0
        # ^ local bundle directory          ^ where layers are mounted

Because Lambda is attaching layers to /opt, we have to make sure, our gems are all in /opt/ruby/gems/3.2.0. And because Bundler applies a different directory layout when installing gems, we need to find the correct copy command to move those gems into the right place, put them correctly into a zip file, and then upload it as a Lambda layer.

ruby-lambda-example's People

Contributors

neckhair avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.