Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws-node-typescript-multi-instance-lambda community example #729

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aws-node-typescript-multi-instance-lambda/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
ecmaVersion: 2018,
sourceType: "module",
},
env: {
"jest/globals": true,
es6: true,
},
plugins: [
"jest",
"sonarjs",
"@typescript-eslint",
"prettier",
],
};
105 changes: 105 additions & 0 deletions aws-node-typescript-multi-instance-lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/
.webpack/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions aws-node-typescript-multi-instance-lambda/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
21 changes: 21 additions & 0 deletions aws-node-typescript-multi-instance-lambda/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Daniel Simpson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions aws-node-typescript-multi-instance-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Multi-instance Serverless AWS Lambda function example

This example demonstrates how to deploy multiple instances (or copies) of the same lambda function by using the "--param" `sls` option to pass in an instance name. The main idea is to rename AWS resources and outputs which would otherwise clash using the instance name.

A lambda instance's configuration can then be made instance-specific by adding the instance name to its environment variable or parameter store keys.

The benefit of this approach is that only one lambda function needs to be defined in `serverless.ts` (or `serverless.yml`), but any number of copies of it can be deployed directly from the command line.

## Setup

1. Install Node.js 18 (recommended: [Node Version Manager](https://github.com/nvm-sh/nvm#install--update-script)).

2. Install packages:
```bash
npm i
```

## Use case

When you want to deploy multiple lambda functions with the same implementation, but you don't want to explicitly define each copy in `serverless.yml`.

For example, you could deploy the lambda copies in a continuous deployment (CD) pipeline and configure them to fetch data from different sources based on their instance names.

## Usage

### Deployment

To deploy an instance "foo" of the lambda `hello` to the default `dev` stage in the `ap-southeast-2` region , run:

```bash
npx sls deploy --region ap-southeast-2 --param="instance=foo"
```

Another instance, "bar", can be deployed with:

```bash
npx sls deploy --region ap-southeast-2 --param="instance=bar"
```

Now, two separate yet identical `hello` lambdas are available on AWS:
<img src="./.images/aws-lambda-screenshot.png" alt="Both instances of `hello` are deployed." width="600"/>
<br>

Note that the "--param" option is now required when running other `serverless` commands, such as `sls info`:

```bash
npx sls info --region ap-southeast-2 --param="instance=foo"
```

### Invocation

To invoke an instance of `hello` using `sls invoke`, pass the instance name to the "--param" option:

```bash
npx sls invoke -f hello --region ap-southeast-2 --param="instance=foo"
```

Output:

```
{
"statusCode": 200,
"body": "{\n \"message\": \"Function `aws-node-typescript-multi-instance-lambda-foo-dev-hello` executed successfully.\",\n \"input\": {}\n}"
}
```

### Removal

"--param" is also required to run the `sls remove` command to tear down the CloudFormation stack associated with a specific instance.

```bash
npx sls remove --region ap-southeast-2 --param="instance=foo"
```

## Acknowledgements

- [This comment](https://github.com/serverless/serverless/issues/9361#issuecomment-884602588) by [**@rdemorais**](https://github.com/rdemorais) on Serverless issue [#9361](https://github.com/serverless/serverless/issues/9361).
- [**@billkidwell**](https://github.com/billkidwell)'s [Simple Kinesis Example](https://github.com/serverless/examples/tree/v3/aws-node-typescript-kinesis).
13 changes: 13 additions & 0 deletions aws-node-typescript-multi-instance-lambda/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Context } from "aws-lambda";

module.exports.hello = async (event: unknown, context: Context) => ({
statusCode: 200,
body: JSON.stringify(
{
message: `Function \`${context.functionName}\` executed successfully.`,
input: event,
},
null,
2
),
});
33 changes: 33 additions & 0 deletions aws-node-typescript-multi-instance-lambda/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "aws-node-typescript-multi-instance-lambda",
"version": "1.0.0",
"description": "Serverless example demonstrating how to deploy multiple instances of the same lambda",
"main": "serverless.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Daniel Simpson",
"license": "MIT",
"devDependencies": {
"@serverless/typescript": "^3.25.0",
"@types/aws-lambda": "^8.10.109",
"@types/node": "^18.11.11",
"@typescript-eslint/eslint-plugin": "^5.45.1",
"@typescript-eslint/parser": "^5.45.1",
"eslint": "^8.29.0",
"import": "^0.0.6",
"jest": "^29.3.1",
"prettier": "^2.8.1",
"serverless": "^3.25.1",
"serverless-webpack": "^5.11.0",
"sonarjs": "^1.0.0",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.3",
"webpack": "^5.75.0"
},
"dependencies": {
"aws-lambda": "^1.0.7",
"source-map-support": "^0.5.21"
}
}
105 changes: 105 additions & 0 deletions aws-node-typescript-multi-instance-lambda/serverless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { AWS } from "@serverless/typescript";

const serverlessConfiguration: AWS = {
service: "aws-node-typescript-multi-instance-lambda",
frameworkVersion: "3",
configValidationMode: "error",
plugins: ["serverless-webpack"],
provider: {
name: "aws",
runtime: "nodejs18.x",
// region: "ap-southeast-2",
// Rename CloudFormation stack to include the instance name
stackName: "${self:service}-${param:instance}-${sls:stage}",
},
functions: {
hello: {
// Rename function to include the instance name
name: "${self:service}-${param:instance}-${sls:stage}-hello",
handler: "handler.hello",
environment: {
INSTANCE_NAME: "${param:instance}",
},
},
},

resources: {
Resources: {
IamRoleLambdaExecution: {
Type: "AWS::IAM::Role",
Properties: {
Policies: [
{
PolicyName: {
"Fn::Join": [
"-",
[
"${self:service}",
"${param:instance}",
"${sls:stage}",
"lambda",
],
],
},
},
],
// Include instance name to avoid resource collision between instances
RoleName: {
"Fn::Join": [
"-",
[
// "${self:service}",
"multi-instance", // shorten service name due to 64-character "roleName" length requirement
"${param:instance}",
"${sls:stage}",
{
Ref: "AWS::Region",
},
"lambdaRole",
],
],
},
},
},
},
Outputs: {
// Rename output export names to include instance name
// https://github.com/serverless/serverless/issues/9361#issuecomment-884602588
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
HelloLambdaFunctionQualifiedArn: {
Export: {
Name: {
"Fn::Join": [
"-",
[
"sls",
"${self:service}",
"${param:instance}",
"${sls:stage}",
"HelloLambdaFunctionQualifiedArn",
],
],
},
},
},
ServerlessDeploymentBucketName: {
Export: {
Name: {
"Fn::Join": [
"-",
[
"sls",
"${self:service}",
"${param:instance}",
"${sls:stage}",
"ServerlessDeploymentBucketName",
],
],
},
},
},
},
},
};

module.exports = serverlessConfiguration;
Loading