diff --git a/.github/workflows/deploy-lambda.yaml b/.github/workflows/deploy-lambda.yaml new file mode 100644 index 0000000..576fb0e --- /dev/null +++ b/.github/workflows/deploy-lambda.yaml @@ -0,0 +1,75 @@ +name: Deploy to Lambda (ECR) + +on: + push: + branches: [ "develop", "main" ] + +# OIDC 인증을 위한 권한 설정 +permissions: + id-token: write + contents: read + +jobs: + deploy: + name: Build, Push to ECR, and Deploy to Lambda + runs-on: ubuntu-latest + + # 브랜치에 따라 사용할 환경 선택 + environment: ${{ github.ref == 'refs/heads/main' && 'PROD' || 'DEV' }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + # AWS 자격 증명 설정 + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ vars.AWS_ROLE_ARN }} + aws-region: ${{ vars.AWS_REGION }} + + # Amazon ECR 로그인 + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + # Docker 이미지 빌드 및 푸시 + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY_NAME }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest -f Dockerfile . + + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest + + echo "image_uri=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + + # Lambda 함수 업데이트 + - name: Deploy new image to AWS Lambda + run: | + aws lambda update-function-code \ + --function-name ${{ vars.LAMBDA_FUNCTION_NAME }} \ + --image-uri ${{ steps.build-image.outputs.image_uri }} + + # 코드 업데이트가 완료될 때까지 대기 + - name: Wait for Lambda function update to complete + run: | + aws lambda wait function-updated \ + --function-name ${{ vars.LAMBDA_FUNCTION_NAME }} + + # Lambda 환경 변수 설정 + - name: Update Lambda Environment Variables + run: | + aws lambda update-function-configuration \ + --function-name ${{ vars.LAMBDA_FUNCTION_NAME }} \ + --environment "Variables={ \ + REGION_NAME=${{ vars.AWS_REGION }}, \ + LAMBDA_FUNCTION_NAME=${{ vars.LAMBDA_FUNCTION_NAME }}, \ + ECR_REPOSITORY_NAME=${{ vars.ECR_REPOSITORY_NAME }}, \ + BUCKET_NAME=${{ vars.BUCKET_NAME }}, \ + THUMBNAIL_BUCKET=${{ vars.THUMBNAIL_BUCKET }} \ + }" \ No newline at end of file diff --git a/.github/workflows/dev-deploy-lambda.yaml b/.github/workflows/dev-deploy-lambda.yaml deleted file mode 100644 index b405bc7..0000000 --- a/.github/workflows/dev-deploy-lambda.yaml +++ /dev/null @@ -1,49 +0,0 @@ -name: Dev Deploy ECR - -on: - push: - branches: [ "main" ] - -env: - AWS_REGION: ${{ secrets.AWS_REGION }} - ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} - LAMBDA_FUNCTION_NAME: ${{ secrets.LAMBDA_FUNCTION_NAME }} - -jobs: - deploy: - name: Build, Push to ECR, and Deploy to Lambda - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v4 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ env.AWS_REGION }} - - - name: Login to Amazon ECR - id: login-ecr - uses: aws-actions/amazon-ecr-login@v2 - - - name: Build, tag, and push image to Amazon ECR - id: build-image - env: - ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ github.sha }} - run: | - docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest -f Dockerfile . - - docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest - - echo "image_uri=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT - - - name: Deploy new image to AWS Lambda - run: | - aws lambda update-function-code \ - --function-name $LAMBDA_FUNCTION_NAME \ - --image-uri ${{ steps.build-image.outputs.image_uri }} \ No newline at end of file diff --git a/lambda_compare.py b/lambda_compare.py index 140c63b..2fc3fe9 100644 --- a/lambda_compare.py +++ b/lambda_compare.py @@ -3,12 +3,13 @@ import base64 import re import time +import os -REGION_NAME = "ap-northeast-2" -BUCKET_NAME = "snorose-bucket" +REGION_NAME = os.environ.get('REGION_NAME') +BUCKET_NAME = os.environ.get('BUCKET_NAME') IMAGE_KEY = "test/12345/test_video.mp4" -FUNC_CON = "CreateThumbnail" -FUNC_IMG = "CreateThumbnailECR" +LAMBDA_FUNCTION_NAME = os.environ.get('LAMBDA_FUNCTION_NAME') +ECR_REPOSITORY_NAME = os.environ.get('ECR_REPOSITORY_NAME') client = boto3.client('lambda', region_name=REGION_NAME) @@ -91,9 +92,9 @@ def run_test(func_name, label): print("=== Lambda 성능 비교 테스트 시작 ===\n") # Console 버전 테스트 - run_test(FUNC_CON, "Console Version") + run_test(LAMBDA_FUNCTION_NAME, "Console Version") # Container 버전 테스트 - run_test(FUNC_IMG, "Container Image Version") + run_test(ECR_REPOSITORY_NAME, "Container Image Version") print("\n=== 테스트 완료 ===") \ No newline at end of file diff --git a/lambda_function.py b/lambda_function.py index 52fda2e..8ccdfd7 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -4,7 +4,7 @@ import subprocess s3_client = boto3.client('s3') -thumbnail_bucket = 'snorose-public-bucket' +thumbnail_bucket = os.environ.get('THUMBNAIL_BUCKET') IMG_EXT_LIST = ["jpg","jpeg","png","jfif","bmp","webp"] VDO_EXT_LIST = ["mp4","mov"]