Skip to content

Commit 5e669fe

Browse files
committed
Initial Ruby RIC implementation
1 parent c2609de commit 5e669fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3224
-167
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
vendor
10+
test/examples/hello-world-docker/pkg
11+
*.iml
12+
.DS_Store

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
# frozen_string_literal: true
4+
5+
source 'https://rubygems.org'
6+
7+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
8+
9+
# Specify your gem's dependencies in aws_lambda_ric.gemspec
10+
gemspec

Gemfile.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PATH
2+
remote: .
3+
specs:
4+
aws_lambda_ric (1.0.0)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
minitest (5.14.2)
10+
rake (10.5.0)
11+
12+
PLATFORMS
13+
ruby
14+
15+
DEPENDENCIES
16+
aws_lambda_ric!
17+
bundler (>= 1.17)
18+
minitest (~> 5.0)
19+
rake (~> 10.0)
20+
21+
BUNDLED WITH
22+
2.2.0.rc.2

LICENSE

Lines changed: 213 additions & 159 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.PHONY: target
2+
target:
3+
$(info ${HELP_MESSAGE})
4+
@exit 0
5+
6+
.PHONY: init
7+
init:
8+
bundle install
9+
10+
.PHONY: setup-codebuild-agent
11+
setup-codebuild-agent:
12+
docker build -t codebuild-agent - < test/integration/codebuild-local/Dockerfile.agent
13+
14+
.PHONY: test-smoke
15+
test-smoke: setup-codebuild-agent
16+
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_one.sh test/integration/codebuild/buildspec.os.alpine.yml alpine 3.12 2.7
17+
18+
.PHONY: test-unit
19+
test-unit:
20+
ruby test/run_tests.rb unit
21+
22+
.PHONY: test-integ
23+
test-integ: setup-codebuild-agent
24+
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_all.sh test/integration/codebuild
25+
26+
.PHONY: build
27+
build:
28+
rake build
29+
30+
define HELP_MESSAGE
31+
32+
Usage: $ make [TARGETS]
33+
34+
TARGETS
35+
36+
build Builds the package.
37+
clean Cleans the working directory by removing built artifacts.
38+
init Initialize and install the dependencies and dev-dependencies for this project.
39+
test-integ Run Integration tests.
40+
test-unit Run Unit Tests.
41+
test-smoke Run Sanity/Smoke tests.
42+
43+
endef

README.md

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,147 @@
1-
## My Project
1+
## AWS Lambda Ruby Runtime Interface Client
22

3-
TODO: Fill this README out!
3+
We have open-sourced a set of software packages, Runtime Interface Clients (RIC), that implement the Lambda
4+
[Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html), allowing you to seamlessly extend your preferred
5+
base images to be Lambda compatible.
6+
The Lambda Runtime Interface Client is a lightweight interface that allows your runtime to receive requests from and send requests to the Lambda service.
47

5-
Be sure to:
8+
The Lambda Ruby Runtime Interface Client is vended through [rubygems](https://rubygems.org/gems/aws_lambda_ric).
9+
You can include this package in your preferred base image to make that base image Lambda compatible.
610

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
11+
## Requirements
12+
The Ruby Runtime Interface Client package currently supports Ruby versions:
13+
- 2.5.x up to and including 2.7.x
14+
15+
## Usage
16+
17+
### Creating a Docker Image for Lambda with the Runtime Interface Client
18+
First step is to choose the base image to be used. The supported Linux OS distributions are:
19+
20+
- Amazon Linux 2
21+
- Alpine
22+
- CentOS
23+
- Debian
24+
- Ubuntu
25+
26+
In order to install the Runtime Interface Client, either add this line to your application's Gemfile:
27+
28+
```ruby
29+
gem 'aws_lambda_ric'
30+
```
31+
32+
And then execute:
33+
34+
$ bundle
35+
36+
Or install it manually as:
37+
38+
$ gem install aws_lambda_ric
39+
40+
41+
Next step would be to copy your Lambda function code into the image's working directory.
42+
```dockerfile
43+
# Copy function code
44+
RUN mkdir -p ${FUNCTION_DIR}
45+
COPY app.rb ${FUNCTION_DIR}
46+
47+
WORKDIR ${FUNCTION_DIR}
48+
```
49+
50+
The next step would be to set the `ENTRYPOINT` property of the Docker image to invoke the Runtime Interface Client and then set the `CMD` argument to specify the desired handler.
51+
52+
Example Dockerfile:
53+
```dockerfile
54+
FROM amazonlinux:latest
55+
56+
# Define custom function directory
57+
ARG FUNCTION_DIR="/function"
58+
59+
# Install ruby
60+
RUN amazon-linux-extras install -y ruby2.6
61+
62+
# Install the Runtime Interface Client
63+
RUN gem install aws_lambda_ric
64+
65+
# Copy function code
66+
RUN mkdir -p ${FUNCTION_DIR}
67+
COPY app.rb ${FUNCTION_DIR}
68+
69+
WORKDIR ${FUNCTION_DIR}
70+
71+
ENTRYPOINT ["aws_lambda_ric"]
72+
CMD ["app.App::Handler.process"]
73+
```
74+
75+
Example Ruby handler `app.rb`:
76+
```ruby
77+
module App
78+
class Handler
79+
def self.process(event:, context:)
80+
"Hello World!"
81+
end
82+
end
83+
end
84+
```
85+
86+
### Local Testing
87+
88+
To make it easy to locally test Lambda functions packaged as container images we open-sourced a lightweight web-server, Lambda Runtime Interface Emulator (RIE), which allows your function packaged as a container image to accept HTTP requests. You can install the [AWS Lambda Runtime Interface Emulator](https://github.com/aws/aws-lambda-runtime-interface-emulator) on your local machine to test your function. Then when you run the image function, you set the entrypoint to be the emulator.
89+
90+
*To install the emulator and test your Lambda function*
91+
92+
1) From your project directory, run the following command to download the RIE from GitHub and install it on your local machine.
93+
94+
```shell script
95+
mkdir -p ~/.aws-lambda-rie && \
96+
curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \
97+
chmod +x ~/.aws-lambda-rie/aws-lambda-rie
98+
```
99+
2) Run your Lambda image function using the docker run command.
100+
101+
```shell script
102+
docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
103+
--entrypoint /aws-lambda/aws-lambda-rie \
104+
myfunction:latest \
105+
aws_lambda_ric app.App::Handler.process
106+
```
107+
108+
This runs the image as a container and starts up an endpoint locally at `http://localhost:9000/2015-03-31/functions/function/invocations`.
109+
110+
3) Post an event to the following endpoint using a curl command:
111+
112+
```shell script
113+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'.
114+
```
115+
116+
This command invokes the function running in the container image and returns a response.
117+
118+
*Alternately, you can also include RIE as a part of your base image. See the AWS documentation on how to [Build RIE into your base image](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html#images-test-alternative).*
119+
120+
## Development
121+
122+
### Building the package
123+
Clone this repository and run:
124+
125+
```shell script
126+
make init
127+
make build
128+
```
129+
130+
### Running tests
131+
132+
Make sure the project is built:
133+
```shell script
134+
make init build
135+
```
136+
Then,
137+
* to run unit tests: `make test`
138+
* to run integration tests: `make test-integ`
139+
* to run smoke tests: `make test-smoke`
9140

10141
## Security
11142

12-
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
143+
If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.
13144

14145
## License
15146

16-
This project is licensed under the Apache-2.0 License.
17-
147+
This project is licensed under the Apache-2.0 License.

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
4+
require 'rake/testtask'
5+
6+
Rake::TestTask.new(:test) do |t|
7+
t.libs << 'test'
8+
t.libs << 'lib'
9+
t.test_files = FileList['test/**/*_test.rb']
10+
end
11+
12+
task default: :test

aws_lambda_ric.gemspec

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
# frozen_string_literal: true
4+
5+
lib = File.expand_path('lib', __dir__)
6+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7+
require './lib/aws_lambda_ric/version'
8+
9+
Gem::Specification.new do |spec|
10+
spec.name = 'aws_lambda_ric'
11+
spec.version = AwsLambdaRuntimeInterfaceClient::VERSION
12+
spec.authors = ['AWS Lambda']
13+
14+
spec.summary = 'AWS Lambda Runtime Interface Client for Ruby'
15+
spec.description = 'The AWS Lambda Ruby Runtime Interface Client implements the Lambda programming model for Ruby.'
16+
spec.homepage = 'https://github.com/aws/aws-lambda-ruby-runtime-interface-client'
17+
18+
# Specify which files should be added to the gem when it is released.
19+
spec.files = %w[
20+
LICENSE
21+
README.md
22+
Gemfile
23+
NOTICE
24+
Gemfile.lock
25+
aws_lambda_ric.gemspec
26+
bin/aws_lambda_ric
27+
] + Dir['lib/**/*']
28+
29+
spec.bindir = 'bin'
30+
# all application-style files are expected to be found in bindir
31+
spec.executables = 'aws_lambda_ric'
32+
spec.require_paths = ['lib']
33+
34+
spec.add_development_dependency 'bundler', '>= 2.0'
35+
spec.add_development_dependency 'minitest', '~> 5.0'
36+
spec.add_development_dependency 'rake', '~> 10.0'
37+
end

bin/aws_lambda_ric

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#! /usr/bin/env ruby
2+
3+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
5+
# frozen_string_literal: true
6+
7+
require 'bundler/setup'
8+
require_relative '../lib/aws_lambda_ric/bootstrap'
9+
10+
Bootstrap.start

0 commit comments

Comments
 (0)