I build an AMI from a 'prototype' EC2 instance I created to confirm everything is working. I create a Launch Configuration and Auto Scaling Group based on the AMI. I associate the Auto Scaling Group with CodeDeploy as a Blue/Green Deployment behind an Application Load Balancer.

When I run the CodePipeline, all goes well until it gets to the CodeDeploy phase and it tries to install the application in the specified directory in appspec.yml:

The deployment failed because a specified file already exists at this location: /var/www/html/mysite/test.rb

My expectation was that CodeDeploy will only update the files in the deploy directory. Instead, it breaks and says the files already exist. From research online, I discovered that when you deploy, CodeDeploy will look for a file (on the instance) with information on previously deployed files for the application/deployment-group in question. If it finds the file it then it uses it to cleanup (delete) the existing files, preparing for copying of the new revision files.

But if the cleanup file doesn't exist, CodeDeploy won't continue running. Therefore the files will not be cleaned up, and the deployment will fail since CodeDeploy does not overwrite files. it wont be run.

Therefore the files will not be cleaned up, and the deployment will fail since CodeDeploy does not overwrite files.

It seems as though the only option in these scenarios is to have a hook (eg BeforeInstall) that calls a script to delete the applications files (if they exist) on the instance. Is this really true? The only option I have is to delete the files in a beforeInstall hook?

I notice if you copy a deployment in the Console, there is an option to 'overwrite the content'. But this option is not available in the Deployment Group configuration. It only exists when you copy the deployment.

  • Well, I also came here for a solution but I think as of today there's not a straight forward solution. The only way to do the job is by using the BeforeInstall hook. quite annoying though.
    – umer
    Commented Oct 1, 2020 at 19:36

2 Answers 2


You probably can just add a flag --file-exists-behavior OVERWRITE to your aws deploy create-deployment command.

Ref: https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html


Add "file_exists_behavior: OVERWRITE" in appspec.yml.

This is my sample appspec.yml

version: 0.0
os: linux
  - source: /
    destination: /var/www/html/
    overwrite: true
file_exists_behavior: OVERWRITE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.