【Serverless】Serverless Framework ~ offline ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/11/02/000200
https://dk521123.hatenablog.com/entry/2023/11/03/234825

の続き。

今回は、sls offline について扱う。

目次

【1】sls offline
【2】インストール
 1)確認
【3】起動の仕方
【4】Hello world
 1)修正箇所
 2)動作確認

【1】sls offline

ネット環境がなく、ローカル環境(localhost)で確認できる

【2】インストール

serverless plugin install -n serverless-offline

1)確認

* 以下が追加される

serverless.yml

plugins:
  - serverless-offline

【3】起動の仕方

sls offline start

【4】Hello world

* 前回のコードをベースにHello worldしてみる

https://dk521123.hatenablog.com/entry/2023/11/03/234825

1)修正箇所

* 以下の「★追加」を追加しただけ。後は前回と同じコード。

serverless.yml

service: hello-world

frameworkVersion: '3'

provider:
  name: aws
  # https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-runtimes.html
  runtime: python3.11
  stage: dev
  # AWS Region
  region: us-west-2
  tags:
    foo: bar
    baz: qux
functions:
  hello:
    handler: handler.hello
    # ★追加
    events:
      - httpApi:
          path: /hello
          method: get
plugins:
  - serverless-offline

2)動作確認

# 起動
$ serverless offline

Running "serverless" from node_modules

Starting Offline at stage dev (us-west-2)

Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
           * hello: hello-world-dev-hello

   ┌─────────────────────────────────────────────────────────────────────────┐
   │                                                                         │
   │   GET | http://localhost:3000/hello                                     │
   │   POST | http://localhost:3000/2015-03-31/functions/hello/invocations   │
   │                                                                         │
   └─────────────────────────────────────────────────────────────────────────┘

Server ready: http://localhost:3000 🚀


GET /hello (λ: hello)
(λ: hello) RequestId: f1a1b75d-3a1b-4a66-b59c-98bb5da85224  Duration: 172.38 ms  Billed Duration: 173 ms

動作確認

* 上記にあるURLをとりあえずブラウザで表示

http://localhost:3000/hello
出力結果

{"message": "Hello world!!!?", "input": {"body": null, "cookies": [], "headers": {"host": "localhost:3000", "connection": "keep-alive", "sec-ch-ua": "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "dnt": "1", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "sec-fetch-site": "none", "sec-fetch-mode": "navigate", "sec-fetch-user": "?1", "sec-fetch-dest": "document", "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9,ja-JP;q=0.8,ja;q=0.7"}, "isBase64Encoded": false, "pathParameters": null, "queryStringParameters": null, "rawPath": "/hello", "rawQueryString": "", "requestContext": {"accountId": "offlineContext_accountId", "apiId": "offlineContext_apiId", "authorizer": {"lambda": {}, "jwt": {}}, "domainName": "offlineContext_domainName", "domainPrefix": "offlineContext_domainPrefix", "http": {"method": "GET", "path": "/hello", "protocol": "HTTP/1.1", "sourceIp": "127.0.0.1", "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"}, "requestId": "offlineContext_resourceId", "routeKey": "GET /hello", "stage": "$default", "time": "09/Nov/2023:00:37:58 +0900", "timeEpoch": 1699457878273}, "routeKey": "GET /hello", "stageVariables": null, "version": "2.0"}}

関連記事

Serverless Framework ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/11/02/000200
Serverless Framework ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/11/03/234825