You can use Github Actions to run your tests against an ephemeral stack each time you open a new Pull Request and push new code to it.
name:Deploy and run testson:pull_request:branches: - main# avoid running several deployments in parallelconcurrency:${{ github.ref }}# Grant GA the required permissions to assume the IAM rolepermissions:id-token:writecontents:readjobs:deploy:runs-on:ubuntu-lateststeps: - uses:actions/checkout@v3 - uses:actions/setup-node@v3with:node-version:'16.x' - name:Install dependenciesrun:npm ci - name:Configure AWS Credentialsuses:aws-actions/configure-aws-credentials@v1-node16with:role-to-assume:arn:aws:iam::123456789012:role/github-deployer# the role to assumerole-duration-seconds:3600# Deploy to a unique stack for each Pull Request - name:Deploy run: sls deploy --stage ci${{ github.event.pull_request.number }} # or cdk deploy --all or a deploy command that makes sense for your stack
# run tests - name:Integration testsenv:# specify a unique SLS_JEST_TAG for this PRSLS_JEST_TAG:ci${{ github.event.pull_request.number }}run:npx jest --runInBand
Teardown the ephemeral stack on merge
When you are happy with your PR, you can teardown your stack and cleanup sls-jest artifacts after you merge it.
name:Teardown stackon:pull_request:types: [closed]branches: - main# Grant GA the required permissions to assume the IAM rolepermissions:id-token:writecontents:readjobs:deploy:runs-on:ubuntu-lateststeps: - uses:actions/checkout@v3 - uses:actions/setup-node@v3with:node-version:'16.x' - name:Install dependenciesrun:npm ci - name:Configure AWS Credentialsuses:aws-actions/configure-aws-credentials@v1-node16with:role-to-assume:arn:aws:iam::123456789012:role/github-deployer# the role to assumerole-duration-seconds:3600 - name:Cleanup sls-jest artifactsrun:npx sls-jest destroy --tag ci${{ github.event.pull_request.number }} - name:Destroy stack run: sls remove --stage ci${{ github.event.pull_request.number }} # or cdk destroy --all --force or a remove/destroy command that makes sense for your stack