곰돌푸우❤️

목차

    [AWS] Serverless Framework로 쉽게 서비스 개발 및 배포하기

     

    Serverless를 글로벌로 설치한다.

    $ npm install -g serverless

     

    IAM User를 생성한다.

    - 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 초기화

    $ npm init

     

    Serverless Offline 설치

    $ npm install serverless-offline --save-dev

     

    serverless.yml파일 하단에 두줄을 추가 한다.

    # 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

     

    로컬에서 Lambda + API Gateway 대기상태 만들기

    - 브라우저에서 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한다.

     

     

    [참고]
    https://medium.com/@jwyeom63/%EB%B9%A0%EB%A5%B4%EA%B2%8C-%EB%B0%B0%EC%9B%8C%EB%B3%B4%EB%8A%94-node-js%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EC%84%9C%EB%B2%84%EB%A6%AC%EC%8A%A4-serverless-503ee61539d4

    반응형
    facebook twitter googleplus kakaostory naver