`
sillycat
  • 浏览: 2551977 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

AMAZON and Lambda(3)Lambda with TypeScript/NodeJS

 
阅读更多
AMAZON and Lambda(3)Lambda with TypeScript/NodeJS

serverless framework
https://github.com/serverless/serverless

All examples
https://github.com/serverless/examples

Install the serverless tool
> sudo npm install serverless -g

Prepare the directory /Users/carl/work/typescript/node-typescript-lambda
Generate project from the template
> serverless create --template aws-nodejs
Serverless: Generating boilerplate...
_______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.26.1
-------'
Serverless: Successfully generated boilerplate for template: "aws-nodejs"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name

I will have 2 files, handler.js and serverless.yml

Get and Setup the Credential
click my name and select “My Security Credential” —> “Users”—> “My Name”—> “Security Credentials”
Command will write the key and security in to ~/.aws/credentials
> serverless config credentials --provider aws --key AKIAJ4Vxxxxx --secret vEEJqNMXxxxxxxx

Change the service name in the YML file
service: node-ts-lambda

Deploy the lambda to server
> serverless deploy

Useful Plugins
serverless-offline and serverless-plugin-typescript
https://github.com/serverless/examples/tree/master/aws-node-typescript-rest-api-with-dynamodb

Serverless on Local
https://www.phodal.com/blog/serverless-architecture-development-serverless-offline-localhost-debug-test/
https://www.phodal.com/blog/serverless-developement-gui-lambda-api-gateway-dynamodb-create-restful-services/

Let’s make nodeJS working first
https://www.phodal.com/blog/serverless-architecture-development-serverless-offline-localhost-debug-test/
https://www.phodal.com/blog/serverless-developement-gui-lambda-api-gateway-dynamodb-create-restful-services/

Download the example project
> serverless install -u https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb -n aws-node-rest-api-with-dynamodb

Roughly read all codes in the sample in VisualStudio Code. It is straightforward.

Try to deploy the NodeJS one
> pwd
/Users/hluo/work/aws-node-rest-api-with-dynamodb

Npm install the uuid module
> npm install

Add serverless-offline Plugin
> yarn add --dev serverless-offline
> yarn add --dev serverless-dynamodb-local

Add Plugins in serverless.yml
plugins:
- serverless-offline
- serverless-dynamodb-local

custom:
    dynamodb:
        start:
            port: 8000
            inMemory: true
            migrate: true
        migration:
            dir: offline/migrations

Set up the file
> cat offline/migrations/todos.json
{
    "Table": {
        "TableName": "serverless-rest-api-with-dynamodb-dev",
        "KeySchema": [
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            }
        ],
        "AttributeDefinitions": [
            {
                "AttributeName": "id",
                "AttributeType": "S"
            }
        ],
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        }
    }
}

Install the DB on local
> serverless dynamodb install

Start on Local
> serverless offline start
Dynamodb Local Started, Visit: http://localhost:8000/shell
Serverless: DynamoDB - created table aws-node-rest-api-with-dynamodb-dev
Serverless: Starting Offline: dev/us-east-1.
Serverless: Routes for create:
Serverless: POST /todos
Serverless: Routes for list:
Serverless: GET /todos
Serverless: Routes for get:
Serverless: GET /todos/{id}
Serverless: Routes for update:
Serverless: PUT /todos/{id}
Serverless: Routes for delete:
Serverless: DELETE /todos/{id}
Serverless: Offline listening on http://localhost:3000

Dynamodb local is here
http://localhost:8000/shell

When it runs, it throw exceptions:
{ ConfigError: Missing region in config
    at Request.VALIDATE_REGION (/Users/hluo/work/aws-node-rest-api-with-dynamodb/node_modules/aws-sdk/lib/event_listeners.js:91:45)
    at Request.callListeners (/Users/hluo/work/aws-node-rest-api-with-dynamodb/node_modules/aws-sdk/lib/sequential_executor.js:105:20)

Because the NodeJS codes are still calling the remote DynamoDB, add file and change to call local
> cat todos/dynamodb.js
'use strict';
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
let options = {};
if (process.env.IS_OFFLINE) {
    options = {
        region: 'localhost',
        endpoint: 'http://localhost:8000',
    }
}
const client = new AWS.DynamoDB.DocumentClient(options);
module.exports = client;

//const dynamoDb = new AWS.DynamoDB.DocumentClient();
const dynamoDb = require('./dynamodb');

Start the Local Server Again
> serverless offline start

Post one Data
> curl -X POST -H "Content-Type:application/json" http://localhost:3000/todos --data '{ "text": "Learn Serverless" }'
{"id":"59c70760-27c0-11e8-a4bc-2bbab7cc4318","text":"Learn Serverless","checked":false,"createdAt":1521056772821,"updatedAt":1521056772821}

Refresh the page, we can see the data then.
http://localhost:3000/todos

We can also download the TypeScript Example
> serverless install -u https://github.com/serverless/examples/tree/master/aws-node-typescript-rest-api-with-dynamodb -n aws-node-typescript-rest-api-with-dynamodb

References:
https://blog.shovonhasan.com/deploying-a-typescript-node-aws-lambda-function-with-serverless/
https://gregshackles.com/getting-started-with-serverless-and-typescript/
https://www.jamestharpe.com/serverless-typescript-getting-started/
https://serverless.com/framework/docs/getting-started/
https://github.com/serverless/serverless

old notes
http://sillycat.iteye.com/blog/2409833
http://sillycat.iteye.com/blog/2410027
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics