如何用serverless創建aws-lambda

 

https://serverless.com/framework/docs/providers/aws/guide/quick-start/

安裝serverless

$npm install -g serverless

$mkdir serverless-demo

$cd serverless-demo/

$serverless create -t aws-nodejs

從aws裏找到"My Security Credentials'

https://serverless.com/framework/docs/providers/aws/guide/credentials/#create-an-iam-user-and-access-key

 

Add credential to your local laptop:

$serverless config credentials --provider aws --key AKIAIDEXAZA --secret xxxEL5xFZYxxx49nuu

$cat ~/.aws/credentials 

接下來打開demo文件,編輯serverless.yml:

service: lambda-test
provider:
  name: aws
  runtime: nodejs10.x
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: users/create
          method: get
  imageResize:
    handler: handler.imageResize
    events:
      - http:
          path: /imageResize
          method: get

編輯handel.js

'use strict';

module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'This is v1.0',
      },
      null,
      2
    ),
  };
};

module.exports.imageResize = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'resized your image',
      },
      null,
      2
    ),
  };
};

保存修改之後, 發佈到測試環境

$serverless deploy

Serverless: Packaging service...

Serverless: Excluding development dependencies...

Serverless: Uploading CloudFormation file to S3...

Serverless: Uploading artifacts...

Serverless: Uploading service lambda-test.zip file to S3 (417 B)...

Serverless: Validating template...

Serverless: Updating Stack...

Serverless: Checking Stack update progress...

................................

Serverless: Stack update finished...

Service Information

service: lambda-test

stage: dev

region: us-east-1

stack: lambda-test-dev

resources: 17

api keys:

  None

endpoints:

  GET - https://xxx.execute-api.us-east-1.amazonaws.com/dev/users/create

  GET - https://xxx.execute-api.us-east-1.amazonaws.com/dev/imageResize

functions:

  hello: lambda-test-dev-hello

  imageResize: lambda-test-dev-imageResize

layers:

  None

Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

 

發佈到正式環境的命令:

$ serverless deploy --stage production

Serverless: Packaging service...

Serverless: Excluding development dependencies...

Serverless: Creating Stack...

Serverless: Checking Stack create progress...

.....

Serverless: Stack create finished...

Serverless: Uploading CloudFormation file to S3...

Serverless: Uploading artifacts...

Serverless: Uploading service lambda-test.zip file to S3 (417 B)...

Serverless: Validating template...

Serverless: Updating Stack...

Serverless: Checking Stack update progress...

...................................................

Serverless: Stack update finished...

Service Information

service: lambda-test

stage: production

region: us-east-1

stack: lambda-test-production

resources: 17

api keys:

  None

endpoints:

  GET - https://xxx.execute-api.us-east-1.amazonaws.com/production/users/create

  GET - https://xxx.execute-api.us-east-1.amazonaws.com/production/imageResize

functions:

  hello: lambda-test-production-hello

  imageResize: lambda-test-production-imageResize

layers:

  None

Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章