├── .gitignore ├── .ruby-version ├── Makefile ├── README.md ├── hello_ruby ├── .bundle │ └── config ├── Gemfile ├── Gemfile.lock └── lib │ └── hello.rb └── resources ├── bundler-config ├── index.js ├── mysql2-0.3.18-linux.tar.gz ├── schema.sql ├── seed.sql ├── traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz ├── traveling-ruby-20150715-2.2.2-osx.tar.gz └── wrapper.sh /.gitignore: -------------------------------------------------------------------------------- 1 | hello-1.0.0-osx/ 2 | hello-1.0.0-linux-x86_64/ 3 | deploy/ 4 | tmp/ 5 | hello_ruby/vendor/ 6 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.2.2 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OSXDIR=hello-1.0.0-osx 2 | LAMBDADIR=hello-1.0.0-linux-x86_64 3 | DBPASSWD=Kew2401Sd 4 | DBNAME=awslambdaruby 5 | 6 | THIS_FILE := $(lastword $(MAKEFILE_LIST)) 7 | 8 | .DEFAULT_GOAL := help 9 | 10 | run: ## Runs the code locally 11 | @echo 'Run the app locally' 12 | @echo '-------------------' 13 | @rm -fr $(OSXDIR) 14 | @mkdir -p $(OSXDIR)/lib/ruby 15 | @tar -xzf resources/traveling-ruby-20150715-2.2.2-osx.tar.gz -C $(OSXDIR)/lib/ruby 16 | @mkdir $(OSXDIR)/lib/app 17 | @cp hello_ruby/lib/hello.rb $(OSXDIR)/lib/app/hello.rb 18 | @cp -pR hello_ruby/vendor $(OSXDIR)/lib/ 19 | @rm -f $(OSXDIR)/lib/vendor/*/*/cache/* 20 | @mkdir -p $(OSXDIR)/lib/vendor/.bundle 21 | @cp resources/bundler-config $(OSXDIR)/lib/vendor/.bundle/config 22 | @cp hello_ruby/Gemfile $(OSXDIR)/lib/vendor/ 23 | @cp hello_ruby/Gemfile.lock $(OSXDIR)/lib/vendor/ 24 | @cp resources/wrapper.sh $(OSXDIR)/hello 25 | @chmod +x $(OSXDIR)/hello 26 | @cd $(OSXDIR) && ./hello 27 | 28 | package: ## Packages the code for AWS Lambda 29 | @echo 'Package the app for deploy' 30 | @echo '--------------------------' 31 | @rm -fr $(LAMBDADIR) 32 | @rm -fr deploy 33 | @mkdir -p $(LAMBDADIR)/lib/ruby 34 | @tar -xzf resources/traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz -C $(LAMBDADIR)/lib/ruby 35 | @mkdir $(LAMBDADIR)/lib/app 36 | @cp hello_ruby/lib/hello.rb $(LAMBDADIR)/lib/app/hello.rb 37 | @cp -pR hello_ruby/vendor $(LAMBDADIR)/lib/ 38 | @rm -fr $(LAMBDADIR)/lib/vendor/ruby/2.2.0/extensions 39 | @tar -xzf resources/mysql2-0.3.18-linux.tar.gz -C $(LAMBDADIR)/lib/vendor/ruby/ 40 | @rm -f $(LAMBDADIR)/lib/vendor/*/*/cache/* 41 | @mkdir -p $(LAMBDADIR)/lib/vendor/.bundle 42 | @cp resources/bundler-config $(LAMBDADIR)/lib/vendor/.bundle/config 43 | @cp hello_ruby/Gemfile $(LAMBDADIR)/lib/vendor/ 44 | @cp hello_ruby/Gemfile.lock $(LAMBDADIR)/lib/vendor/ 45 | @cp resources/wrapper.sh $(LAMBDADIR)/hello 46 | @chmod +x $(LAMBDADIR)/hello 47 | @cp resources/index.js $(LAMBDADIR)/ 48 | @cd $(LAMBDADIR) && zip -r hello_ruby.zip hello index.js lib/ > /dev/null 49 | @mkdir deploy 50 | @cd $(LAMBDADIR) && mv hello_ruby.zip ../deploy/ 51 | @echo '... Done.' 52 | 53 | create: ## Creates an AWS lambda function 54 | aws lambda create-function \ 55 | --function-name HelloFromRuby \ 56 | --handler index.handler \ 57 | --runtime nodejs4.3 \ 58 | --memory 512 \ 59 | --timeout 10 \ 60 | --description "Saying hello from MRI Ruby" \ 61 | --role arn:aws:iam::___xyz___:role/lambda_basic_execution \ 62 | --zip-file fileb://./deploy/hello_ruby.zip 63 | 64 | publish: package ## Deploys the latest version to AWS 65 | aws lambda update-function-code \ 66 | --function-name HelloFromRuby \ 67 | --zip-file fileb://./deploy/hello_ruby.zip 68 | 69 | delete: ## Removes the Lambda 70 | aws lambda delete-function --function-name HelloFromRuby 71 | 72 | invoke: ## Invokes the AWS Lambda in the command line 73 | rm -fr tmp && mkdir tmp 74 | aws lambda invoke \ 75 | --invocation-type RequestResponse \ 76 | --function-name HelloFromRuby \ 77 | --log-type Tail \ 78 | --region us-east-1 \ 79 | --payload '{"name":"John Adam Smith"}' \ 80 | tmp/outfile.txt \ 81 | | jq -r '.LogResult' | base64 -D 82 | 83 | create-rds-instance: ## Creates an RDS MySQL DB instance 84 | aws rds create-db-instance \ 85 | --db-instance-identifier MyInstance01 \ 86 | --db-instance-class db.t1.micro \ 87 | --engine mysql \ 88 | --allocated-storage 10 \ 89 | --master-username master \ 90 | --master-user-password $(DBPASSWD) 91 | 92 | delete-rds-instance: ## Deletes an RDS MySQL DB instance 93 | aws rds delete-db-instance \ 94 | --db-instance-identifier MyInstance01 \ 95 | --skip-final-snapshot 96 | 97 | db-connect: ## Connects to the RDS instance 98 | mysql --user=master --password=$(DBPASSWD) --host myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com 99 | 100 | create-db: ## Creates a DB with a table and records 101 | @echo "Dropping and creating database" 102 | @echo "-------------------------------" 103 | @mysql -u master --password='$(DBPASSWD)' --host myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com -e "DROP DATABASE IF EXISTS $(DBNAME)" > /dev/null 2>&1 104 | @mysql -u master --password='$(DBPASSWD)' --host myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com -e "CREATE DATABASE $(DBNAME)" > /dev/null 2>&1 105 | @mysql -u master --password='$(DBPASSWD)' --host myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com $(DBNAME) < resources/schema.sql > /dev/null 2>&1 106 | @mysql -u master --password='$(DBPASSWD)' --host myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com $(DBNAME) < resources/seed.sql > /dev/null 2>&1 107 | @echo "... Done" 108 | 109 | .PHONY: help 110 | 111 | help: 112 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' 113 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## (MRI) Ruby on AWS Lambda with ActiveRecord 2 | 3 | A Ruby project to demonstrate how you can run MRI Ruby on AWS Lambda with [Traveling Ruby](https://github.com/phusion/traveling-ruby). 4 | 5 | Read more about it [here](http://www.adomokos.com/2016/06/using-ruby-in-aws-lambda.html) and [here](http://www.adomokos.com/2016/06/using-ruby-with-activerecord-in-aws.html). 6 | -------------------------------------------------------------------------------- /hello_ruby/.bundle/config: -------------------------------------------------------------------------------- 1 | --- 2 | BUNDLE_PATH: vendor 3 | BUNDLE_WITHOUT: development 4 | BUNDLE_DISABLE_SHARED_GEMS: true 5 | -------------------------------------------------------------------------------- /hello_ruby/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'faker' 4 | gem 'activerecord' 5 | gem 'mysql2', '0.3.18' 6 | -------------------------------------------------------------------------------- /hello_ruby/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | activemodel (4.2.6) 5 | activesupport (= 4.2.6) 6 | builder (~> 3.1) 7 | activerecord (4.2.6) 8 | activemodel (= 4.2.6) 9 | activesupport (= 4.2.6) 10 | arel (~> 6.0) 11 | activesupport (4.2.6) 12 | i18n (~> 0.7) 13 | json (~> 1.7, >= 1.7.7) 14 | minitest (~> 5.1) 15 | thread_safe (~> 0.3, >= 0.3.4) 16 | tzinfo (~> 1.1) 17 | arel (6.0.3) 18 | builder (3.2.2) 19 | faker (1.6.3) 20 | i18n (~> 0.5) 21 | i18n (0.7.0) 22 | json (1.8.3) 23 | minitest (5.9.0) 24 | mysql2 (0.3.18) 25 | thread_safe (0.3.5) 26 | tzinfo (1.2.2) 27 | thread_safe (~> 0.1) 28 | 29 | PLATFORMS 30 | ruby 31 | 32 | DEPENDENCIES 33 | activerecord 34 | faker 35 | mysql2 (= 0.3.18) 36 | -------------------------------------------------------------------------------- /hello_ruby/lib/hello.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'faker' 4 | require 'active_record' 5 | 6 | ActiveRecord::Base.establish_connection( 7 | :adapter => "mysql2", 8 | :host => "myinstance01.cgic5q3lz0bb.us-east-1.rds.amazonaws.com", 9 | :username => "master", 10 | :password => "Kew2401Sd", 11 | :database => "awslambdaruby" 12 | ) 13 | 14 | class User < ActiveRecord::Base 15 | end 16 | 17 | puts "Number of users: #{User.count}" 18 | puts "First user: #{User.first.firstname} #{User.first.lastname}" 19 | puts "Hello - '#{Faker::Name.name}' from Ruby!" 20 | -------------------------------------------------------------------------------- /resources/bundler-config: -------------------------------------------------------------------------------- 1 | BUNDLE_PATH: . 2 | BUNDLE_WITHOUT: development:test 3 | BUNDLE_DISABLE_SHARED_GEMS: '1' 4 | -------------------------------------------------------------------------------- /resources/index.js: -------------------------------------------------------------------------------- 1 | process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] 2 | 3 | var exec = require('child_process').exec; 4 | exports.handler = function(event, context) { 5 | var command = `./hello`; 6 | child = exec(command, {env: {'LD_LIBRARY_PATH': __dirname + '/lib'}}, function(error) { 7 | // Resolve with result of process 8 | context.done(error, 'Process complete!'); 9 | }); 10 | // Log process stdout and stderr 11 | child.stdout.on('data', console.log); 12 | child.stderr.on('data', console.error); 13 | }; 14 | -------------------------------------------------------------------------------- /resources/mysql2-0.3.18-linux.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adomokos/aws-lambda-ruby/f2b83da0f1ac1dbadfa6c5fac7a8be1341315e4b/resources/mysql2-0.3.18-linux.tar.gz -------------------------------------------------------------------------------- /resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `users`; 2 | 3 | CREATE TABLE `users` ( 4 | `id` int(11) NOT NULL AUTO_INCREMENT, 5 | `login` varchar(255) DEFAULT NULL, 6 | `email` varchar(255) NOT NULL DEFAULT '', 7 | `firstname` varchar(255) DEFAULT NULL, 8 | `lastname` varchar(255) DEFAULT NULL, 9 | PRIMARY KEY (`id`) 10 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 11 | -------------------------------------------------------------------------------- /resources/seed.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO `users` VALUES (1, 'jsmith', 'jsmith@gmail.com', 'John', 'Smith'); 2 | INSERT INTO `users` VALUES (2, 'bjones', 'bjones@gmail.com', 'Bob', 'Jones'); 3 | 4 | -------------------------------------------------------------------------------- /resources/traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adomokos/aws-lambda-ruby/f2b83da0f1ac1dbadfa6c5fac7a8be1341315e4b/resources/traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz -------------------------------------------------------------------------------- /resources/traveling-ruby-20150715-2.2.2-osx.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adomokos/aws-lambda-ruby/f2b83da0f1ac1dbadfa6c5fac7a8be1341315e4b/resources/traveling-ruby-20150715-2.2.2-osx.tar.gz -------------------------------------------------------------------------------- /resources/wrapper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | # Figure out where this script is located. 5 | SELFDIR="`dirname \"$0\"`" 6 | SELFDIR="`cd \"$SELFDIR\" && pwd`" 7 | 8 | # Tell Bundler where the Gemfile and gems are. 9 | export BUNDLE_GEMFILE="$SELFDIR/lib/vendor/Gemfile" 10 | unset BUNDLE_IGNORE_CONFIG 11 | 12 | # Run the actual app using the bundled Ruby interpreter, with Bundler activated. 13 | exec "$SELFDIR/lib/ruby/bin/ruby" -rbundler/setup "$SELFDIR/lib/app/hello.rb" 14 | --------------------------------------------------------------------------------