AMAZON and serverless(二)TypeScript

AMAZON and serverless(2)TypeScript
AMAZON and serverless(2)TypeScript

Current serverless version
> serverless --version

We are not using this plugin

Follow this example, have better understanding of TypeScript Serverless

All example list here

Check serverless version
> sls --version

Create the Project from template
Create directory first
> mkdir sls-ts-sns

Go into that directory and create project from example
> sls create --template aws-nodejs-typescript

A list of templates are here

After creation we will install the library
> npm install

Run that from local
> sls invoke local -f hello
Serverless: Bundling with Webpack...
ts-loader: Using typescript@2.8.1 and /Users/hluo/work/typescript/sls-ts-sns/tsconfig.json
Time: 1054ms
         Asset    Size  Chunks            Chunk Names
    handler.js  2.93 kB       0  [emitted]  handler  3.18 kB       0  [emitted]  handler
   [0] ./handler.ts 338 bytes {0} [built]
    "statusCode": 200,
    "body": "{"message":"Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!","input":""}"

Check my AWS Credentials
[My Security Credentials] —> [Users] —> Find my Own Name —> [Security Credentials]
I already have a Access Key ID and security, they are already configure in ~/.aws/credentials

I think the sls config command will do the same thing
> serverless config credentials --provider aws --key AKIAJZxxxxxxxxxxx --secret P3o3fcixxxxxxxxxxx --profile sillycat-hluo

It will create a profile in ~/.aws/credential
aws_access_key_id = AKIxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx

In the serverless.yaml, I can configure the profile: sillycat-hluo
    name: aws
    runtime: nodejs6.10
    profile: sillycat-hluo

Try to deploy that under int
> sls deploy --stage int
Service Information
service: sls-ts-sns
stage: int
region: us-east-1
stack: sls-ts-sns-int
api keys:
  GET -
  hello: sls-ts-sns-int-hello

Serverless framework will do all the hard work for us.

I can see there is a Function under N.Virginia with name sls-ts-sns-int-hello
Click on that, it already have a API Gateway and CloudWatch Log there.

Directly invoke the method from console
> sls invoke -f hello -l --stage int
    "statusCode": 200,
    "body": "{"message":"Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!","input":{}}"
START RequestId: f5fd0d81-3e83-11e8-8499-17eb9202f4e2 Version: $LATEST
END RequestId: f5fd0d81-3e83-11e8-8499-17eb9202f4e2
REPORT RequestId: f5fd0d81-3e83-11e8-8499-17eb9202f4e2 Duration: 0.52 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 20 MB

-l will directly put the output on the console.

Remove that lambda
> sls remove --stage int

In my template, I already have all these
"devDependencies": {
    "@types/aws-lambda": "0.0.22",
    "@types/node": "^8.0.57",
    "serverless-webpack": "^4.0.0",
    "ts-loader": "^2.3.7",
    "typescript": "^2.5.2",
    "webpack": "^3.6.0"

Otherwise, I need use command to install them
>npm install —save-dev @types/aws-lambda

Test with Mocha and Chai
> npm install mocha @types/mocha chai @types/chai --save-dev
If there is no this package there

Package ts-node will compile automatically before run the test
> npm install ts-node --save-dev

Add the test file there handler.spec.ts
import * as mocha from 'mocha';
import * as chai from 'chai';
import { APIGatewayEvent, Handler, Callback, Context } from 'aws-lambda';
import { hello } from '../src/handler';

const expect = chai.expect;
const should = chai.should();

describe("handler", () => {
    describe("hello", () => {
        it("should return Serverless boilerplate message", () => {
            hello(null, null, (error : Error, result : any) => {
                result.body.should.equal('{"message":"Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!","input":null}');

Add script to package.json
"scripts": {
    "test": "mocha -r ts-node/register ./**/*.spec.ts"

Command to Run the Unit Test
> npm run test
> aws-nodejs-typescript@1.0.0 test /Users/hluo/work/typescript/sls-ts-sns
> mocha -r ts-node/register ./**/*.spec.ts
      ✓ should return Serverless boilerplate message
  1 passing (5ms)