<p>Add &quot;file_exists_behavior: OVERWRITE&quot; in appspec.yml.</p>
<p>This is my sample appspec.yml</p>
<pre><code>version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/
    overwrite: true
file_exists_behavior: OVERWRITE
</code></pre>

CC BY-SA 4.0
2022-06-15T22:23:27.607Z
2022-06-15T22:23:27.607Z
mahendra rathod
<p>You probably can just add a flag <code>--file-exists-behavior OVERWRITE </code> to your <code>aws deploy create-deployment</code> command.</p>
<p>Ref:
<a href="https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html</a></p>

CC BY-SA 4.0
2021-07-12T09:17:29.060Z
2021-07-12T09:17:29.060Z
Diego
<p>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.</p>

<p>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:</p>

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

<p>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.</p>

<p>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. </p>

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

<p>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? </p>

<p>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. </p>

CC BY-SA 4.0
2020-10-01T19:36:16.693Z
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
CC BY-SA 4.0
2024-12-11T19:16:01.313Z
Yes, adding the `file_exists_behavior: OVERWRITE` attribute as mentioned in the thread - https://devops.stackexchange.com/a/16156/48775 - worked for me as well
Raul Gil
<amazon-web-services><aws-codedeploy>
CodeDeploy: deployment failed because a specified file already exists at this location
Daniel Viglione
# CodeDeploy: deployment failed because a specified file already exists at this location

(Asked by: username_0 on 2020-01-17)

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.

**Tags:** amazon-web-services, aws-codedeploy

> **Comments:**
> **username_1**: 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.  
> **username_2**: Yes, adding the `file_exists_behavior: OVERWRITE` attribute as mentioned in the thread - https://devops.stackexchange.com/a/16156/48775 - worked for me as well  

---

## 2 answers

### Answer by username_4 on 2021-07-12. Score: 1

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>


---

### Answer by username_3 on 2022-06-15. Score: 0

Add "file_exists_behavior: OVERWRITE" in appspec.yml.
This is my sample appspec.yml
version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/
    overwrite: true
file_exists_behavior: OVERWRITE


---

