From 5e2c7b1a1c14a5d9d492e5d4ce6f524106d431a2 Mon Sep 17 00:00:00 2001 From: Perry Zhu Date: Sun, 12 Oct 2025 22:14:41 -0700 Subject: [PATCH] feat: script to lambda --- .github/workflows/build-lambda.yaml | 46 +++++++++++++++++++++++++++ .github/workflows/push-artifacts.yaml | 2 +- scripts/lambda-handler.py | 45 ++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build-lambda.yaml create mode 100644 scripts/lambda-handler.py diff --git a/.github/workflows/build-lambda.yaml b/.github/workflows/build-lambda.yaml new file mode 100644 index 0000000..e1cba4c --- /dev/null +++ b/.github/workflows/build-lambda.yaml @@ -0,0 +1,46 @@ +# Build the lambda function zip file and push to S3 +name: Publish to S3 +on: + push: + branches: + - main + paths: + - scripts/** +jobs: + push-artifacts-to-s3: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ secrets.AWS_IAM_ROLE_ARN }} + aws-region: ${{ secrets.AWS_REGION }} + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.13" + - name: Build Lambda + run: | + # Create build directory + mkdir -p dist + + # Install dependencies to build directory + uv sync --no-cache --target dist + + # Copy Lambda function files to build directory + cp scripts/lambda-handler.py dist/ + cp scripts/main.py dist/ + + # Create deployment package + cd dist + zip -r ../lambda-function.zip . + cd .. + - name: Deploy to S3 + run: | + # Upload the Lambda function zip to S3 + aws s3 cp lambda-function.zip s3://${{ secrets.AWS_S3_BUCKET }}/lambda/lambda-function.zip diff --git a/.github/workflows/push-artifacts.yaml b/.github/workflows/push-artifacts.yaml index 6a85860..4466c5c 100644 --- a/.github/workflows/push-artifacts.yaml +++ b/.github/workflows/push-artifacts.yaml @@ -32,7 +32,7 @@ jobs: run: yarn cibuild - name: Deploy to S3 run: | - aws s3 sync ./dist s3://${{ secrets.AWS_S3_BUCKET }} --delete + aws s3 sync ./dist s3://${{ secrets.AWS_S3_BUCKET }}/website --delete - name: Invalidate CloudFront cache uses: foxdalas/cloudfront-invalidator@v4 with: diff --git a/scripts/lambda-handler.py b/scripts/lambda-handler.py new file mode 100644 index 0000000..c49bc33 --- /dev/null +++ b/scripts/lambda-handler.py @@ -0,0 +1,45 @@ +import sys +import os + +sys.path.append(os.path.dirname(os.path.abspath(__file__))) + +from main import get_image_links_selenium + +def lambda_handler(event, context): + """ + Fetches the latest pexels featured image links using Selenium + """ + try: + pexels_url = "https://www.pexels.com/@perry-z-1662054943/featured-uploads/" + image_links = get_image_links_selenium(pexels_url) + + if image_links: + return { + 'statusCode': 200, + 'body': { + 'success': True, + 'image_links': image_links, + 'total_count': len(image_links) + } + } + else: + return { + 'statusCode': 200, + 'body': { + 'success': False, + 'message': 'No image links found', + 'image_links': [], + 'total_count': 0 + } + } + + except Exception as e: + return { + 'statusCode': 500, + 'body': { + 'success': False, + 'error': str(e), + 'image_links': [], + 'total_count': 0 + } + }