[AWS] 서버리스 프레임워크 간단한 예제(Serverless Framework Simple Example)

Serverless 프레임워크는 서버리스 애플리케이션을 생성하고 배포하기 위해 필요한 모든 도구들을 쉽게 관리할 수 있는 패키지 형태로 묶어서 제공한다.

serverless cli 를 이용한 간단한 serverless 테스트

node 와 npm 설치 생략…

1. serverless cli 설치

1
npm install -g serverless


2. 서비스/프로젝트 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
serverless
Serverless: No project detected. Do you want to create a new one? Yes
Serverless: What do you want to make? AWS Node.js
Serverless: What do you want to call this project? sample

Project successfully created in 'sample' folder.
(node:30044) Warning: a promise was created in a handler at domain.js:121:23 but was not returned from it, see http://goo at Object.ret [as lstatAsync] (eval at makeNodePromisifiedEval (C:\Users\DEV\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:8:21)

You can monitor, troubleshoot, and test your new service with a free Serverless account.

Serverless: Would you like to enable this? Yes

Serverless: What application do you want to add this to? (Use arrow keys)
Serverless: What application do you want to add this to? myapp

Your project is setup for monitoring, troubleshooting and testing

Deploy your project and monitor, troubleshoot and test it:
- Run “serverless deploy” to deploy your service.
- Run “serverless dashboard” to view the dashboard.


3. 샘플소스
handler.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

'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
),
};

// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

serverless.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
service: sample

app: myapp
org: skarlsla

provider:
name: aws
runtime: nodejs12.x

functions:
hello:
handler: handler.hello
## endpoint 추가
events:
- http:
path: hello
method: post


4. 배포(serverless deploy)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
serverless deploy -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Installing dependencies for custom CloudFormation resources...
Serverless: Safeguards Processing...
Serverless: Safeguards Results:

Summary --------------------------------------------------

warned - require-cfn-role
passed - allowed-runtimes
passed - framework-version
passed - allowed-stages
passed - allowed-regions
passed - no-unsafe-wildcard-iam-permissions
passed - no-secret-env-vars

Details --------------------------------------------------

1) Warned - no cfnRole set
details: http://slss.io/sg-require-cfn-role
Require the cfnRole option, which specifies a particular role for CloudFormation to assume while deploying.


Serverless: Safeguards Summary: 6 passed, 1 warnings, 0 errors
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service sample.zip file to S3 (68.29 KB)...
Serverless: Uploading custom CloudFormation resources...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - sample-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - ApiGatewayLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleCustomResourcesLambdaExecution
CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleCustomResourcesLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - ApiGatewayLogGroup
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - ApiGatewayLogGroup
CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - HelloLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHello
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersiong6DbuZk46pqXfxgUZxqbVYYaGuA5hOV9bb2oYYmZCQCloudFormation - CREATE_IN_PROGRESS - AWS::Logs::SubscriptionFilter - CloudWatchLogsSubscriptionFilterApiGatewayLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::SubscriptionFilter - CloudWatchLogsSubscriptionFilterApiGatewayLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHello
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersiong6DbuZk46pqXfxgUZxqbVYYaGuA5hOV9bb2oYYmZCQCloudFormation - CREATE_COMPLETE - AWS::Logs::SubscriptionFilter - CloudWatchLogsSubscriptionFilterApiGatewayLogGroup
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceHello
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - HelloLambdaVersiong6DbuZk46pqXfxgUZxqbVYYaGuA5hOV9bb2oYYmZCQ
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloPost
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloPost
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloPost
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1577175940021
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1577175940021
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1577175940021
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - CREATE_COMPLETE - AWS::IAM::Role - IamRoleCustomResourcesLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - CustomDashresourceDashapigwDashcwDashroleLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - CustomDashresourceDashapigwDashcwDashroleLambdaFunction
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - CustomDashresourceDashapigwDashcwDashroleLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - Custom::ApiGatewayAccountRole - CustomApiGatewayAccountCloudWatchRole
CloudFormation - CREATE_IN_PROGRESS - Custom::ApiGatewayAccountRole - CustomApiGatewayAccountCloudWatchRole
CloudFormation - CREATE_COMPLETE - Custom::ApiGatewayAccountRole - CustomApiGatewayAccountCloudWatchRole
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - sample-dev
CloudFormation - DELETE_SKIPPED - AWS::Lambda::Version - HelloLambdaVersionkQRfnNyzdsFOd9cvxTBAHNZoGDtQ2l6Z2hDwU58
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - sample-dev
Serverless: Stack update finished...
Service Information
service: sample
stage: dev
region: us-east-1
stack: sample-dev
resources: 18
api keys:
None
endpoints:
POST - https://bmusgn5jfh.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
hello: sample-dev-hello
layers:
None

Stack Outputs
EnterpriseLogAccessIamRole: arn:aws:iam::271382553193:role/sample-dev-EnterpriseLogAccessIamRole-ZZR7ZNGND4U4
HelloLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:271382553193:function:sample-dev-hello:2
ServiceEndpoint: https://bmusgn5jfh.execute-api.us-east-1.amazonaws.com/dev
ServerlessDeploymentBucketName: sample-dev-serverlessdeploymentbucket-1r9jccbx0b91l

Serverless: Publishing service to the Serverless Dashboard...
Serverless: Successfully published your service to the Serverless Dashboard: https://dashboard.serverless.com/tenants/skarlsla/applications/myapp/services/sample/stage/dev/region/us-east-1


5. endpoint 호출 테스트 (path: hello , method: post)

-


6. 모니터링
-


7. invoke service function 호출

1
2
3
4
5
serverless invoke -f hello -l
{
"statusCode": 200,
"body": "{\n \"message\": \"Go Serverless v1.0! Your function executed successfully!\",\n \"input\": {}\n}"
}


8. Fetch the function logs(로그 실시간보기 logtail) 서비스 호출시 로그 전송됨

1
2
3
serverless logs -f hello -t
START RequestId: 119d14e0-0450-4095-b808-e7eca56c4fa7 Version: $LATEST
2019-12-24 17:44:30.511 (+09:00) 119d14e0-0450-4095-b808-e7eca56c4fa7 INFO SERVERLESS_ENTERPRISE {"origin":"sls-agent","schemaVersion":"0.0","timestamp":"2019-12-24T08:44:30.511Z","requestId":"119d14e0-0450-4095-b808-e7eca56c4fa7","type":"transaction","payload":{"schemaType":"s-span","schemaVersion":"0.0","operationName":"s-transaction-function","startTime":"2019-12-24T08:44:30.508Z","endTime":"2019-12-24T08:44:30.511Z","duration":2.69708,"spanContext":{"traceId":"11f9f87782a45a96c;Parent=7e64f99f36e026e6;Sampled=0"},"tags":{"schemaType":"s-transaction-function","schemaVersion":"0.0","timestamp":"2019-12-24T08:44:30.508Z","tenantId":"skarlsla","applicationName":"myapp","serviceName":"sample","stageName":"dev","functionName":"sample-dev-hello","timeout":6,"computeType":"aws.lambda","computeRuntime":"aws.lambda.nodejs.12.13.0","computeRegion":"us-east-1","computeMemorySize":"1024","computeMemoryUsed":"{\"rss\":37830656 중략...


9. 서비스 삭제

1
2
3
4
5
6
7
8
9
serverless remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
.........................
Serverless: Stack removal finished...
Serverless: Publishing service to the Serverless Dashboard...
Serverless: Successfully published your service to the Serverless Dashboard: https://dashboard.serverless.com/tenants/skarlsla/applications/myapp/services/sample/stage/dev/region/us-east-1