AWS Magic
develop
develop
  • Welcome
  • Lambda
    • Local Lambda Development
      • Prerequisites
      • Starter Kit or Manual?
      • Use Starter Kit
      • Manual Configuration
        • Environment Variables
        • Python Setup
        • Node Setup
        • Source & Config Files
        • Test The Configuration
      • Run Lambda in VSCode
      • Debug Lambda in VSCode
      • Deploy Lambda Function
    • Lambda & API GW
  • TypeScript
    • Promises
Powered by GitBook
On this page
  • SAM Configuration
  • Node and TypeScript Configuration
  • TypeScript Configuration
  • TypeScript Linting
  • Node Project Configuration
  • Visual Studio Code Configuration
  • Create Lambda Function

Was this helpful?

  1. Lambda
  2. Local Lambda Development
  3. Manual Configuration

Source & Config Files

Now, let's create configuration files.

SAM Configuration

Switch to function folder first.

$ cd $WORKSPACE/$FUNCTIONNAME/

Create template.yaml under this folder with the content below:

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A starter AWS Lambda function.
Parameters: 
    IdentityNameParameter: 
      Type: String
Resources:
  function-one:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: dist/index.handler
      Runtime: nodejs8.10
      CodeUri: .
      Description: A starter AWS Lambda function.
      MemorySize: 128
      Timeout: 3
      Environment:
        Variables:
          Stage: DEV

Node and TypeScript Configuration

Under the function folder we will create a few files.

$ cd $WORKSPACE/$FUNCTIONNAME/

TypeScript Configuration

Create tsconfig.json with the content below:

tsconfig.json
{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "node"
    ],
    "esModuleInterop": true
  }
}

TypeScript Linting

Create tslint.json file with the content below:

tslint.json
{
  "defaultSeverity": "error",
  "extends": [
    "tslint:recommended"
  ],
  "jsRules": {
    "no-unused-expression": true,
    "noImplicitAny": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "preserveConstEnums": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "rules": {
    "quotemark": [
      true,
      "single"
    ],
    "member-access": [
      false
    ],
    "ordered-imports": [
      false
    ],
    "max-line-length": [
      true,
      150
    ],
    "member-ordering": [
      false
    ],
    "interface-name": [
      false
    ],
    "arrow-parens": false,
    "object-literal-sort-keys": false,
    "trailing-comma": [
      true,
      {
        "singleline": "never",
        "multiline": {
          "objects": "ignore",
          "arrays": "ignore",
          "functions": "never",
          "typeLiterals": "ignore"
        }
      }
    ],
    "rulesDirectory": []
  }
}

Node Project Configuration

Edit package.json to make it look like below:

package.json
{
  "name": "function-one",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc",
    "sam": "node_modules/aws-sam-local/node_modules/.bin/sam local invoke -e src/event.json function-one",
    "debug": "node_modules/aws-sam-local/node_modules/.bin/sam local invoke -e src/event.json --debug-port 9999 function-one"
  },
  "repository": {
    "type": "git",
    "url": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/workspace"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^12.7.3",
    "aws-sam-local": "^0.2.11"
  }
}

Visual Studio Code Configuration

Under workspace folder, create .vscode folder. Visual Studio Code configuration files are kept here.

$ mkdir $WORKSPACE/.vscode
$ cd $WORKSPACE/.vscode

Create launch.json with the content below:

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Lambda",
      "type": "node",
      "request": "attach",
      "sourceMaps": true,
      "address": "localhost",
      "port": 9999,
      "localRoot": "${workspaceRoot}/function-one",
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "stopOnEntry": false,
      "outFiles": [
        "${workspaceFolder}/function-one/dist/**/*.js"
      ]
    }
  ]
}

Create settings.json with the content below:

settings.json
{
  "files.autoSave": "afterDelay",
  "files.autoSaveDelay": 10000,
  "editor.tabSize": 2,
  "editor.detectIndentation": false,
  "files.exclude": {
    "**/*.js": true,
    "**/*.js.map": true,
    "**/node_modules": true,
    "dist": true
  },
  "explorer.autoReveal": true
}

Create tasks.json with the content below;

tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "script": "build",
      "problemMatcher": []
    },
    {
      "type": "npm",
      "script": "sam",
      "problemMatcher": []
    }
  ]
}

Create Lambda Function

We will create src folder first.

$ mkdir $WORKSPACE/$FUNCTIONNAME/src
$ cd $WORKSPACE/$FUNCTIONNAME/src

Create index.ts with the content below:

index.ts
export const handler = async (event: any = {}, content: any = {}): Promise<any> => {
  console.log('value1 =', event.key1);
  console.log('value2 =', event.key2);
  console.log('value3 =', event.key3);
  console.log(process.env.Stage);
  const response = JSON.stringify(event, null, 2);
  return response;
}

Create event.json with the content below:

event.json
{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}
PreviousNode SetupNextTest The Configuration

Last updated 5 years ago

Was this helpful?