목차
$ npm install -g serverless
- https://console.aws.amazon.com/iam 접속
- Users 탭 이동 - Add User 버튼 클릭
- User name을 입력하고 Programmatic access
를 체크한 후 Next: permissions를 클릭
- Create group버튼을 누르고 AdministratorAccess를 찾아 체크한 후 그룹을 생성
- 방금 생성한 Group을 체크하고 Next: Review누르고 계속해서 Create User버튼을 누른다
- Access key ID와 Secret access key를 저장해놓거나 Download.csv
파일을 다운받아 꼭 안전하게 저장해놓는다. (절대 잃어버리면 안 됨)
- 위에서 발급받은 사용자 인증키를 Serverless에 등록한다.
$ serverless config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "default" profile.
- 함수의 런타임(템플릿)은 Node.js로 지정
- 서비스 이름(ServiceName)을 지정
$ serverless create --template aws-nodejs --path [ServiceName]
- handler.js는 서비스가 실행 할 함수를 포함
- serverless.yml은 서비스의 모든 설정을 포함
seo$ ls -al
total 24
drwxr-xr-x 5 seo staff 170 11 3 01:56 .
drwxr-xr-x 17 seo staff 578 11 3 01:56 ..
-rw-r--r-- 1 seo staff 86 11 3 01:56 .gitignore
-rw-r--r-- 1 seo staff 466 11 3 01:56 handler.js
-rw-r--r-- 1 seo staff 2819 11 3 01:56 serverless.yml
seo$
funtions는 서비스가 포함하는 모든 함수
# serverless.yml
service: ServerlessTest
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.hello
- -v
옵션으로 로그가 쭈욱 찍힌다.
- 배포가 되면서 API Gateway의 endpoint를 생성해준다.
https://h7k8czu0lh.execute-api.us-east-1.amazonaws.com/dev/users/create
seo$ serverless deploy -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - ServerlessTest-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - ServerlessTest-dev
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (409 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - ServerlessTest-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - HelloLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - HelloLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - HelloLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUsers
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUsers
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceUsers
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUsersCreate
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUsersCreate
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceUsersCreate
CloudFormation - CREATE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - HelloLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersionPSzzisjnTvvYknuXwQOlAvdkQZ67qXYSvgoAi9T8W0
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodUsersCreateGet
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersionPSzzisjnTvvYknuXwQOlAvdkQZ67qXYSvgoAi9T8W0
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodUsersCreateGet
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - HelloLambdaVersionPSzzisjnTvvYknuXwQOlAvdkQZ67qXYSvgoAi9T8W0
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodUsersCreateGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1509642848897
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1509642848897
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1509642848897
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - HelloLambdaPermissionApiGateway
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - ServerlessTest-dev
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - ServerlessTest-dev
Serverless: Stack update finished...
Service Information
service: ServerlessTest
stage: dev
region: us-east-1
stack: ServerlessTest-dev
api keys:
None
endpoints:
GET - https://h7k8czu0lh.execute-api.us-east-1.amazonaws.com/dev/users/create
functions:
hello: ServerlessTest-dev-hello
Stack Outputs
HelloLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:513755243845:function:ServerlessTest-dev-hello:1
ServiceEndpoint: https://h7k8czu0lh.execute-api.us-east-1.amazonaws.com/dev
ServerlessDeploymentBucketName: serverlesstest-dev-serverlessdeploymentbucket-1o2n2n2jbd7oj
seo$
$ serverless invoke -f hello -l
$ npm init
$ npm install serverless-offline --save-dev
# serverless.yml
service: ServerlessTest
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.hello
events:
- http:
path: hello/get
method: get
# adding these two lines
plugins:
- serverless-offline
- 브라우저에서 http://localhost:3000/hello/get 을 접근해보면 응답을 확인할 수 있다.
seo$ serverless offline start
Serverless: Starting Offline: dev/us-east-1.
Serverless: Routes for hello:
Serverless: GET /hello/get
Serverless: Offline listening on http://localhost:3000
- 테스트가 끝나면 AWS에 Deploy한다.
[AWS] Lambda와 API Gateway로 API 서비스 하기 (0) | 2017.11.01 |
---|---|
[AWS] Node.js로 DynamoDB사용하기 (3) - DynamoDB 테이블/데이터 다루기 (2) | 2017.10.27 |
[AWS] Node.js로 DynamoDB사용하기 (2) - AWS SDK로 개발하기 (0) | 2017.10.27 |
[AWS] Node.js로 DynamoDB사용하기 (1) - DynamoDB 다운로드 (0) | 2017.10.27 |