目次

serverless

since 2022-08-31

chalice zappa と比べてみたい。

https://docs.serverless.com/

https://www.serverless.com/framework/docs/getting-started

https://qiita.com/suzuki-navi/items/b5f513f21d37365c248f

Python

https://ja.nishimotz.com/n

$ sudo n lts

installed : v16.17.0 (with npm 8.15.0)

$ sudo npm install -g serverless

$ mkdir serverless-aws-python3
$ cd serverless-aws-python3/

$ serverless create --template aws-python3

$ ls -1 -a
.
..
.gitignore
handler.py
serverless.yml

$ serverless plugin install -n serverless-python-requirements

プラグインの説明

下記を書きかえて runtime の Python バージョンを 3.8 から 3.9 にする。region も変更。

# serverless.yml

provider:
  name: aws
  runtime: python3.9
  stage: dev
  region: ap-northeast-1

デプロイする。

$ serverless deploy

invoke してみる。

$ serverless invoke -f hello --log
Running "serverless" from node_modules
{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
--------------------------------------------------------------------
START
END Duration: 1.18 ms (init: 112.78 ms) Memory Used: 36 MB

AWS コンソールの Lambda でみると以下ができている。

なお serverless = sls コマンド。

HTTP API

あらためて create コマンドを確認する。

https://www.serverless.com/framework/docs/providers/aws/cli-reference/create

https://www.serverless.com/examples を探す

https://www.serverless.com/examples/aws-python-http-api

こういうほうがよかったか。。

https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint

serverless.yml を更新する。

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get

sls deploy すると endpoint URL が表示される

$ sls deploy
Running "serverless" from node_modules

Deploying serverless-aws-python3 to stage dev (ap-northeast-1)

✔ Service deployed to stack serverless-aws-python3-dev (42s)

endpoint: GET - https://******************.execute-api.ap-northeast-1.amazonaws.com/hello
functions:
  hello: serverless-aws-python3-dev-hello (373 B)

Improve API performance – monitor it with the Serverless Dashboard: run "serverless"

sls info でも再表示できる。

httpie でアクセスしてみる。

$ http https://************.execute-api.ap-northeast-1.amazonaws.com/hello
HTTP/1.1 200 OK
Apigw-Requestid: ************
Connection: keep-alive
Content-Length: 979
Content-Type: text/plain; charset=utf-8
Date: Wed, 31 Aug 2022 00:56:29 GMT

{
    "input": {

中略

        "routeKey": "GET /hello",
        "version": "2.0"
    },
    "message": "Go Serverless v1.0! Your function executed successfully!"
}

コメントを削るとこんなコードになる

$ cat serverless.yml
service: serverless-aws-python3

frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.9
  stage: dev
  region: ap-northeast-1

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get
plugins:
  - serverless-python-requirements
$ cat handler.py
import json
 
 
def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        # "input": event
    }
 
    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }
 
    return response

nodejs

nodejs もやってみよう

$ mkdir serverless-aws-nodejs
$ cd serverless-aws-nodejs/

$ sls create --template aws-nodejs

ちなみに serverless.yml の path と method のところだけインデントが4つなのは、そうしないとエラーになるから、らしい。。

https://stackoverflow.com/questions/64074827/serverless-configuration-warning-at-functions-app-events0-unsupported-funct

無事にデプロイできて動く

$ http https://************.execute-api.ap-northeast-1.amazonaws.com/hello
HTTP/1.1 200 OK
Apigw-Requestid: ***********
Connection: keep-alive
Content-Length: 75
Content-Type: text/plain; charset=utf-8
Date: Wed, 31 Aug 2022 01:22:32 GMT

{
    "message": "Go Serverless v1.0! Your function executed successfully!"
}
$ cat serverless.yml
service: serverless-aws-nodejs

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs16.x
  stage: dev
  region: ap-northeast-1

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get
$ cat handler.js
'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
    ),
  };
};

serverless express

下記が新しいという話

https://github.com/vendia/serverless-express

下記の記事のとおりにやってみた。

https://zenn.dev/yuta_saito/articles/8b543a1957c375593ee5

$ npm init
$ npm i express --save
$ npm i @vendia/serverless-express
$ ls -1
app.js
lambda.js
local-app.js
node_modules
package-lock.json
package.json
routes
serverless.yml

$ ls -1 routes
router.js

serverless.yml は警告が出ないように直してみた

service: serverless-express-test

provider:
  name: aws
  runtime: nodejs16.x
  region: ap-northeast-1

package:
  patterns:
    - '!.git/**'
    - '!test/**'
    - '!README.md'
    - '!local-app.js'

functions:
  serverlessTest:
    handler: lambda.handler
    events:
      - http: ANY /
      - http: 'ANY /{proxy+}'

stage を省略すると dev になる。

$ curl https://************.execute-api.ap-northeast-1.amazonaws.com/dev
{"message":"Hello World!"}

$ curl https://************.execute-api.ap-northeast-1.amazonaws.com/dev/users/
[{"name":"Taro"},{"name":"Hanako"}]
$ sls info
Running "serverless" from node_modules
service: serverless-express-test
stage: dev
region: ap-northeast-1
stack: serverless-express-test-dev
endpoints:
  ANY - https://************.execute-api.ap-northeast-1.amazonaws.com/dev
  ANY - https://************.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:
  serverlessTest: serverless-express-test-dev-serverlessTest

events

domain manager

カスタムドメイン設定にこれが使えそう

https://github.com/amplify-education/serverless-domain-manager

https://qiita.com/sugurutakahashi12345/items/c4e0cf5708a792703b9d

https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/how-to-custom-domains.html

前述の express の続き

$ npm install serverless-domain-manager --save-dev

最初にやったこと

やり直した手順

やり直すときの本来の手順

どうなったか

再び情報収集

serverless + express + Lambda Function URLs

since 2022-09-07

カスタムドメインが不要であれば API Gateway さえ使わない方法がある。

functions:
  handler:
    handler: lambda.handler
    url: true
$ sls deploy
Running "serverless" from node_modules

Deploying serverless-express-test to stage dev (ap-northeast-1)

✔ Service deployed to stack serverless-express-test-dev (55s)

endpoint: https://***********************************.lambda-url.ap-northeast-1.on.aws/
functions:
  handler: serverless-express-test-dev-handler (838 kB)

Want to ditch CloudWatch? Try our new console: run "serverless --console"

console と cloud

ここからは serverless 社のサービス。アカウントを作る話になりそう。

https://www.serverless.com/console/docs

https://www.serverless.com/cloud/docs