monitor showing Java programming

Triển khai Continuous Delivery cho dự án Serverless Backend với Gitlab-CI và AWS Lambda Function

Article overview

Giả sử chúng ta phát triển một sản phẩm Serverless Backend với AWS Lambda Function và mong muốn áp dụng CD để tự động hoá công việc deploy lên Cloud.
Bài viết áp dụng cho cấu trúc hệ thống git với mỗi một Function sẽ có một branch phát triển riêng. Ví dụ source code cho Function authentication sẽ được lưu ở branch master-authentication.

Tổng quan về các công nghệ sử dụng:

  • NodeJS
  • Gitlab-CI
  • AWS Lambda Function, AWS CLI
  • Môi trường MacOS, Linux

Table of contents

Chúng ta cần một số bước sau:

Cài đặt và cấu hình môi trường tại thiết bị chạy service runner

Đầu tiên, chúng ta cần cài đặt AWS CLI.
Sau khi cài đặt xong, ta thực hiện config với thông tin của AWS User có quyền deploy lên AWS S3 với câu lệnh sau:

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json

Cấu hình các job CI/CD với gitlab-ci.xml và Gitlab-CI

Đầu tiên, để có thể update code lên AWS Lambda chúng ta sẽ sử dụng command aws lambda update-function-code.

Command hỗ trợ tham số –zip-file để upload source code dưới dạng .zip file, nên việc đầu tiên chúng ta cần làm là zip source code lại.
zip -r deploy.zip .
Sau khi zip xong, ta thực hiện deploy zip file lên AWS Lambda bằng câu lệnh sau:

aws lambda update-function-code --function-name authentication --zip-file fileb://deploy.zip

Với authentication là tên của Lambda Function, deploy.zip là tên file zip cần đẩy lên.

Ta sẽ setting command cho package.json như sau:

"scripts": {
    "deploy": "zip -r deploy.zip . && aws lambda update-function-code --function-name authentication --zip-file fileb://deploy.zip"
}

Tiếp đó, ta sẽ cấu hình .gitlab-ci.yml để hệ thống tự động deploy khi có thay đổi trên nhánh master-authentication.

stages:
  - Deployment
deploy:
  stage: Deployment
  before_script: []
  only:
      - master-authentication
  allow_failure: true
  script:
    - yarn install --production 
    - yarn deploy

Sau đó merge code vào master-authentication, và hưởng thành quả. Từ giờ các bạn không cần phải deploy bằng tay nữa rồi, chúc các bạn may mắn.

Authors

ThangPV12


Posted

in

, ,

by

Comments

4 responses to “Triển khai Continuous Delivery cho dự án Serverless Backend với Gitlab-CI và AWS Lambda Function”

  1. Sam Avatar
    Sam

    Amazing, Gút Chóp em!

  2.  Avatar
    Anonymous

    Good job, bro

  3. minhnn1020 Avatar
    minhnn1020

    Best article, hihi

  4. AnhLPT Avatar
    AnhLPT

    Hay quá

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: