Skip to content

Commit 8c67f3f

Browse files
committed
Add example to demonstrate S3 file upload triggering SNS, which invokes Lambda based on prefix
1 parent 7f2d778 commit 8c67f3f

File tree

16 files changed

+342
-0
lines changed

16 files changed

+342
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"eslint-config-airbnb-base",
5+
"airbnb-typescript",
6+
"plugin:@typescript-eslint/recommended",
7+
"prettier"
8+
],
9+
"env": {
10+
"es2021": true,
11+
"node": true
12+
},
13+
"parser": "@typescript-eslint/parser",
14+
"plugins": ["@typescript-eslint", "prettier"],
15+
"rules": {
16+
"@typescript-eslint/no-unused-vars": "error",
17+
"import/prefer-default-export": "off",
18+
"no-tabs": ["off"],
19+
"max-len": ["off"],
20+
"no-await-in-loop": "off",
21+
"react/jsx-filename-extension": "off",
22+
"class-methods-use-this": "off",
23+
"no-new": "off",
24+
"no-param-reassign": "off",
25+
"@typescript-eslint/quotes": [
26+
"error",
27+
"single",
28+
{
29+
"avoidEscape": true
30+
}
31+
],
32+
"@typescript-eslint/comma-dangle": ["error", "never"],
33+
"@typescript-eslint/no-namespace": "off",
34+
"@typescript-eslint/no-non-null-assertion": "off",
35+
"@typescript-eslint/no-use-before-define": "off",
36+
"@typescript-eslint/no-explicit-any": "off",
37+
"@typescript-eslint/lines-between-class-members": [
38+
"error",
39+
"always",
40+
{
41+
"exceptAfterSingleLine": true
42+
}
43+
],
44+
"prettier/prettier": [
45+
"error",
46+
{
47+
"tabWidth": 2
48+
}
49+
]
50+
},
51+
"overrides": [
52+
{
53+
"files": ["*.spec.{js,ts}"],
54+
"env": {
55+
"mocha": true,
56+
"node": true
57+
},
58+
"rules": {
59+
"no-unused-expressions": "off",
60+
"@typescript-eslint/no-unused-expressions": "off",
61+
"@typescript-eslint/no-throw-literal": "off"
62+
}
63+
}
64+
],
65+
"parserOptions": {
66+
"project": "./tsconfig.json"
67+
},
68+
"ignorePatterns": [
69+
"dist",
70+
"node_modules",
71+
".eslintrc.js",
72+
"jest.config.js",
73+
"functions",
74+
"cdk.out"
75+
]
76+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Created by https://www.gitignore.io/api/osx,node,linux,windows
2+
3+
### Linux ###
4+
*~
5+
# Temporary files which can be created if a process still has a handle open of a deleted file
6+
.fuse_hidden*
7+
# KDE directory preferences
8+
.directory
9+
# Linux trash folder which might appear on any partition or disk
10+
.Trash-*
11+
# .nfs files are created when an open file is removed but is still being accessed
12+
.nfs*
13+
14+
### Node ###
15+
# Logs
16+
logs
17+
*.log
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*
21+
# Runtime data
22+
pids
23+
*.pid
24+
*.seed
25+
*.pid.lock
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
# nyc test coverage
31+
.nyc_output
32+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
33+
.grunt
34+
# Bower dependency directory (https://bower.io/)
35+
bower_components
36+
# node-waf configuration
37+
.lock-wscript
38+
# Compiled binary addons (http://nodejs.org/api/addons.html)
39+
build/Release
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
# Typescript v1 declaration files
44+
typings/
45+
# Optional npm cache directory
46+
.npm
47+
# npm list directory
48+
target
49+
# Optional eslint cache
50+
.eslintcache
51+
# Optional REPL history
52+
.node_repl_history
53+
# Output of 'npm pack'
54+
*.tgz
55+
# Yarn Integrity file
56+
.yarn-integrity
57+
# dotenv environment variables file
58+
.env
59+
60+
### OSX ###
61+
*.DS_Store
62+
.AppleDouble
63+
.LSOverride
64+
# Icon must end with two \r
65+
Icon
66+
# Thumbnails
67+
._*
68+
# Files that might appear in the root of a volume
69+
.DocumentRevisions-V100
70+
.fseventsd
71+
.Spotlight-V100
72+
.TemporaryItems
73+
.Trashes
74+
.VolumeIcon.icns
75+
.com.apple.timemachine.donotpresent
76+
# Directories potentially created on remote AFP share
77+
.AppleDB
78+
.AppleDesktop
79+
Network Trash Folder
80+
Temporary Items
81+
.apdisk
82+
83+
### Windows ###
84+
# Windows thumbnail cache files
85+
Thumbs.db
86+
ehthumbs.db
87+
ehthumbs_vista.db
88+
# Folder config file
89+
Desktop.ini
90+
# Recycle Bin used on file shares
91+
$RECYCLE.BIN/
92+
# Windows Installer files
93+
*.cab
94+
*.msi
95+
*.msm
96+
*.msp
97+
# Windows shortcuts
98+
*.lnk
99+
100+
# AWS SAM and CLI directories
101+
.aws/
102+
.aws-sam/
103+
104+
# End of https://www.gitignore.io/api/osx,node,linux,windows
105+
106+
# Custom entries
107+
test.ts
108+
db.sqlite
109+
.turbo
110+
*.sqlite3
111+
output
112+
samconfig.toml
113+
build
114+
.vscode
115+
cdk.out
116+
cdk.context.json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cdk.out
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"printWidth": 85,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"quoteProps": "as-needed",
8+
"jsxSingleQuote": false,
9+
"trailingComma": "none",
10+
"bracketSpacing": true,
11+
"jsxBracketSameLine": false,
12+
"arrowParens": "always",
13+
"proseWrap": "always",
14+
"plugins": [
15+
"prettier-plugin-packagejson",
16+
"@trivago/prettier-plugin-sort-imports"
17+
],
18+
"importOrder": ["^dotenv/config$", "<THIRD_PARTY_MODULES>", "^[./]"],
19+
"importOrderSeparation": true,
20+
"importOrderSortSpecifiers": true,
21+
"importOrderCaseInsensitive": false
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# S3, SNS, Lambda Integration
2+
3+
This AWS CDK configuration sets up an automated workflow to trigger a specific Lambda
4+
function based on the prefix of files uploaded to Amazon S3. Using the SNS service
5+
with a filter policy, it directs events to the appropriate Lambda function
6+
efficiently.
7+
8+
## Get Started
9+
10+
1. Install AWS CDK and Bootstrap Your AWS Account in a Specific Region
11+
12+
```
13+
npm install -g aws-cdk
14+
cdk bootstrap
15+
```
16+
17+
2. Create a `.env` File with Your AWS Account ID
18+
19+
```
20+
AWS_ACCOUNT_ID=YOUR_ACCOUNT_ID
21+
```
22+
23+
3. Install Dependencies
24+
25+
1. Run the following command in the project root to install all dependencies:
26+
27+
```
28+
yarn
29+
```
30+
31+
2. Then navigate to the functions folder to install the required node modules for
32+
Lambda functions:
33+
```
34+
cd ./functions yarn
35+
```
36+
37+
4. Deploy to AWS
38+
39+
- Use this command to deploy all stacks to AWS:
40+
41+
```
42+
cdk deploy --all
43+
```
44+
45+
## Try it out
46+
47+
After running the above commands, an S3 bucket will be created. Upload a file to one
48+
of the designated folders (e.g., folder1 or folder2). Depending on the folder, the
49+
corresponding Lambda function (e.g., lambda1 or lambda2) will be triggered
50+
automatically.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "ts-node main.ts"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"nodeEnv": "NODE_ENV",
3+
"aws": {
4+
"region": "AWS_REGION",
5+
"accountId": "AWS_ACCOUNT_ID"
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"nodeEnv": "qa",
3+
"aws": {
4+
"region": "us-east-1"
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nodeEnv": "develop"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nodeEnv": "production"
3+
}

0 commit comments

Comments
 (0)