From 41f65c1c2a3173b66a37e22ebfb443074574bdf8 Mon Sep 17 00:00:00 2001 From: ulises-jeremias Date: Wed, 1 Nov 2023 12:32:45 -0300 Subject: [PATCH] Added Serverless LocalStack example --- examples.json | 17 + .../.env.example | 4 + .../.env.local | 4 + .../.eslintrc.js | 22 + .../.eslitignore | 11 + .../.gitignore | 46 + .../.husky/pre-commit | 4 + .../.husky/pre-push | 4 + .../.lintstagedrc.json | 4 + .../.node-version | 1 + .../.prettierignore | 8 + .../.prettierrc.js | 7 + .../README.md | 93 + .../compose.yml | 45 + .../docs/DEPLOYMENT.md | 25 + .../docs/README.md | 8 + .../jest.config.js | 11 + .../localstack/.gitignore | 2 + .../localstack/README.md | 44 + .../localstack/setup-resources.sh | 21 + .../package.json | 65 + .../pnpm-lock.yaml | 9836 +++++++++++++++++ .../resource/DynamoDB.yml | 22 + .../resource/S3.yml | 13 + .../serverless.yml | 75 + .../src/helpers/env.ts | 29 + .../src/rest/program.ts | 87 + .../src/rest/serverless.yml | 7 + .../tsconfig.json | 21 + 29 files changed, 10536 insertions(+) create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.env.example create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.env.local create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.eslintrc.js create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.eslitignore create mode 100755 examples/serverless-localstack-with-s3-and-dynamodb/.gitignore create mode 100755 examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-commit create mode 100755 examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-push create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.lintstagedrc.json create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.node-version create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.prettierignore create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/.prettierrc.js create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/README.md create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/compose.yml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/docs/DEPLOYMENT.md create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/docs/README.md create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/jest.config.js create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/localstack/.gitignore create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/localstack/README.md create mode 100755 examples/serverless-localstack-with-s3-and-dynamodb/localstack/setup-resources.sh create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/package.json create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/pnpm-lock.yaml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/resource/DynamoDB.yml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/resource/S3.yml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/serverless.yml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/src/helpers/env.ts create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/src/rest/serverless.yml create mode 100644 examples/serverless-localstack-with-s3-and-dynamodb/tsconfig.json diff --git a/examples.json b/examples.json index c97d1e5..4039dae 100644 --- a/examples.json +++ b/examples.json @@ -602,6 +602,23 @@ "MariaDB Database", "MySQL Database" ] + }, + { + "name": "Serverless LocalStack with S3 and DynamoDB", + "description": "Serverless Framework example to run lambda functions locally using [Serverless Offline](https://www.serverless.com/plugins/serverless-offline) with LocalStack. It provides a full local development environment using [Serverless LocalStack](https://www.serverless.com/plugins/serverless-localstack).", + "url": "https://github.com/nanlabs/devops-reference/tree/main/examples/serverless-localstack-with-s3-and-dynamodb/", + "tags": [ + "Examples > DevOps > Infrastructure as Code > Serverless Framework, SAM and CloudFormation", + "Examples > DevOps > Containers, Orchestration and Serverless > Containers and Compositions (Docker, Docker Compose, Buildpacks and more)" + ], + "labels": [ + "Serverless Framework", + "Serverless Offline", + "Serverless LocalStack", + "LocalStack", + "S3", + "DynamoDB" + ] } ] } diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.env.example b/examples/serverless-localstack-with-s3-and-dynamodb/.env.example new file mode 100644 index 0000000..582404e --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.env.example @@ -0,0 +1,4 @@ +# Deployment Configuration +SERVICE_NAME=serverless-localstack-example + +PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.env.local b/examples/serverless-localstack-with-s3-and-dynamodb/.env.local new file mode 100644 index 0000000..582404e --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.env.local @@ -0,0 +1,4 @@ +# Deployment Configuration +SERVICE_NAME=serverless-localstack-example + +PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.eslintrc.js b/examples/serverless-localstack-with-s3-and-dynamodb/.eslintrc.js new file mode 100644 index 0000000..39df139 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.eslintrc.js @@ -0,0 +1,22 @@ +module.exports = { + parser: '@typescript-eslint/parser', + parserOptions: { + project: './tsconfig.json', + tsconfigRootDir: __dirname, + }, + plugins: ['@typescript-eslint/eslint-plugin'], + extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'], + root: true, + env: { + node: true, + jest: true, + }, + ignorePatterns: ['.eslintrc.js'], + rules: { + '@typescript-eslint/interface-name-prefix': 'off', + '@typescript-eslint/explicit-function-return-type': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-empty-function': 'off', + }, +}; diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.eslitignore b/examples/serverless-localstack-with-s3-and-dynamodb/.eslitignore new file mode 100644 index 0000000..dea4e6a --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.eslitignore @@ -0,0 +1,11 @@ +# /node_modules/* in the project root is ignored by default +# build artefacts +dist/* +coverage/* +node_modules/* +logs/* +prod/* +.husky/* +.github/* +tools/ +localstack/volume diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.gitignore b/examples/serverless-localstack-with-s3-and-dynamodb/.gitignore new file mode 100755 index 0000000..b2e6f23 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.gitignore @@ -0,0 +1,46 @@ +# compiled output +/dist +/node_modules +.tsbuildinfo + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# env +.env +.envrc + +# Serverless Framework +.serverless +.build +.webpack +.local-s3 +.volume diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-commit b/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-commit new file mode 100755 index 0000000..8c86c46 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +pnpm run lint-staged diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-push b/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-push new file mode 100755 index 0000000..2f3fc64 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.husky/pre-push @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +pnpm run type-check diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.lintstagedrc.json b/examples/serverless-localstack-with-s3-and-dynamodb/.lintstagedrc.json new file mode 100644 index 0000000..34eee43 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.lintstagedrc.json @@ -0,0 +1,4 @@ +{ + "*.{js,jsx,ts,tsx}": ["prettier --write", "npm run lint:fix"], + "*.{json,css,sass,scss,less,html,md,yml,yaml}": ["prettier --write"] +} diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.node-version b/examples/serverless-localstack-with-s3-and-dynamodb/.node-version new file mode 100644 index 0000000..e048c8c --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.node-version @@ -0,0 +1 @@ +v18.15.0 diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.prettierignore b/examples/serverless-localstack-with-s3-and-dynamodb/.prettierignore new file mode 100644 index 0000000..d1ea712 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.prettierignore @@ -0,0 +1,8 @@ +# /node_modules/* in the project root is ignored by default +# build artefacts +dist/* +coverage/* +node_modules/* +logs/* +prod/* +localstack/volume diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.prettierrc.js b/examples/serverless-localstack-with-s3-and-dynamodb/.prettierrc.js new file mode 100644 index 0000000..13c2be1 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.prettierrc.js @@ -0,0 +1,7 @@ +module.exports = { + semi: true, + trailingComma: 'all', + singleQuote: true, + printWidth: 120, + tabWidth: 2, +}; diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/README.md b/examples/serverless-localstack-with-s3-and-dynamodb/README.md new file mode 100644 index 0000000..1a31a84 --- /dev/null +++ b/examples/serverless-localstack-with-s3-and-dynamodb/README.md @@ -0,0 +1,93 @@ +# Serverless LocalStack Example [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) + +This project is a TypeScript based API generated using [create-awesome-node-app](https://www.npmjs.com/package/create-awesome-node-app). However, it is recommended to generate your own project using the command and following the options in the interactive menu. Please refer to the documentation for more information. + +We use Serverless Framework to do production ready deployments and local development using +_serverless-offline_. + +## Features + +- 🦾 [TypeScript](https://www.typescriptlang.org/) - Ensures type safety +- 🌐 API Gateway - Build scalable and maintainable APIs +- ⚙️ Configuration Management - Manage different environments and configurations +- 📜 Logging - Implement logging for debugging and monitoring purposes +- 🧪 Unit Testing - Write and execute unit tests to ensure code quality + +## Pre-packed with + +- [TypeScript](https://www.typescriptlang.org/) +- [ESLint](https://eslint.org/) - JavaScript and JSX linting utility +- [Prettier](https://prettier.io/) - Opinionated code formatter +- [Husky](https://www.npmjs.com/package/husky) - Simplifies Git hooks setup +- [lint-staged](https://www.npmjs.com/package/lint-staged) - Runs linters against staged git files, preventing 💩 from slipping into your code base! + +## Requirements + +- [Docker](https://www.docker.com/) +- [Docker Composer](https://docs.docker.com/compose/) +- [Node.js](https://nodejs.org/en/) 18 or later + + **You’ll need to have Node 18 or later on your local development machine** (but it’s not required on the server). You can use [fnm](https://github.com/Schniz/fnm) to easily switch Node versions between different projects. + +## Quickstart + +```sh +# Setup NodeJs and install dependencies +fnm use +pnpm install + +# Run the Local Stack using Docker Compose +docker-compose up -d + +# Run the application locally +pnpm run sls:offline +``` + +## Testing locally + +```sh +curl -X POST http://localhost:3000/program --header 'Content-Type': 'application/json' --data '{ + "content": "console.log(\"Hello World\");", + "filePath": "example/file.js" +}' +``` + +Then run the following command to check the uploaded file: + +```sh +# Set the AWS credentials +export AWS_ACCESS_KEY_ID="dummy-value" +export AWS_SECRET_ACCESS_KEY="dummy-value" + +# List the uploaded files +aws --endpoint-url=http://localhost:4566 --no-verify-ssl s3 ls --recursive serverless-localstack-bucket +# 2023-10-31 18:00:00 18 example/file.js +``` + +## Development + +While developing, you will mostly rely on `pnpm run dev`; however, there are additional scripts available: + +| `pnpm run