AMAZON and serverless(2)TypeScript

阮俊弼
2023-12-01
AMAZON and serverless(2)TypeScript

Current serverless version
> serverless --version
1.26.1

We are not using this plugin https://github.com/graphcool/serverless-plugin-typescript

Follow this example, have better understanding of TypeScript Serverless
https://www.jamestharpe.com/serverless-typescript-getting-started/
https://gregshackles.com/getting-started-with-serverless-and-typescript/
http://sillycat.iteye.com/blog/2413127

All example list here
https://github.com/serverless/examples/tree/master/

Check serverless version
> sls --version
1.26.1

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 https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates

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
handler.js.map 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
[sillycat-hluo]
aws_access_key_id = AKIxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx

In the serverless.yaml, I can configure the profile: sillycat-hluo
provider:
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:
None
endpoints:
GET - https://n0fr7youfg.execute-api.us-east-1.amazonaws.com/int/hello
functions:
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) => {
expect(error).to.be.null;
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
handler
hello
✓ should return Serverless boilerplate message
1 passing (5ms)


References:
http://sillycat.iteye.com/blog/2413259
http://sillycat.iteye.com/blog/2413127

https://github.com/graphcool/serverless-plugin-typescript
https://github.com/y13i/serverless-ts-example
https://gregshackles.com/getting-started-with-serverless-and-typescript/
https://blog.shovonhasan.com/deploying-a-typescript-node-aws-lambda-function-with-serverless/
https://www.jamestharpe.com/serverless-typescript-getting-started/
 类似资料: