How to add rollback functionality to a basic S3 CodeBuild deploy

5.8k views Asked by At

I have followed this instruction to get a very basic ci workflow in aws. It works flawless but I want to have a extra functionality, rollback. First i though it would work "out-of-the-box", but not in my case, if I select the the previous job in CodeBuild that i want to rollback to and hit "Retry" i get this error message: "Error ArtifactsOverride must be set when using artifacts type CodePipelines". I have also tried to rerun the whole pipeline again with pipeline history page, but it's just a list of builds without any functionality.

My questions is: how to add a rollback function to my workflow. It doesn't have to be in the same pipeline etc. But it should not touch git.

1

There are 1 answers

2
TimB On

AWS CloudFormation now supports rolling back based in a CloudWatch alarm.

I'd put a CloudFront distribution in front of your S3 bucket with the origin path set to a folder within that bucket. Every time you deploy to S3 from CodeBuild you deploy to a random new S3 folder.

You then pass the folder name in a JSON file as an output artifact from your CodeBuild step. You can use this artifact as a parameter to a CloudFormation template updated by a CloudFormation action in your pipeline.

The CloudFormation template would update the OriginPath field of your CloudFront distribution to the folder containing your new deployment.

If the alarm fires then the CloudFormation template would roll back and flip back to the old folder.

There are several advantages to this approach:

  • Customers should only see either the new or old version while the deployment is happening rather than seeing potentially mixed files while the deployment is running.
  • The deployment logic is simpler because you're uploading a fresh set of files every time, rather than figuring out which files are new and which need to be deleted.
  • The rollback is pretty simple because you're flipping back to files which are still there rather than re-deploying the old files.

Your pipeline would need to contain both the CodeBuild and a sequential CloudFormation action.