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
Why these changes are being introduced:
AWS SAM is a way to run the Lambda as a container in a
way that simulates much of the deployed context: HTTP requests,
memory resources, etc.
How this addresses that need:
* Adds new 'sam' module in tests
* Adds documentation and Makefile commands to support testing
the lambda function with AWS SAM
Side effects of this change:
* None
Relevant ticket(s):
* None
**NOTE:** AWS credentials are automatically passed from the terminal context that runs `make sam-run`; they do not need to be explicitly set as env vars.
56
51
57
-
- Observe output:
52
+
2- Build Docker image:
53
+
```shell
54
+
make sam-build
55
+
```
56
+
57
+
### Invoking Lambda via HTTP requests
58
58
59
-
```
60
-
"You have successfully called this lambda!"
61
-
```
59
+
The following outlines how to run the Lambda SAM docker image as an HTTP endpoint, accepting requests and returning respnoses similar to a lambda behind an ALB, Function URL, or API Gateway.
62
60
63
-
## Running a Specific Handler Locally with Docker
61
+
1- Ensure any required AWS credentials set in terminal, and any other env vars in `tests/sam/env.json` up-to-date.
62
+
63
+
2- Run HTTP server:
64
+
```shell
65
+
make sam-http-run
66
+
```
64
67
65
-
If this repo contains multiple lambda functions, you can call any handler you copy into the container (see Dockerfile) by name as part of the `docker run` command:
68
+
This starts a server at `http://localhost:3000`. Requests must include a path, e.g. `/myapp`, but are arbitrary insofar as the lambda does not utilize them in the request payload.
66
69
67
-
```bash
68
-
docker run -p 9000:8080 my_function:latest lambdas.<a-different-module>.lambda_handler
70
+
3- In another terminal, perform an HTTP request via another `Makefile` command:
71
+
```shell
72
+
make sam-http-ping
69
73
```
70
74
75
+
Response should have an HTTP status of `200` and respond with:
76
+
```json
77
+
{
78
+
"response": "pong"
79
+
}
80
+
```
81
+
82
+
### Invoking Lambda directly
83
+
84
+
While Lambdas can be invoked via HTTP methods (ALB, Function URL, etc.), they are also often invoked directly with an `event` payload. To do so with SAM, you do **not** need to first start an HTTP server with `make sam-run`, you can invoke the function image directly:
85
+
86
+
```shell
87
+
echo'{"action": "ping","challenge_secret":"totally-local-archival-packaging"}'| sam local invoke -e -
As you can see from this response, the lambda is still returning a dictionary that _would_ work for an HTTP response, but is actually just a dictionary with the required information.
96
+
97
+
It's unknown at this time if APT will get invoked via non-HTTP methods, but SAM will be helpful for testing and development if so.
When running a Lambda via SAM, it attempts to parse and setup AWS credentials just like a real Lambda would establish them. Depending on how you setup AWS credentials on your host machine, if they are stale or invalid, you may encounter this error when making your first requests of the Lambda.
104
+
105
+
**Solution:** Stop the SAM container, refresh AWS credentials, and restart it.
0 commit comments