Implement APIs fastest with Serverless Flamework

As the title says, the fastest deployment. Use .js 12.x runtime.

Prerequisites

Serverless Framework

Tools for configuring and deploying serverless applications.

Serverless: Zero-Friction Serverless Apps On AWS Lambda & Beyond.
Easily build auto-scaling, low-overhead applications on AWS Lambda, API Gateway, DynamoDB, and other managed services wi...

Prepare

$ npm install -g serverless

implementation

1. Create a project

$ serverless create --template aws-nodejs --name my-project --path my-project
...
$ cd my-project
$ ls
handler.js serverless.yml

When you create a project, two files are generated.

The contents of each file are like this.

'use strict';
module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };
  (Comment part omitted)
};
# (comments omitted)
service: my-project
# (comments omitted)
provider:
  name: aws
  runtime: nodejs12.x
# (comments omitted)
functions:
  hello:
    handler: handler.hello
# (comments omitted)

2. Implement an API that returns the simplest JSON

If the .js just returns what's written in the instructions, you can implement serverless.yml with just a little fiddle.

(omitted)
provider:
  name: aws
  runtime: nodejs12.x
+ region: ap-northeast-1
+ profile: test
(omitted)
functions:
  hello:
    handler: handler.hello
+ events:
+ - http: GET hello
(omitted)

Note: Profile for deploying to AWS. Choose from ~/.aws/creditals to use. (test this time)

For credital, see Formulas and more:

AWS CLI を設定する - AWS Command Line Interface
AWS CLI を設定し、AWS とやり取りするための設定を指定します。

3. Deploy

$ serverless deploy
(omitted)
api keys:
  None
endpoints:
  GET - https:// (random).execute-api.ap-northeast-1.amazonaws.com/dev/hello
(omitted)

Now it's com
plete. https:// you access the execute-api.ap-northeast-1.amazonaws.com/dev/hello (random). .js the contents of the handler file are returned in JSON format.

Summary

Super easy!

タイトルとURLをコピーしました