Skip to content

Commit 53f79df

Browse files
committed
Add Step-by-Step Instructions To The Readme
1 parent f3612ff commit 53f79df

File tree

1 file changed

+113
-4
lines changed

1 file changed

+113
-4
lines changed

elixir_runtime/README.md

+113-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,129 @@
1-
## AWS Lambda Elixir Runtime
1+
# AWS Lambda Elixir Runtime
22

33
Example implementation of a custom runtime for running Elixir on AWS Lambda.
44

55
## Installation
66

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`:
99

1010
```elixir
1111
def deps do
1212
[
13-
{:lambda, "~> 0.1.0"}
13+
{:aws_lambda_elixir_runtime, "~> 0.1.0"}
1414
]
1515
end
1616
```
1717

18+
## Documentation
19+
1820
Documentation can be generated with
1921
[ExDoc](https://github.com/elixir-lang/ex_doc).
2022

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

Comments
 (0)