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:
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:
{
"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:
{
"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:
{
"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:
{
"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:
{
"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;
{
"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:
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:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}