You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/01-getting-started.md
+10-26Lines changed: 10 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,25 +11,25 @@ If not, you can do so by issuing the following command on your terminal:
11
11
curl -sSL https://get.haskellstack.org/ | sh
12
12
```
13
13
14
-
Haskell compiles to **native code**, which is super efficient. But it has one main drawback: linking changes from machine to machine.
14
+
Haskell compiles to **native code**, which is super efficient. But it has one main drawback: linking changes from machine to machine. It's very hard to make sure that the executable you build will work when deployed to AWS Lambda.
15
15
16
-
To make sure that our projects always work and are reproducible, we use the Stack [feature](https://docs.haskellstack.org/en/stable/docker_integration/)
17
-
for [Docker](https://www.docker.com/) support to build our projects. Be sure to install Docker before getting started with the runtime 😄
16
+
To make sure our projects work consistently, we use AWS Lambda's [docker image](https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/) feature. Be sure to install Docker before getting started with the runtime 😄
18
17
19
18
## Using the template
20
19
21
20
If you are testing the package, or you are starting a new project, we have provided a Stack template that will scaffold the project for you.
22
21
To use it, enter the following command:
23
22
24
23
```bash
25
-
stack new my-haskell-lambda https://github.com/theam/aws-lambda-haskell-runtime/raw/master/stack-template.hsfiles --resolver=lts-15.16 --omit-packages
24
+
stack new my-haskell-lambda https://github.com/theam/aws-lambda-haskell-runtime/raw/master/stack-template.hsfiles
26
25
```
27
26
28
27
This will create a `my-haskell-lambda` directory with the following structure:
29
28
30
29
```text
31
30
.
32
31
├── LICENSE
32
+
├── Dockerfile
33
33
├── Makefile
34
34
├── README.md
35
35
├── Setup.hs
@@ -42,43 +42,27 @@ This will create a `my-haskell-lambda` directory with the following structure:
42
42
└── stack.yaml
43
43
```
44
44
45
-
Now, add the following to your `stack.yaml` file:
46
-
47
-
```yaml
48
-
packages:
49
-
- .
50
-
51
-
extra-deps:
52
-
- aws-lambda-haskell-runtime-3.0.0
53
-
```
45
+
The project contains a sample handler that you can use as a starting point.
54
46
55
47
## Adding the dependency to an existing project
56
48
57
-
If you currently have a project, you can add this package by adding,
58
-
59
-
to the `stack.yaml` file:
49
+
If you want to add the runtime to an existing project, you can do so by adding the following `extra-dep` entry to the `stack.yaml` file:
60
50
61
51
```yaml
62
52
extra-deps:
63
-
- aws-lambda-haskell-runtime-3.0.0
53
+
- aws-lambda-haskell-runtime-4.0.0
64
54
```
65
55
66
56
and, to the `package.yaml` file:
67
57
68
58
```yaml
69
59
dependencies:
70
60
- ... # other dependencies of your project
71
-
- aws-lambda-haskell-runtime >= 3.0.0
61
+
- aws-lambda-haskell-runtime >= 4.0.0
72
62
```
73
63
74
-
## Keep reading!
75
-
76
-
If you have completed these steps, and type into your terminal:
77
-
78
-
```bash
79
-
stack build
80
-
```
64
+
If you have completed these steps, you should be able to execute `stack build` and see the project build correctly.
Copy file name to clipboardExpand all lines: doc/02-adding-a-handler.md
+23-28Lines changed: 23 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,64 +8,59 @@ In this example, we'll create a person age validator.
8
8
9
9
If you have used the Stack template, you will have a handler that is pre-defined in the `src/Lib.hs` file.
10
10
11
-
If you are starting from scratch, let's write it bit by bit:
12
-
13
-
First, we will enable some language extensions in order to work with JSON easier, also, we'll import the required
14
-
modules:
15
-
16
-
```haskell top hide
17
-
{-# LANGUAGE DeriveGeneric #-}
18
-
{-# LANGUAGE DeriveAnyClass #-}
19
-
```
11
+
First, we need to enable some language extensions in order to make working with JSON easier. We'll also import a few required modules:
20
12
21
13
```haskell
22
14
{-# LANGUAGE DeriveGeneric #-}
23
15
{-# LANGUAGE DeriveAnyClass #-}
24
16
25
17
moduleLibwhere
26
-
```
27
18
28
-
```haskell top
29
19
importAws.Lambda
30
20
importGHC.Generics
31
21
importData.Aeson
32
22
```
33
23
34
-
The runtime will decode the JSON input that reaches the AWSLambda handler, so let's create a type
35
-
for persons.We also tell the compiler to derive (auto-implement) the `Generic`, `FromJSON` and `ToJSON` classes
36
-
for us.
24
+
We'll create a basic handler that validates a person's age is positive.Let's create a `Person` type to use.
37
25
38
26
```haskell top
39
27
dataPerson=Person
40
28
{name::String
41
29
, age::Int
42
-
}deriving (Generic, FromJSON, ToJSON)
30
+
}-- We kindly ask the compiler to autogenerate JSON instances for us
31
+
deriving (Generic, FromJSON, ToJSON)
43
32
```
44
33
45
-
Now, let's write the handler.It**must** be a function that is called `handler`and has a type signature.
34
+
Now, let's implement the actual handler.
46
35
47
-
The arguments to this function will always go like this:
36
+
A handler is a function with the following type signature:
48
37
49
-
*The first argument to this handler will always be the input type we expect (note that it has to implement `FromJSON`).
50
-
*The second argument is the `Aws.Lambda` `Context` type, which has some information regarding our Lambda execution.The `Context` type also takes a `context` parameter, which we can use if we want to have some state that is shared between Lambda calls.For this example, we don't want to have such state, so we'll just use `()`.
38
+
```haskell
39
+
-- Note that request, error and response must all implement ToJSON and FromJSON
Note that both types must implement `ToJSON`, as the runtime will use it to serialize the values.
49
+
This means we expect to be given a `Person` object and we'll returneither some `String` as an erroror some other `Person` object (that passed validation).
58
50
59
-
For example, here we will check if the age of a `Person` is positive, and will returnif it is correct.Ifnot, we
60
-
will return a `String` error:
51
+
You can ignore the `Context()` parameter at this point.This is the Lambda context object which is present in every runtime.By specifying `()` as an inner value, we say we don't want to have anything there.
61
52
62
-
```haskell top
53
+
The implementation of our handler will look like this:
0 commit comments