Serverless Framework로 간편한 서버리스(기본편)
13782 단어 람다ServerlessFrameworkCloudWatchAWS
문제
해결책
Serverless Framework 를 사용하자!
할 수 있는 일
많이 있지만, 다음과 같은 것을 실현할 수 있습니다.
전제
Serverless Framework는 Node에서 작동하므로 다음 도구를 설치하십시오.
설치 설정
npm install -g serverless
내 곳에서는 이런 느낌입니다.
( sls
는 serverless
의 별칭입니다.)
$ node -v
v8.11.4
$ npm -v
5.6.0
$ sls --version
1.27.3
$ aws --version
aws-cli/1.16.1 Python/3.7.0 Darwin/18.0.0 botocore/1.11.1
설치가 끝나면 AWS 액세스 키를 설정합니다.
(이미 설정되어 있으면 통과해도 문제 없습니다)
sls config credentials --provider aws --key AWSACCESSKEY --secret AWSSECRETACCESSKEY
정의 파일
Serverless Framework에서는 배포할 각 서비스에 대해 정의 파일 serverless.yml
를 만들어야 합니다.
간단한 샘플
다음 파일을 Lambda에서 간편한 서버리스 입문(Lambda 이벤트 from API Gateway편)로 만든 디렉토리에 넣습니다.
serverless.ymlservice: qiita-lambda-apigateway
provider:
name: aws
profile: ${opt:profile, 'default'}
stage: prod
region: ap-northeast-1
runtime: python3.6
role: arn:aws:iam::ACCOUNT_ID:role/service-role/YOUR-ROLE-NAME # 適切なロール名に置き換えてください!
timeout: 30
memorySize: 128
versionFunctions: false
resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:service}-${self:provider.stage}
package:
individually: true
custom:
prefix: ${self:service}-${self:provider.stage}
functions:
lambda_function:
name: ${self:custom.prefix}-lambda_function
handler: lambda_function.lambda_handler
events:
- http:
integration: lambda-proxy
path: hello-world
method: GET
cors: true
- http:
integration: lambda-proxy
path: hello-world/{id}
method: GET
cors: true
- http:
integration: lambda-proxy
path: hello-world
method: POST
cors: true
무엇을 하는지 간단히 해설
npm install -g serverless
$ node -v
v8.11.4
$ npm -v
5.6.0
$ sls --version
1.27.3
$ aws --version
aws-cli/1.16.1 Python/3.7.0 Darwin/18.0.0 botocore/1.11.1
sls config credentials --provider aws --key AWSACCESSKEY --secret AWSSECRETACCESSKEY
Serverless Framework에서는 배포할 각 서비스에 대해 정의 파일
serverless.yml
를 만들어야 합니다.간단한 샘플
다음 파일을 Lambda에서 간편한 서버리스 입문(Lambda 이벤트 from API Gateway편)로 만든 디렉토리에 넣습니다.
serverless.yml
service: qiita-lambda-apigateway
provider:
name: aws
profile: ${opt:profile, 'default'}
stage: prod
region: ap-northeast-1
runtime: python3.6
role: arn:aws:iam::ACCOUNT_ID:role/service-role/YOUR-ROLE-NAME # 適切なロール名に置き換えてください!
timeout: 30
memorySize: 128
versionFunctions: false
resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:service}-${self:provider.stage}
package:
individually: true
custom:
prefix: ${self:service}-${self:provider.stage}
functions:
lambda_function:
name: ${self:custom.prefix}-lambda_function
handler: lambda_function.lambda_handler
events:
- http:
integration: lambda-proxy
path: hello-world
method: GET
cors: true
- http:
integration: lambda-proxy
path: hello-world/{id}
method: GET
cors: true
- http:
integration: lambda-proxy
path: hello-world
method: POST
cors: true
무엇을 하는지 간단히 해설
qiita-lambda-apigateway
lambda_function
정의lambda_function.lambda_handler
즉시 배포
터미널을 열고 serverless.yml이 있는 디렉토리에서 다음 명령을 실행하십시오.
serverless deploy
# serverless deploy --verbose # 詳細を見たいとき
이러한 느낌으로 로그가 출력됩니다.
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
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 - qiita-lambda-apigateway-prod
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (6.73 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
Serverless: Stack update finished...
Service Information
service: qiita-lambda-apigateway
stage: prod
region: ap-northeast-1
stack: qiita-lambda-apigateway-prod
api keys:
None
endpoints:
GET - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world
GET - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world/{id}
POST - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world
functions:
lambda_function: qiita-lambda-apigateway-prod-lambda_function
Stack Outputs
ServiceEndpoint: https://**********.execute-api.ap-northeast-1.amazonaws.com/prod
ServerlessDeploymentBucketName: qiita-lambda-apigateway-serverlessdeploymentbuck-**********
자세히 보면 CloudFormation이 움직이는 것 같습니다.
… 네, Serverless Framework는 그 악마처럼 까다로운 CloudFormation 템플릿을 만들어 실행하는 도구입니다.
확인
각각 콘솔을 들여다 봅시다.
람다
빨간색 테두리로 둘러싸인 곳에 CloudFormation 스택에 속한 것으로 표시됩니다.
API 게이트웨이
3개 설정한 엔드포인트와 CORS용 OPTIONS, 총 5개가 등록되어 있습니다.
또한 이 엔드포인트는 ↑의 Lambda를 호출하도록 설정되어 있습니다.
CloudWatch Logs
로그 그룹도 자동으로 만들어집니다.
아무것도 실행하지 않으므로 스트림이 비어 있습니다.
요약
수동 설정과는 안녕하세요!
전직의 신인 시절, 제품의 배포를 맡았을 때는, 수동 배포 밖에 방법이 없었습니다만(지금이라면 좀 더 생각합니다…), 잘 설정을 잊거나 파일을 두어 잊거나 해, 시장 문제 라고 해서 꾸짖은 것이 많이…
그런 곳에서 신경을 덜어주는 것보다, 사용할 수 있는 자동화 툴은 점점 활용해 버립시다!
Reference
이 문제에 관하여(Serverless Framework로 간편한 서버리스(기본편)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kakken1988/items/ed528aa73fecd34238ec
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
serverless deploy
# serverless deploy --verbose # 詳細を見たいとき
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
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 - qiita-lambda-apigateway-prod
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (6.73 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - LambdaUnderscorefunctionLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworld
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - LambdaUnderscorefunctionLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceHelloDashworldIdVar
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldGet
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldPost
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarGet
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodHelloDashworldIdVarOptions
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1539166300519
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - LambdaUnderscorefunctionLambdaPermissionApiGateway
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - qiita-lambda-apigateway-prod
Serverless: Stack update finished...
Service Information
service: qiita-lambda-apigateway
stage: prod
region: ap-northeast-1
stack: qiita-lambda-apigateway-prod
api keys:
None
endpoints:
GET - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world
GET - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world/{id}
POST - https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/hello-world
functions:
lambda_function: qiita-lambda-apigateway-prod-lambda_function
Stack Outputs
ServiceEndpoint: https://**********.execute-api.ap-northeast-1.amazonaws.com/prod
ServerlessDeploymentBucketName: qiita-lambda-apigateway-serverlessdeploymentbuck-**********
각각 콘솔을 들여다 봅시다.
람다
빨간색 테두리로 둘러싸인 곳에 CloudFormation 스택에 속한 것으로 표시됩니다.
API 게이트웨이
3개 설정한 엔드포인트와 CORS용 OPTIONS, 총 5개가 등록되어 있습니다.
또한 이 엔드포인트는 ↑의 Lambda를 호출하도록 설정되어 있습니다.
CloudWatch Logs
로그 그룹도 자동으로 만들어집니다.
아무것도 실행하지 않으므로 스트림이 비어 있습니다.
요약
수동 설정과는 안녕하세요!
전직의 신인 시절, 제품의 배포를 맡았을 때는, 수동 배포 밖에 방법이 없었습니다만(지금이라면 좀 더 생각합니다…), 잘 설정을 잊거나 파일을 두어 잊거나 해, 시장 문제 라고 해서 꾸짖은 것이 많이…
그런 곳에서 신경을 덜어주는 것보다, 사용할 수 있는 자동화 툴은 점점 활용해 버립시다!
Reference
이 문제에 관하여(Serverless Framework로 간편한 서버리스(기본편)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kakken1988/items/ed528aa73fecd34238ec
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Serverless Framework로 간편한 서버리스(기본편)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kakken1988/items/ed528aa73fecd34238ec텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)