One Way to Run Post Deployment Cypress Tests for an Amplify App

I recently had to figure out a way to have Cypress tests run against an Amplify app immediately after the app has been deployed. Since Amplify doesn’t have a post deployment step as part of its build and deployment process (defined in amplify.yml), I had to come up with a solution using other AWS services.

I settled on using a combination of Event Bridge and CodeBuild.

EventBridge

EventBridge is an AWS service that connects various AWS components together using events.

How it works

{ 
  "source": ["aws.amplify"], 
  "detail-type": ["Amplify Deployment Status Change"] 
} 
{ 
  "source": ["aws.amplify"], 
  "detail-type": ["Amplify Deployment Status Change"], 
  "detail": { 
    "appId": ["your_app_id"], 
    "branchName": ["main"], 
    "jobStatus": ["SUCCEED"] 
  } 
}

CodeBuild

CodeBuild is an AWS service that enables running various commands against a code repository to generate the final artifacts for storage in S3 and later deployment.

Commands are defined in various steps within a buildspec.yml file that is stored in the root of the code project repo. In my case, I can use CodeBuild simply to run my Cypress tests after deployment and to store the Cypress artifacts in S3 for viewing.

How it works

version: 0.2 
env: 
  variables: # Set any environment variables that Cypress needs here. Prefix them with CYPRESS_ 
phases: 
  install: 
    runtime-versions: 
      nodejs: latest 
    commands: 
      - npm ci 
  build: 
    commands: 
      - npx cypress run 
artifacts: 
  files: 
    - "**/*.png" 
    - "**/*.mp4" 
  base-directory: "cypress" 
  discard-paths: yes # Uploads just the files, not their full directory paths 
  name: builds/$CODEBUILD_BUILD_NUMBER/cypress-artifacts 
# make sure to set "enable semantic versioning" in the Codebuild project configuration section for artifacts