|
| 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 | + |
0 commit comments