How it started
One day, I noticed that a thumbnail image on a screen I was working on looked weirdly stretched.
"Oh, I know this one. Just switch CSS object-fit from cover to contain and done."
Feeling pretty confident, I fixed the code. But the image was still stretched. When I opened the image URL directly from the browser's dev tools, the image coming back from Amazon S3 was already stretched.
"Are you kidding me?"
But I had just joined the project, and I don't really know infrastructure. I had no idea where the image-generation code lived, so I asked a coworker.
"Sorry to bother you — this thumbnail seems to be resized somewhere on the AWS side. Do you know where that happens?"
My coworker said:
"Oh, that's done by a Lambda function in a separate infrastructure repository. But editing that repository directly won't help. We need to move the code into the current application repository and set up CI/CD again."
In my head it was like, "Ah, Lambda, I've heard of that… it's an AWS thing," but I decided to just give it a try.
STEP 1: What is Lambda anyway?
It's "a small program that runs automatically when a specific event happens." It's one of the serverless services, so you don't have to manage servers yourself. For example, it can run on events like these:
- When a file is uploaded to Amazon S3
- When a request comes in to API Gateway
- At a specific time (like cron)
In this case, the setup was: "When an original image is uploaded to an S3 bucket, a Lambda function starts, generates a thumbnail, and saves it to another S3 bucket." So fixing the Lambda function's code looked like it would fix the stretched image too.
STEP 2: Migrating the Lambda function and building CI/CD
- Bring the Lambda function's source code from the old infrastructure repository into the current application repository.
- Fix that code so images don't get stretched. (This time I fixed the resize logic, changing the
resizemethod to thethumbnailmethod.) - Build a mechanism (CI/CD) with GitHub Actions that automatically deploys to the Lambda function on AWS when the code is updated.
▼ Automatically deploying to Lambda with GitHub Actions
Using a GitHub Actions workflow, set it up so that code is automatically deployed to AWS Lambda when it's pushed to a specific branch (e.g. main). The config goes in .github/workflows.
# sample code name: Deploy to AWS Lambda on: workflow_dispatch: inputs: environment: description: 'Choose the environment to deploy to' required: true type: choice options: - release-candidate - staging - production jobs: deploy: runs-on: ubuntu-latest environment: ${{ github.event.inputs.environment }} steps: - name: Checkout code uses: actions/checkout@v3 # ... (set up Node.js or Python, install dependencies, etc.) ... - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v2 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ap-northeast-1 - name: Build and Zip run: | # build and packaging steps zip -r function.zip . - name: Deploy to Lambda run: | aws lambda update-function-code \ --function-name ${{ secrets.YOUR_LAMBDA_FUNCTION_NAME }} \ --zip-file fileb://function.zip There are two key points in this workflow.
1. Using secrets per environment
The GitHub repository had release-candidate, staging, and production environments under Settings > Environments. So in each environment's secrets, I registered the matching Lambda function name (e.g. rc-YourLambdaFunctionName) under the key YOUR_LAMBDA_FUNCTION_NAME.
When the workflow runs, the right function name is loaded automatically for the selected environment, which reduces the risk of deploying to the wrong place.
2. Safe deploys with a manual trigger
Using on: workflow_dispatch instead of on: push prevents unintended automatic deploys. Developers just press the "Run workflow" button in the GitHub Actions tab and pick the environment, and the release goes out safely and reliably.
🔥 Gotcha: the "Run workflow" button doesn't show up in GitHub Actions!
This happened when I created the workflow and tried to test running it manually. Looking at the Actions tab of the GitHub repository, the manual "Run workflow" button just wasn't there.
The reason was simple: the manual-run UI only appears once the workflow file containing workflow_dispatch is merged into the repository's default branch (main or master).
No matter how much you experiment on a development branch, the button won't appear. Not knowing this, I spent a good while fighting with GitHub.
Summary
It was just one stretched image, but it pulled me into the world of AWS Lambda and CI/CD.
- A display bug isn't always caused by the frontend or backend.
- AWS Lambda is a handy, event-driven automation program.
- Manual workflow runs (
workflow_dispatch) only become available from the default branch.
And so the bug got fixed, and I felt great about it~