|
1 |
| -## AWS Lambda Elixir Runtime |
| 1 | +# AWS Lambda Elixir Runtime |
2 | 2 |
|
3 | 3 | Example implementation of a custom runtime for running Elixir on AWS Lambda.
|
4 | 4 |
|
5 | 5 | ## Installation
|
6 | 6 |
|
7 |
| -If [available in Hex](https://hex.pm/docs/publish), the package can be installed |
8 |
| -by adding `lambda` to your list of dependencies in `mix.exs`: |
| 7 | +The package can be installed by adding `aws_lambda_elixir_runtime` to your list |
| 8 | +of dependencies in `mix.exs`: |
9 | 9 |
|
10 | 10 | ```elixir
|
11 | 11 | def deps do
|
12 | 12 | [
|
13 |
| - {:lambda, "~> 0.1.0"} |
| 13 | + {:aws_lambda_elixir_runtime, "~> 0.1.0"} |
14 | 14 | ]
|
15 | 15 | end
|
16 | 16 | ```
|
17 | 17 |
|
| 18 | +## Documentation |
| 19 | + |
18 | 20 | Documentation can be generated with
|
19 | 21 | [ExDoc](https://github.com/elixir-lang/ex_doc).
|
20 | 22 |
|
| 23 | +## Step By Step Usage |
| 24 | + |
| 25 | +This section is a step by step for creating the hello world example. |
| 26 | + |
| 27 | +First, create a new mix project in a fresh directory: |
| 28 | + |
| 29 | +```sh |
| 30 | +> mix new --app hello_world ./hello_world |
| 31 | +``` |
| 32 | + |
| 33 | +Now declare a dependency on `:aws_lambda_elixir_runtime` and |
| 34 | +`:distillery`, which is used to package the OTP release. |
| 35 | + |
| 36 | +Edit `mix.exs`: |
| 37 | + |
| 38 | +```elixir |
| 39 | +def deps do |
| 40 | + [ |
| 41 | + {:aws_lambda_elixir_runtime, "~> 0.1.0"}, |
| 42 | + {:distillery, "~> 2.0"} |
| 43 | + ] |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +Now get the dependencies: |
| 48 | + |
| 49 | +```sh |
| 50 | +> mix deps.get |
| 51 | +``` |
| 52 | + |
| 53 | +The `:aws_lambda_elixir_runtime` has a mix task which will generate a correct |
| 54 | +Distillery release file for deploying to Lambda. This is a one-time setup |
| 55 | +for the project because once generated the file can be versioned and customized |
| 56 | +like any other release. Generate the file like so: |
| 57 | + |
| 58 | +```sh |
| 59 | +> mix gen_lambda_release |
| 60 | +``` |
| 61 | + |
| 62 | +Now the project is ready to be built and deployed -- all that remains is to |
| 63 | +actually write a handler function. Open the `lib/hello_world.ex` and edit it |
| 64 | +to read: |
| 65 | + |
| 66 | +```elixir |
| 67 | +defmodule HelloWorld do |
| 68 | + |
| 69 | + def my_hello_world_handler(request, context) |
| 70 | + when is_map(request) and is_map(context) do |
| 71 | + """ |
| 72 | + Hello World! |
| 73 | + Request: #{Kernel.inspect(request)} |
| 74 | + Context: #{Kernel.inspect(context)} |
| 75 | + """ |
| 76 | + |> IO.puts() |
| 77 | + |
| 78 | + :ok |
| 79 | + end |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | +This just defines a single public function in the HelloWorld module. Any |
| 84 | +public function can be used to handle Lambda invocations, it just needs to |
| 85 | +accept two maps. |
| 86 | + |
| 87 | +Now, the project can be built and zipped: |
| 88 | + |
| 89 | +```sh |
| 90 | +> mix do release, bootstrap, zip |
| 91 | +``` |
| 92 | + |
| 93 | +The `release` task is the standard Distillery release operation. The |
| 94 | +`bootstrap` task generates an executable shell script which is called by the |
| 95 | +AWS Lambda service to start the Elixir OTP application. And the `zip` task just |
| 96 | +bundles the contents of the Distillery release into a single zip file. |
| 97 | + |
| 98 | +When this finishes, there should be a `lambda.zip` file in the current |
| 99 | +directory. This file can be uploaded to AWS lambda using the AWS console or the |
| 100 | +cli. Using the CLI would look like the following: |
| 101 | + |
| 102 | +```sh |
| 103 | +> aws lambda create-function \ |
| 104 | + --region $AWS_REGION \ |
| 105 | + --function-name HelloWorld \ |
| 106 | + --handler Elixir.HelloWorld:my_hello_world_handler \ |
| 107 | + --role $ROLE_ARN \ |
| 108 | + --runtime provided \ |
| 109 | + --zip-file fileb://./lambda.zip |
| 110 | +``` |
| 111 | + |
| 112 | +Once created the function can be invoked from the console, the SDK, or the CLI. |
| 113 | +Invoking from the CLI would look like this: |
| 114 | + |
| 115 | +```sh |
| 116 | +> aws lambda invoke \ |
| 117 | + --function-name HelloWorld \ |
| 118 | + --region $AWS_REGION \ |
| 119 | + --lag-type TAIL \ |
| 120 | + --payload '{"msg": "a fake request"}' \ |
| 121 | + outputfile.txt |
| 122 | +... |
| 123 | + |
| 124 | +> cat outputfile.txt |
| 125 | +Hello World! |
| 126 | +Request: %{ "msg" => "a fake request" } |
| 127 | +Context: %{ ... } |
| 128 | +``` |
| 129 | + |
0 commit comments