Skip to main content

Build Once, Promote the Digest: Shipping Lambda Container Images Across Environments

When a change passes in staging and ships to production, what exactly was shipped? If each environment runs its own build, the honest answer is "the same commit, built again", and that is not the same artifact. For a container-image Lambda function, the unit you test and promote should be one image digest, with a tag acting only as a label for it. An earlier post, Why CI/CD Still Fails When the Pipeline Turns Green, covers manual steps and rollbacks in general. This guide covers the artifact itself: how to make sure production runs the bytes that staging tested.

A rebuild per environment ships a different artifact with the same name

A second build can differ from the first in ways the commit hash does not show. A base image tag may have moved, a package restore may resolve differently, or a build tool may have been updated. AWS documents this for Lambda base images: their tags are mutable and move to each newly patched image, while a digest never changes and therefore gives reproducible builds. A rebuild for production can pick up a base layer that staging never ran.

Four criteria decide whether a pipeline is trustworthy:

  • Identity: can you name the exact bytes that passed the tests?
  • Reversal: can you redeploy yesterday's bytes without rebuilding them?
  • Audit: can you connect what runs in production to a specific workflow run?
  • Config separation: does everything that differs by environment live outside the image?

A rebuild-per-environment pipeline fails the first three. The fourth is what makes the others possible: if staging and production images differ because of baked-in settings, nothing can be promoted. Environment-specific values and runtime credentials belong in configuration or a secrets service, not in the image.

Lambda already pins a digest, so the pipeline should too

Lambda does not follow tags at invocation time. AWS's launch post for container image support explains that selecting an image makes Lambda translate it to the underlying digest, and that updating the function configuration does not change the image in use, even if the tag was reassigned in the meantime. A tag is resolved once, when you create or update the function, and after that the function runs the digest.

That has two consequences. First, moving a tag does not update a function, so a pipeline that pushes latest and assumes production now runs it is wrong. Second, the function records what it resolved: the GetFunction response includes a ResolvedImageUri field under Code. You can ask Lambda which image a function points at and compare the answer with what the pipeline says it deployed.

Reference What it identifies Use it for
repo:latest or repo:prodWhatever the tag points at nowNothing in a deploy path
repo:<commit-sha>A label for humans; stays stable only if tags are immutableFinding a build from a commit
repo@sha256:...Exactly one imageDeploying, promoting, rolling back, auditing

A build job that emits a digest, and deploy jobs that only consume it

The build job pushes once and exposes the digest as an output. The sketch below uses placeholders for action versions, so pin each action to a current release, ideally by commit SHA, and omit registry login and the OIDC role configuration for brevity. It is not a tested workflow.

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
      attestations: write
    outputs:
      digest: ${{ steps.push.outputs.digest }}
    steps:
      # checkout, AWS credentials via OIDC and ECR login omitted
      - id: push
        uses: docker/build-push-action@<pinned version>
        with:
          push: true
          provenance: false     # Lambda rejects the extra attestation manifest
          tags: ${{ env.REPO }}:${{ github.sha }}

      - uses: actions/attest-build-provenance@<pinned version>
        with:
          subject-name: ${{ env.REPO }}
          subject-digest: ${{ steps.push.outputs.digest }}

  deploy-staging:
    needs: build
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - run: |
          aws lambda update-function-code \
            --function-name my-fn-staging \
            --image-uri "${{ env.REPO }}@${{ needs.build.outputs.digest }}" \
            --publish

  deploy-prod:
    needs: [build, deploy-staging]
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: |
          aws lambda update-function-code \
            --function-name my-fn-prod \
            --image-uri "${{ env.REPO }}@${{ needs.build.outputs.digest }}" \
            --publish
          aws lambda get-function --function-name my-fn-prod \
            --query 'Code.ResolvedImageUri'
  • The commit-SHA tag is for people. Everything after the build uses needs.build.outputs.digest.
  • provenance: false matters for Lambda. BuildKit's built-in provenance attestation can turn the pushed artifact into a manifest index, which Lambda does not accept for functions. The GitHub attestation step is a separate record tied to the digest, so you still get provenance.
  • There is no docker build in the promotion path, so production can only run an image that was already built and tested.
  • The final command closes the loop: the digest it reports should equal the one the build emitted. If it does not, something other than the pipeline changed the function.

On provenance, GitHub documents that artifact attestations alone provide SLSA v1.0 Build Level 2. That is a useful baseline, not a claim that the rest of the supply chain is covered. Availability depends on repository visibility and plan, so check the GitHub documentation for your case, and verify whether your registry accepts attestations before enabling the option that pushes them to it.

Immutable tags are a tripwire, not the guarantee

ECR gives you a second layer. By default, image tags are mutable, and AWS recommends enabling tag immutability for production images. With it on, pushing an existing tag fails with an ImageTagAlreadyExistsException, so a rerun of a workflow with the same SHA tag fails loudly instead of quietly replacing the image.

Treat immutability as a safety net and the digest as the contract. It also has a cost: if a pipeline reuses tags such as staging or prod, enabling it breaks that pipeline. ECR offers exclusion options for tags that must stay mutable, so check how they behave with your setup before relying on them.

The one legitimate reason to rebuild: base image patches

"Build once" does not mean "never rebuild". Lambda does not patch a container image for you. For container-based functions, AWS's guidance is to create a new image from the updated base image and redeploy. The unit of rebuild changes: you rebuild on purpose, produce a new digest, and send that digest through the same staging-to-production path, instead of letting a routine deploy rebuild the base layer as a side effect.

That also argues for pinning the base image, with automation to update it, for example a scheduled job that opens a change request. Pinning without that automation holds you on an aging base.

Checks before trusting the pipeline

  1. Search the deploy workflows for docker build or build-push-action in any job after staging. There should be none.
  2. Confirm the image reference passed to update-function-code contains @sha256:.
  3. After each deploy, compare Code.ResolvedImageUri with the digest the build emitted and fail the job on a mismatch.
  4. Diff the environment variables and secret references between staging and production. Every difference should be intentional and live outside the image.
  5. Roll back once on purpose by redeploying a previous digest, and confirm the function resolves to it.
  6. Check the ECR lifecycle policy against your rollback window. If an image a function references is deleted, the function enters a failed state, so retention has to cover everything you might roll back to.

Where to stay cautious

This pattern proves that the same image ran in both places. It does not prove the image is correct, secure or compatible with production data. Provenance records where an image came from and says nothing about whether it was safe to deploy, and a passing staging run is only as good as its tests and its similarity to production. Also check how your infrastructure-as-code tool treats image references on apply, since some tools rewrite them.

The recommendation is conditional. If container-image functions run in more than one environment, or may need a fast rollback, promote digests and add the post-deploy comparison. It is a few lines of pipeline and removes a whole class of "what actually shipped?" questions. For a single function in a single environment with a tolerable rebuild, the extra structure may not pay for itself yet, though capturing the digest is worth doing even then.

Comments