serverless.yml, serverless.ts

오픈소스·2022년 12월 11일
0
post-thumbnail
post-custom-banner

서버리스 이제는 AWS Lambda로 간다

serverless.ts

$ npx serverless create --template aws-nodejs-typescript --name hello-api-ts --path hello-api-ts

✔ Project successfully created in "hello-api-ts" from "aws-nodejs-typescript" template (3s)

serveless.ts 화일에 region: 'ap-northeast-2', 추가하여 서울 region에 생성하도록 한다.

  ...
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    region: 'ap-northeast-2',
    ...
  },
  ...
$ sls deploy

Deploying hello-api-ts to stage dev (ap-northeast-2)

✔ Service deployed to stack hello-api-ts-dev (117s)

endpoint: POST - https://texukw6j2j.execute-api.ap-northeast-2.amazonaws.com/dev/hello
functions:
  hello: hello-api-ts-dev-hello (13 kB)

Need a better logging experience than CloudWatch? Try our Dev Mode in console: run "serverless --console"
$ curl -X POST -H "Content-Type: application/json" -d '{"name":"lacti"}' https://texukw6j2j.execute-api.ap-northeast-2.amazonaws.com/dev/hello

수행 시간 로그를 보려고, 2번 실행

{
    "message": "Hello lacti, welcome to the exciting Serverless world!",
    "event": {
        "resource": "/hello",
        "path": "/hello",
        "httpMethod": "POST",
        "headers": {
            "Accept": "*/*",
            "CloudFront-Forwarded-Proto": "https",
            "CloudFront-Is-Desktop-Viewer": "true",
            "CloudFront-Is-Mobile-Viewer": "false",
            "CloudFront-Is-SmartTV-Viewer": "false",
            "CloudFront-Is-Tablet-Viewer": "false",
            "CloudFront-Viewer-ASN": "17858",
            "CloudFront-Viewer-Country": "KR",
            "content-type": "application/json",
            "Host": "texukw6j2j.execute-api.ap-northeast-2.amazonaws.com",
            "User-Agent": "curl/7.78.0",
            "Via": "2.0 eea072e601807a25909bbea64d927560.cloudfront.net (CloudFront)",
            "X-Amz-Cf-Id": "DHdSAHQbm-LE13uVWiV9EwZyWrsLbIhcsOFgA8laLBfXxl8NYB9cYA==",
            "X-Amzn-Trace-Id": "Root=1-6395bf9b-46b6a4a2416405b93f43da6c",
            "X-Forwarded-For": "112.151.155.62, 130.176.126.71",
            "X-Forwarded-Port": "443",
            "X-Forwarded-Proto": "https"
        },
        "multiValueHeaders": {
            "Accept": [
                "*/*"
            ],
            "CloudFront-Forwarded-Proto": [
                "https"
            ],
            "CloudFront-Is-Desktop-Viewer": [
                "true"
            ],
            "CloudFront-Is-Mobile-Viewer": [
                "false"
            ],
            "CloudFront-Is-SmartTV-Viewer": [
                "false"
            ],
            "CloudFront-Is-Tablet-Viewer": [
                "false"
            ],
            "CloudFront-Viewer-ASN": [
                "17858"
            ],
            "CloudFront-Viewer-Country": [
                "KR"
            ],
            "content-type": [
                "application/json"
            ],
            "Host": [
                "texukw6j2j.execute-api.ap-northeast-2.amazonaws.com"
            ],
            "User-Agent": [
                "curl/7.78.0"
            ],
            "Via": [
                "2.0 eea072e601807a25909bbea64d927560.cloudfront.net (CloudFront)"
            ],
            "X-Amz-Cf-Id": [
                "DHdSAHQbm-LE13uVWiV9EwZyWrsLbIhcsOFgA8laLBfXxl8NYB9cYA=="
            ],
            "X-Amzn-Trace-Id": [
                "Root=1-6395bf9b-46b6a4a2416405b93f43da6c"
            ],
            "X-Forwarded-For": [
                "112.151.155.62, 130.176.126.71"
            ],
            "X-Forwarded-Port": [
                "443"
            ],
            "X-Forwarded-Proto": [
                "https"
            ]
        },
        "queryStringParameters": null,
        "multiValueQueryStringParameters": null,
        "pathParameters": null,
        "stageVariables": null,
        "requestContext": {
            "resourceId": "wscjhk",
            "resourcePath": "/hello",
            "httpMethod": "POST",
            "extendedRequestId": "c-rgWHbqIE0FTyQ=",
            "requestTime": "11/Dec/2022:11:31:39 +0000",
            "path": "/dev/hello",
            "accountId": "ACCOUNT_ID",
            "protocol": "HTTP/1.1",
            "stage": "dev",
            "domainPrefix": "texukw6j2j",
            "requestTimeEpoch": 1670758299613,
            "requestId": "3a2e47c3-9f98-4f05-a27d-709ddd99129c",
            "identity": {
                "cognitoIdentityPoolId": null,
                "accountId": null,
                "cognitoIdentityId": null,
                "caller": null,
                "sourceIp": "112.151.155.62",
                "principalOrgId": null,
                "accessKey": null,
                "cognitoAuthenticationType": null,
                "cognitoAuthenticationProvider": null,
                "userArn": null,
                "userAgent": "curl/7.78.0",
                "user": null
            },
            "domainName": "texukw6j2j.execute-api.ap-northeast-2.amazonaws.com",
            "apiId": "texukw6j2j"
        },
        "body": {
            "name": "lacti"
        },
        "isBase64Encoded": false,
        "rawBody": "{\"name\":\"lacti\"}"
    }
}
$ sls logs -f hello
START
END Duration: 4.80 ms (init: 194.67 ms) Memory Used: 57 MB
START
END Duration: 6.12 ms Memory Used: 59 MB

로그가 즉시 업데이트 되지 않을 수 있음.

$ sls remove
Removing hello-api-ts from stage dev (ap-northeast-2)

✔ Service hello-api-ts has been successfully removed (31s)

책의 2-2-3 웹팩 적용 처럼 aws-nodejs-typescript template에 webpack을 추가해 보려고 했으나,
aws-nodejs-typescript template에는 이미 esbuild bundler가 들어 있었다.
그래서 방향을 바꾸어 aws-nodejs template과 두 서비리스 서비스를 비교해 보려고 한다.

bundler의 적용 여부는
$ sls package 명렁어로 생성되는 .serverless/hello.zip를 unzip해 보면, node_modules/ 폴더 유무로 알 수 있다.


serverless.yml

$ npx serverless create --template aws-nodejs --name hello-world --path hello-world

✔ Project successfully created in "hello-world" from "aws-nodejs" template (2s)

serveless.yml 화일에서 다음을 주석해제하고, 수정한다.

...
  stage: dev
  region: ap-northeast-2
...
    events:
      - httpApi:
          path: /hello
          method: get
...
$ npx serverless deploy

Deploying hello-world to stage dev (ap-northeast-2)

✔ Service deployed to stack hello-world-dev (112s)

endpoint: GET - https://jwntcfa27d.execute-api.ap-northeast-2.amazonaws.com/hello
functions:
  hello: hello-world-dev-hello (155 kB)

Improve API performance – monitor it with the Serverless Console: run "serverless --console"
$ curl https://jwntcfa27d.execute-api.ap-northeast-2.amazonaws.com/hello

수행 시간 로그를 보려고, 2번 실행

{
  "message": "Go Serverless v1.0! Your function executed successfully!",
  "input": {
    "version": "2.0",
    "routeKey": "GET /hello",
    "rawPath": "/hello",
    "rawQueryString": "",
    "headers": {
      "accept": "*/*",
      "content-length": "0",
      "host": "jwntcfa27d.execute-api.ap-northeast-2.amazonaws.com",
      "user-agent": "curl/7.78.0",
      "x-amzn-trace-id": "Root=1-6395db2e-328950537e60c3840d351548",
      "x-forwarded-for": "112.151.155.62",
      "x-forwarded-port": "443",
      "x-forwarded-proto": "https"
    },
    "requestContext": {
      "accountId": "ACCOUNT_ID",
      "apiId": "jwntcfa27d",
      "domainName": "jwntcfa27d.execute-api.ap-northeast-2.amazonaws.com",
      "domainPrefix": "jwntcfa27d",
      "http": {
        "method": "GET",
        "path": "/hello",
        "protocol": "HTTP/1.1",
        "sourceIp": "112.151.155.62",
        "userAgent": "curl/7.78.0"
      },
      "requestId": "c-8vTj2goE0EPUg=",
      "routeKey": "GET /hello",
      "stage": "$default",
      "time": "11/Dec/2022:13:29:18 +0000",
      "timeEpoch": 1670765358589
    },
    "isBase64Encoded": false
  }
}%
$ npx serverless logs -f hello
START
END Duration: 2.63 ms (init: 156.31 ms) Memory Used: 55 MB
START
END Duration: 2.40 ms Memory Used: 56 MB

npx serverless remove
Removing hello-world from stage dev (ap-northeast-2)

✔ Service hello-world has been successfully removed (26s)
post-custom-banner

0개의 댓글