Skip to content

Commit 1c7f6da

Browse files
committed
First commit
1 parent fc433a7 commit 1c7f6da

File tree

11 files changed

+418
-0
lines changed

11 files changed

+418
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
parser: "@typescript-eslint/parser",
3+
parserOptions: {
4+
project: "./tsconfig.json",
5+
ecmaVersion: 2018,
6+
sourceType: "module",
7+
},
8+
env: {
9+
"jest/globals": true,
10+
es6: true,
11+
},
12+
plugins: [
13+
"jest",
14+
"sonarjs",
15+
"@typescript-eslint",
16+
"prettier",
17+
],
18+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
.webpack/
97+
98+
# FuseBox cache
99+
.fusebox/
100+
101+
# DynamoDB Local files
102+
.dynamodb/
103+
104+
# TernJS port file
105+
.tern-port
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Daniel Simpson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Multi-instance Serverless AWS Lambda function example
2+
3+
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.
4+
5+
A lambda instance's configuration can then be made instance-specific by adding the instance name to its environment variable or parameter store keys.
6+
7+
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.
8+
9+
## Setup
10+
11+
1. Install Node.js 18 (recommended: [Node Version Manager](https://github.com/nvm-sh/nvm#install--update-script)).
12+
13+
2. Install packages:
14+
```bash
15+
npm i
16+
```
17+
18+
## Use case
19+
20+
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`.
21+
22+
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.
23+
24+
## Usage
25+
26+
### Deployment
27+
28+
To deploy an instance "foo" of the lambda `hello` to the default `dev` stage in the `ap-southeast-2` region , run:
29+
30+
```bash
31+
npx sls deploy --region ap-southeast-2 --param="instance=foo"
32+
```
33+
34+
Another instance, "bar", can be deployed with:
35+
36+
```bash
37+
npx sls deploy --region ap-southeast-2 --param="instance=bar"
38+
```
39+
40+
Now, two separate yet identical `hello` lambdas are available on AWS:
41+
<img src="./.images/aws-lambda-screenshot.png" alt="Both instances of `hello` are deployed." width="600"/>
42+
<br>
43+
44+
Note that the "--param" option is now required when running other `serverless` commands, such as `sls info`:
45+
46+
```bash
47+
npx sls info --region ap-southeast-2 --param="instance=foo"
48+
```
49+
50+
### Invocation
51+
52+
To invoke an instance of `hello` using `sls invoke`, pass the instance name to the "--param" option:
53+
54+
```bash
55+
npx sls invoke -f hello --region ap-southeast-2 --param="instance=foo"
56+
```
57+
58+
Output:
59+
60+
```
61+
{
62+
"statusCode": 200,
63+
"body": "{\n \"message\": \"Function `aws-node-typescript-multi-instance-lambda-foo-dev-hello` executed successfully.\",\n \"input\": {}\n}"
64+
}
65+
```
66+
67+
### Removal
68+
69+
"--param" is also required to run the `sls remove` command to tear down the CloudFormation stack associated with a specific instance.
70+
71+
```bash
72+
npx sls remove --region ap-southeast-2 --param="instance=foo"
73+
```
74+
75+
## Acknowledgements
76+
77+
- [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).
78+
- [**@billkidwell**](https://github.com/billkidwell)'s [Simple Kinesis Example](https://github.com/serverless/examples/tree/v3/aws-node-typescript-kinesis).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Context } from "aws-lambda";
2+
3+
module.exports.hello = async (event: unknown, context: Context) => ({
4+
statusCode: 200,
5+
body: JSON.stringify(
6+
{
7+
message: `Function \`${context.functionName}\` executed successfully.`,
8+
input: event,
9+
},
10+
null,
11+
2
12+
),
13+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "aws-node-typescript-multi-instance-lambda",
3+
"version": "1.0.0",
4+
"description": "Serverless example demonstrating how to deploy multiple instances of the same lambda",
5+
"main": "serverless.ts",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Daniel Simpson",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"@serverless/typescript": "^3.25.0",
13+
"@types/aws-lambda": "^8.10.109",
14+
"@types/node": "^18.11.11",
15+
"@typescript-eslint/eslint-plugin": "^5.45.1",
16+
"@typescript-eslint/parser": "^5.45.1",
17+
"eslint": "^8.29.0",
18+
"import": "^0.0.6",
19+
"jest": "^29.3.1",
20+
"prettier": "^2.8.1",
21+
"serverless": "^3.25.1",
22+
"serverless-webpack": "^5.11.0",
23+
"sonarjs": "^1.0.0",
24+
"ts-loader": "^9.4.2",
25+
"ts-node": "^10.9.1",
26+
"typescript": "^4.9.3",
27+
"webpack": "^5.75.0"
28+
},
29+
"dependencies": {
30+
"aws-lambda": "^1.0.7",
31+
"source-map-support": "^0.5.21"
32+
}
33+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { AWS } from "@serverless/typescript";
2+
3+
const serverlessConfiguration: AWS = {
4+
service: "aws-node-typescript-multi-instance-lambda",
5+
frameworkVersion: "3",
6+
configValidationMode: "error",
7+
plugins: ["serverless-webpack"],
8+
provider: {
9+
name: "aws",
10+
runtime: "nodejs18.x",
11+
// region: "ap-southeast-2",
12+
// Rename CloudFormation stack to include the instance name
13+
stackName: "${self:service}-${param:instance}-${sls:stage}",
14+
},
15+
functions: {
16+
hello: {
17+
// Rename function to include the instance name
18+
name: "${self:service}-${param:instance}-${sls:stage}-hello",
19+
handler: "handler.hello",
20+
environment: {
21+
INSTANCE_NAME: "${param:instance}",
22+
},
23+
},
24+
},
25+
26+
resources: {
27+
Resources: {
28+
IamRoleLambdaExecution: {
29+
Type: "AWS::IAM::Role",
30+
Properties: {
31+
Policies: [
32+
{
33+
PolicyName: {
34+
"Fn::Join": [
35+
"-",
36+
[
37+
"${self:service}",
38+
"${param:instance}",
39+
"${sls:stage}",
40+
"lambda",
41+
],
42+
],
43+
},
44+
},
45+
],
46+
// Include instance name to avoid resource collision between instances
47+
RoleName: {
48+
"Fn::Join": [
49+
"-",
50+
[
51+
// "${self:service}",
52+
"multi-instance", // shorten service name due to 64-character "roleName" length requirement
53+
"${param:instance}",
54+
"${sls:stage}",
55+
{
56+
Ref: "AWS::Region",
57+
},
58+
"lambdaRole",
59+
],
60+
],
61+
},
62+
},
63+
},
64+
},
65+
Outputs: {
66+
// Rename output export names to include instance name
67+
// https://github.com/serverless/serverless/issues/9361#issuecomment-884602588
68+
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
69+
HelloLambdaFunctionQualifiedArn: {
70+
Export: {
71+
Name: {
72+
"Fn::Join": [
73+
"-",
74+
[
75+
"sls",
76+
"${self:service}",
77+
"${param:instance}",
78+
"${sls:stage}",
79+
"HelloLambdaFunctionQualifiedArn",
80+
],
81+
],
82+
},
83+
},
84+
},
85+
ServerlessDeploymentBucketName: {
86+
Export: {
87+
Name: {
88+
"Fn::Join": [
89+
"-",
90+
[
91+
"sls",
92+
"${self:service}",
93+
"${param:instance}",
94+
"${sls:stage}",
95+
"ServerlessDeploymentBucketName",
96+
],
97+
],
98+
},
99+
},
100+
},
101+
},
102+
},
103+
};
104+
105+
module.exports = serverlessConfiguration;

0 commit comments

Comments
 (0)