├── .env.example ├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .nvmrc ├── .prettierrc ├── .vscode └── launch.json ├── LICENSE ├── jest.config.js ├── nodemon.json ├── package.json ├── serverless.yml ├── src └── functions │ ├── goodbye.ts │ ├── hello.ts │ └── question.ts ├── tsconfig.json ├── tsconfig.prod.json └── yarn.lock /.env.example: -------------------------------------------------------------------------------- 1 | MY_NAME= -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | coverage -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | es2021: true, 4 | node: true, 5 | jest: true, 6 | }, 7 | extends: [ 8 | 'eslint:recommended', 9 | 'plugin:@typescript-eslint/recommended', 10 | 'airbnb-base', 11 | 'plugin:prettier/recommended', 12 | 'plugin:import/errors', 13 | 'plugin:import/warnings', 14 | 'plugin:import/typescript', 15 | ], 16 | parser: '@typescript-eslint/parser', 17 | parserOptions: { 18 | ecmaVersion: 12, 19 | sourceType: 'module', 20 | }, 21 | plugins: ['@typescript-eslint', 'prettier', 'import'], 22 | rules: { 23 | 'prettier/prettier': 'error', 24 | 'import/extensions': 'off', 25 | 'import/no-unresolved': 'error', 26 | 'no-console': 'off', 27 | 'no-unused-vars': ['off', { args: 'all', argsIgnorePattern: '^_' }], 28 | '@typescript-eslint/no-unused-vars': ['off', { args: 'all', argsIgnorePattern: '^_' }], 29 | 'import/prefer-default-export': 'off', 30 | 'import/order': [ 31 | 'error', 32 | { 33 | 'newlines-between': 'never', 34 | groups: [ 35 | ['builtin', 'external'], 36 | ['internal', 'parent', 'sibling', 'index'], 37 | ], 38 | }, 39 | ], 40 | }, 41 | settings: { 42 | 'import/parsers': { 43 | '@typescript-eslint/parser': ['.ts'], 44 | }, 45 | 'import/resolver': { 46 | typescript: { 47 | alwaysTryTypes: true, // always try to resolve types under `@types` directory even it doesn't contain any source code, like `@types/unist` 48 | 49 | // Choose from one of the "project" configs below or omit to use /tsconfig.json by default 50 | 51 | // use /path/to/folder/tsconfig.json 52 | project: './tsconfig.json', 53 | }, 54 | }, 55 | }, 56 | }; 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | coverage 4 | .env 5 | .serverless 6 | .build -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v14.15.4 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "printWidth": 120, 4 | "singleQuote": true, 5 | "trailingComma": "es5", 6 | "arrowParens": "avoid", 7 | "semi": true 8 | } 9 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Serverless Offline", 11 | "program": "${workspaceFolder}/node_modules/serverless/bin/serverless", 12 | "args": ["offline", "start", "--httpPort", "4000", "--noTimeout"], 13 | "protocol": "inspector", 14 | "runtimeExecutable": "node", 15 | "env": {}, // in case env variables are needed 16 | "windows": { 17 | "program": "${workspaceFolder}/node_modules/serverless/bin/serverless" 18 | } 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Leonardo Roese 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. 22 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | testEnvironment: 'node', 4 | coverageDirectory: 'coverage', 5 | collectCoverageFrom: ['src/**/*.{js,ts}'], 6 | coverageThreshold: { 7 | global: { 8 | branches: 0, 9 | functions: 0, 10 | lines: 0, 11 | statements: 0, 12 | }, 13 | }, 14 | moduleNameMapper: { 15 | 'src/(.*)': '/src/$1', 16 | }, 17 | moduleDirectories: ['node_modules', 'src'], 18 | testPathIgnorePatterns: ['/dist/', '/node_modules/'], 19 | }; 20 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts,js,json", 4 | "ignore": ["node_modules", "coverage", "dist"], 5 | "exec": "ts-node -r tsconfig-paths/register ./src/index.ts", 6 | "restartable": "rs", 7 | "env": { 8 | "NODE_ENV": "development" 9 | } 10 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-ts-serverless", 3 | "version": "1.0.0", 4 | "description": "AWS Serverless setup with Typescript Functions", 5 | "main": "index.js", 6 | "repository": "https://github.com/leoroese/blog-tube", 7 | "author": "Leonardo Roese", 8 | "license": "MIT", 9 | "scripts": { 10 | "dev": "sls offline -s dev", 11 | "test": "jest", 12 | "test:watch": "jest --watch", 13 | "test:coverage": "jest --coverage" 14 | }, 15 | "devDependencies": { 16 | "@types/aws-lambda": "^8.10.80", 17 | "@types/dotenv-safe": "^8.1.2", 18 | "@types/jest": "^26.0.24", 19 | "@types/node": "^16.4.1", 20 | "@types/serverless": "^1.78.32", 21 | "@typescript-eslint/eslint-plugin": "^4.28.4", 22 | "@typescript-eslint/parser": "^4.28.4", 23 | "eslint": "^7.31.0", 24 | "eslint-config-airbnb-base": "^14.2.1", 25 | "eslint-config-prettier": "^8.3.0", 26 | "eslint-import-resolver-typescript": "^2.4.0", 27 | "eslint-plugin-import": "^2.23.4", 28 | "eslint-plugin-prettier": "^3.4.0", 29 | "jest": "^27.0.6", 30 | "nodemon": "^2.0.12", 31 | "prettier": "^2.3.2", 32 | "serverless": "^2.52.1", 33 | "serverless-offline": "^7.0.0", 34 | "serverless-plugin-typescript": "^1.1.9", 35 | "ts-jest": "^27.0.4", 36 | "ts-node": "^10.1.0", 37 | "tsconfig-paths": "^3.10.1", 38 | "typescript": "^4.3.5" 39 | }, 40 | "dependencies": { 41 | "aws-lambda": "^1.0.6", 42 | "cross-env": "^7.0.3", 43 | "dotenv-safe": "^8.2.0" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverlessSetup 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs12.x 6 | region: us-west-1 7 | 8 | # functions 9 | functions: 10 | hello: 11 | handler: src/functions/hello.handler 12 | events: # The events that trigger this function 13 | - http: 14 | path: serverlessSetup/hello 15 | method: get 16 | goodbye: 17 | handler: src/functions/goodbye.handler 18 | events: 19 | - http: 20 | path: serverlessSetup/goodbye 21 | method: post 22 | question: 23 | handler: src/functions/question.handler 24 | dependsOn: SNSHelloEvent 25 | events: 26 | - sns: 27 | arn: !Ref SNSHelloEvent 28 | topicName: hello-event 29 | 30 | # Serverless plugins 31 | plugins: 32 | - serverless-plugin-typescript 33 | - serverless-offline 34 | 35 | # Resources your functions use 36 | resources: 37 | Resources: 38 | # SNS Topics 39 | SNSHelloEvent: 40 | Type: AWS::SNS::Topic 41 | Properties: 42 | DisplayName: Hello Event Topic 43 | TopicName: hello-event 44 | -------------------------------------------------------------------------------- /src/functions/goodbye.ts: -------------------------------------------------------------------------------- 1 | import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; 2 | 3 | export const handler = async (event: APIGatewayProxyEvent): Promise => { 4 | try { 5 | const parsedBody = JSON.parse(event.body || ''); 6 | return { 7 | statusCode: 200, 8 | body: `Goodbye ${parsedBody?.name}`, 9 | }; 10 | } catch (err) { 11 | return { 12 | statusCode: 500, 13 | body: 'An error occured', 14 | }; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /src/functions/hello.ts: -------------------------------------------------------------------------------- 1 | import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; 2 | 3 | export const handler = async (_event: APIGatewayProxyEvent): Promise => { 4 | try { 5 | const response = { 6 | statusCode: 200, 7 | body: 'HELLO YOU ARE MY FRIEND!!!', 8 | }; 9 | return response; 10 | } catch (err) { 11 | return { 12 | statusCode: 500, 13 | body: 'An error occured', 14 | }; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /src/functions/question.ts: -------------------------------------------------------------------------------- 1 | import { SNSHandler, SNSEvent, SNSEventRecord } from 'aws-lambda'; 2 | 3 | /* 4 | SNS Topics don't expect a return value. Usually you either publish to another topic or just do some work and finish 5 | */ 6 | export const handler: SNSHandler = async (event: SNSEvent) => { 7 | const records: SNSEventRecord[] = event.Records; 8 | // For the sake of this tutorial I am just using a fire and forget with this forEach. If you want to try and catch you can use await Promise.all or something 9 | records.forEach(record => { 10 | console.log('Message is: ', record.Sns.Message); 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Basic Options */ 6 | "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 7 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 8 | "lib": ["dom", "esnext"], /* Specify library files to be included in the compilation. */ 9 | "allowJs": true, /* Allow javascript files to be compiled. */ 10 | "removeComments": true, /* Do not emit comments to output. */ 11 | /* Strict Type-Checking Options */ 12 | "strict": true, /* Enable all strict type-checking options. */ 13 | "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ 14 | /* Module Resolution Options */ 15 | "sourceMap": true, 16 | "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 17 | "baseUrl": ".", /* Base directory to resolve non-absolute module names. */ 18 | "paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 19 | "@src/*": ["src/*"] 20 | }, 21 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 22 | /* Experimental Options */ 23 | "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 24 | "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 25 | 26 | /* Advanced Options */ 27 | "skipLibCheck": true, /* Skip type checking of declaration files. */ 28 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 29 | }, 30 | "exclude": ["node_modules", "dist", "coverage"], 31 | "include": ["src"] 32 | } 33 | -------------------------------------------------------------------------------- /tsconfig.prod.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "exclude": ["src/__tests__/", "**/*.test.ts", "**/*.mock.ts"] 4 | } --------------------------------------------------------------------------------