|
1 | | -## My Project |
| 1 | +## AWS Lambda Ruby Runtime Interface Client |
2 | 2 |
|
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. |
4 | 7 |
|
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. |
6 | 10 |
|
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` |
9 | 140 |
|
10 | 141 | ## Security |
11 | 142 |
|
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. |
13 | 144 |
|
14 | 145 | ## License |
15 | 146 |
|
16 | | -This project is licensed under the Apache-2.0 License. |
17 | | - |
| 147 | +This project is licensed under the Apache-2.0 License. |
0 commit comments