Skip to content

Commit b04dfc5

Browse files
HarshCasperthrau
andauthored
add lambda hot-reloading example for javascript (#135)
* add lambda hot-reloading example for javascript * Update lambda-hot-reloading/javascript/README.md Co-authored-by: Thomas Rausch <[email protected]> * Update lambda-hot-reloading/javascript/README.md Co-authored-by: Thomas Rausch <[email protected]> Co-authored-by: Thomas Rausch <[email protected]>
1 parent cd023a8 commit b04dfc5

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# LocalStack Demo: Hot code swapping for Lambda functions using LocalStack’s code mounting in JavaScript
2+
3+
## Prerequisites
4+
5+
* LocalStack
6+
* Docker
7+
* `awslocal` CLI
8+
9+
## Starting up
10+
11+
First, we need to make sure we start LocalStack with the right configuration. This is as simple as setting `LAMBDA_REMOTE_DOCKER`(see the [Configuration Documentation](https://docs.localstack.cloud/localstack/configuration/#lambda) for more information):
12+
13+
```bash
14+
LAMBDA_REMOTE_DOCKER=0 localstack start
15+
```
16+
17+
Accordingly, if you are launching LocalStack via Docker or Docker Compose:
18+
19+
```bash
20+
#docker-compose.yml
21+
22+
services:
23+
localstack:
24+
...
25+
environment:
26+
...
27+
- LAMBDA_REMOTE_DOCKER=false
28+
```
29+
30+
Now we need to create an IAM role, which is a collection of policies that grant specific permissions to access AWS resources on our mocked infrastructure. Before we create the role, we must define a trust policy for it. The trust policy has been defined in `trust-policy.json`. To create an IAM role, open your terminal in the directory where you want to create the role and run the following command:
31+
32+
```bash
33+
awslocal iam create-role --role-name lambda-example --assume-role-policy-document ./trust-policy.json
34+
```
35+
36+
To create the Lambda function, you now need to take care of only two things:
37+
38+
- Deploy via an S3 Bucket. You need to use the magic variable `__local__` as the bucket.
39+
- Set the S3 key to the path of the directory your lambda function resides in. The handler is then referenced by the filename of your lambda code and the function in that code that needs to be invoked.
40+
41+
Push the following command to create the Lambda function:
42+
43+
```bash
44+
awslocal lambda create-function --function-name myfirstlambda \
45+
--code S3Bucket="__local__",S3Key="/path/to/local/lambda/code" \
46+
--handler index.handler \
47+
--runtime nodejs14.x \
48+
--role arn:aws:iam::000000000000:role/lambda-example
49+
```
50+
51+
We can quickly make sure that it works by invoking it with a simple payload:
52+
53+
```bash
54+
awslocal lambda invoke --function-name myfirstlambda output.txt
55+
```
56+
57+
The invocation itself returns:
58+
59+
```json
60+
{
61+
"Difference": 10,
62+
"Number1": 21,
63+
"Number2": 31,
64+
"Product": 651,
65+
"Quotient": 0.6774193548387096,
66+
"Sum": 52
67+
}
68+
```
69+
70+
## Changing things up
71+
72+
Now, that we got everything up and running, the fun begins. Because the function is now mounted as a file in the executing container, any change that we save on the file will be there in an instant.
73+
74+
For example, we can now make a minor change to the API and replace the `number1` and `number2` with new values, let's say 10 and 20. Without redeploying or updating the function, the result of the previous request will look like this:
75+
76+
```json
77+
{
78+
"Difference": 10,
79+
"Number1": 10,
80+
"Number2": 20,
81+
"Product": 200,
82+
"Quotient": 0.5,
83+
"Sum": 30
84+
}
85+
```
86+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
exports.handler = (event, context, callback) => {
2+
var number1 = 21;
3+
var number2 = 31;
4+
var sum = number1 + number2;
5+
var product = number1 * number2;
6+
var difference = Math.abs(number1 - number2);
7+
var quotient = number1 / number2;
8+
callback(null, {
9+
"Number1": number1,
10+
"Number2": number2,
11+
"Sum": sum,
12+
"Product": product,
13+
"Difference": difference,
14+
"Quotient": quotient
15+
});
16+
};

lambda-hot-reloading/javascript/package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "lambda-hotreloading-example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Version": "2022-03-15",
3+
"Statement": {
4+
"Effect": "Allow",
5+
"Principal": {
6+
"Service": "lambda.amazonaws.com"
7+
},
8+
"Action": "sts:AssumeRole"
9+
}
10+
}

0 commit comments

Comments
 (0)