DevOps와 클라우드 환경에서의 CI/CD(지속적 통합 및 지속적 배포)는 현대 소프트웨어 개발의 핵심 요소입니다. 이번 포스팅에서는 CI/CD 파이프라인을 활용하여 서버리스 애플리케이션을 배포하는 간단한 미니 프로젝트를 진행해보겠습니다.

프로젝트 개요

서버리스 아키텍처는 서버 관리의 부담을 덜어주며, 개발자가 비즈니스 로직에 집중할 수 있게 합니다. AWS Lambda를 활용하여 서버리스 애플리케이션을 구축하고, GitHub Actions를 통해 CI/CD 파이프라인을 설정하는 프로젝트를 진행할 것입니다.

환경 설정

  1. AWS 계정 생성: AWS Lambda를 사용하기 위해 AWS 계정이 필요합니다.
  2. AWS CLI 설치: AWS CLI를 설치하여 로컬에서 AWS 서비스에 접근할 수 있도록 합니다.
  3.    pip install awscli
       aws configure
  4. Node.js 설치: Node.js 환경에서 Lambda 함수를 작성할 것입니다.
  5. Lambda 함수 작성

    먼저, 간단한 Node.js Lambda 함수를 작성해보겠습니다. lambda_function.js라는 파일을 생성하고 다음 코드를 입력합니다.

    exports.handler = async (event) => {
        const message = 'Hello, Serverless World!';
        return {
            statusCode: 200,
            body: JSON.stringify(message),
        };
    };

    서버리스 애플리케이션 배포

    AWS Lambda에 이 함수를 배포하기 위해 AWS Management Console을 사용할 수 있지만, CI/CD를 통한 자동 배포를 설정하는 것이 목적이므로, SAM(Serverless Application Model)을 사용할 것입니다.

  6. SAM CLI 설치:
  7.    npm install -g aws-sam-cli
  8. SAM 템플릿 파일 생성: template.yaml 파일을 생성합니다.
  9.    AWSTemplateFormatVersion: '2010-09-09'
       Transform: AWS::Serverless-2016-10-31
       Resources:
         HelloWorldFunction:
           Type: AWS::Serverless::Function
           Properties:
             Handler: lambda_function.handler
             Runtime: nodejs14.x
             Events:
               Api:
                 Type: Api
                 Properties:
                   Path: /hello
                   Method: get
  10. 배포 스크립트 작성: deploy.sh라는 파일을 생성하고 다음 내용을 추가합니다.
   #!/bin/bash
   aws cloudformation package --template-file template.yaml --s3-bucket your-s3-bucket-name --output-template-file packaged.yaml
   aws cloudformation deploy --template-file packaged.yaml --stack-name HelloWorldStack --capabilities CAPABILITY_IAM

CI/CD 파이프라인 구성

이제 GitHub Actions를 사용하여 CI/CD 파이프라인을 설정합니다. .github/workflows/deploy.yml 파일을 생성합니다.

name: Deploy Serverless Application

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'

    - name: Install dependencies
      run: npm install

    - name: Deploy to AWS
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_REGION: 'ap-northeast-2'
      run: |
        chmod +x ./deploy.sh
        ./deploy.sh

GitHub Secrets 설정

AWS 접근을 위해 GitHub Secrets에 AWS_ACCESS_KEY_ID와 AWS_SECRET_ACCESS_KEY를 등록합니다. GitHub 리포지토리의 Settings > Secrets에서 환경 변수를 추가할 수 있습니다.

배포 및 테스트

이제 코드를 main 브랜치에 푸시하면 자동으로 CI/CD 파이프라인이 실행되어 AWS Lambda에 서버리스 애플리케이션이 배포됩니다. 배포가 완료되면, AWS API Gateway URL을 통해 애플리케이션을 테스트할 수 있습니다.

curl https://your-api-id.execute-api.ap-northeast-2.amazonaws.com/Prod/hello

마무리

이번 포스팅을 통해 CI/CD 파이프라인을 활용한 서버리스 애플리케이션 배포의 기초적인 흐름을 배웠습니다. AWS Lambda와 GitHub Actions의 조합은 개발의 효율성을 높이고, 배포 자동화를 통해 안정성을 강화하는 데 큰 도움이 됩니다. 이 예제를 바탕으로 더 복잡한 애플리케이션을 구축하고 CI/CD 파이프라인을 확장해보세요!