AWS CodeDeploy says script missing even though it exists - amazon-web-services

I am trying to deploy an application onto EC2 instances with AWS CodeDeploy. I am getting up to the point where CodeDeploy errors out at the BeforeInstall hook. The error message I am getting is related to a ScriptMissing. The exact error is:
Script does not exist at specified location: /opt/codedeploy-agent/deployment-root/1bfe51a8-151a-4366-8a3f-c061adb4bb90/d-X8N0B5IOE/deployment-archive/scripts\codedeploy\install_dependencies.sh
The odd thing is the file exists in my version control repository. So far I have tried changing the permissions of the script, and have also tried different method in the appspec.yml file in order to get CodeDeploy to recognize this file. All methods have failed. I tried the other solutions posted relating to this scenario but they do not seem to help me.
Do these script files int he appspec.yml also need to part of the zip file that CodeDeploy uses? Any advice would be appreciated. Below is the appspec.yml file.
Appspec
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html
overwrite: true
hooks:
BeforeInstall:
- location: scripts/codedeploy/install_dependencies.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/codedeploy/install_composer_dependencies.sh
timeout: 300
runas: root
- location: scripts/codedeploy/start_server.sh
timeout: 30
runas: root
ApplicationStop:
- location: scripts/codedeploy/stop_server.sh
timeout: 30
runas: root

I had a similar problem. My paths was scripts/file-name.sh. I just removed 'scripts/' from location path and leave only .sh file names and it works for me.

Related

AWS CodeDeploy Appspec.yml files are not being copied to destination

My files are properly copied to an EC2 instance for deployments, but they are not copied to the destination. I've got at appspec.yml file like this:
ubuntu#ip:~$ cat /opt/coded*/deployment-root/*/*/de*/appspec.yml
version: 0.0
os: linux
# I also tried with source: /
files:
- source: .
destination: /home/ubuntu/
file_exists_behavior: OVERWRITE
# notes
# ------------------------------------------------------------------------------------
# To learn more about hooks, visit the docs here:
# https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_server
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: root
I've also tried setting source to source: / with the same result; nothing's copied
And I see my files are all in that /opt/coded* etc. path. But why isn't the CodeDeploy agent copying my files to the destination? The Download bundle hook is good, and the only error I get is in the ApplicationStart hook when I need to actually access my files.
Note: I'm using GitHub as my revision storage, not S3, if that helps
Edit: something that might be useful to know is in my install_dependencies script, I run something along the lines of:
cd /home/ubuntu/
pip install -r requirements.txt
where the pip installation is the line where it can't find the files
Reasoning: https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-example.html#appspec-file-example-server
The order of the hooks. The files are copied before the AfterInstall hook.

AWS CodeDeploy error - The deployment failed because a specified file already exists at this location

i am trying to deploy the python application through code-deploy to the ec2 instances
but during deployment i am facing this error
The deployment failed because a specified file already exists at this
location: /home/ubuntu/yello/manage.py
attaching my appsecfile also tried with overwrite but no luck
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/yello
overwrite: true
hooks:
AfterInstall:
- location: script/services.sh
timeout: 300
runas: ubuntu
can anyone help ?
This is a very common error so AWS public docs has a section for the same.
https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-same-files-different-app-name
Basically when creating the deployment, you need to select Overwrite the content option so the deployment does not fail (default behaviour). Details here:
https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-rollback-and-redeploy.html#deployments-rollback-and-redeploy-content-options
The default behaviour prevents two Deployments from two different CodeDeploy "Applications" writing the same file and possibly causing some sort of conflict.
cd /var/www/your-directory/public/funnel_webhooks/test/
sudo rm -rf clickfunnel.txt
After removing this file please release the change in the code pipeline.
Now you can specify file_exists_behavior in your appspec file. Allowed values are DISALLOW, OVERWRITE, or RETAIN.
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/yello
file_exists_behavior: OVERWRITE

CodeDeploy configuration to overwrite files

I am deploying via CodeDeploy a logstash configuration in a target instance in /etc/logstash/.
This is the last step in a 3-staged CodePipeline.
However it fails with message
The deployment failed because a specified file already exists at this location: /etc/logstash/pipelines.yml
How can I instruct CodeDeploy to overwrite files?
March 2022 Update: Here are the docs on how to handle the file-exists-behavior: AppSpec 'files' Section.
July 2021 Update: Looks like AWS has finally acknowledged this defect -- look here for updates on all that.
August 2020 answer:
The only way I have seen that you can overwrite files is selecting to overwrite or retain, as seen here, when you're creating a new deployment. In no other place will you see those settings. And if I'm not mistaken, this is the only way to do it because it is not supported on the CLI.
An issue to add an overwrite option is open on GitHub and it has been there for quite a while. I solved this by adding a custom script in the BeforeInstallation hook which will erase all existing files before copying over my new deployment.
This is how my appspec.yml file looks like
version: 0.0
os: linux
files:
- source: /
destination: /path/to/destination
hooks:
BeforeInstall:
- location: DeploymentScripts/CleanupScript.sh
AfterInstall:
- location: DeploymentScripts/InstallScript.sh
timeout: 600
ApplicationStart:
- location: DeploymentScripts/RestartScript.sh
timeout: 3600
You can pass this attribute in the appspec.yml under
files :
overwrite: true

AWS CodeDeploy. AppSpec: Script at specified location: scripts/start_server.sh is not executable

I have a porblem with the correct configuration of the AppSpec.yml. I'v been trying for hours now to get it to work correctly. The problem is that I cant get my ApplicationStart -script to run afterwards.
The logs file says that:
Script at specified location: scripts/start_server.sh is not executable. Trying to make it executable.
Which is pretty straightforward. My question is, can someone spot the mistake I'v been doing here? I want to give the scripts file 755 permission, so I can run it with ubuntu -user.
Here is what my AppSpec looks at the moment:
version: 0.0
os: linux
files:
- source: /
destination: /var/www/app-folder
permissions:
- object: /var/www/app-folder
mode: 755
owner: ubuntu
type:
- directory
- object: /var/www/app-folder/scripts
mode: 755
owner: ubuntu
type:
- file
hooks:
ApplicationStart:
- location: scripts/start_server.sh
timeout: 10
runas: ubuntu
Turns out my code-deploy agent was stuck on EC2 and only showing the last error before got stuck.
The error was:
Script at specified location: scripts/start_server.sh is not
executable. Trying to make it executable.
Restarted the service and now working.

AWS CodeDeploy ScriptFailed Error in AfterInstall

While trying to deploy a Django project with CodeDeploy for the first time, I keep getting the following error in the AfterInstall phase:
Error Code: ScriptFailed
Script Name: /setup.sh
Message: Script at specified location: /setup.sh failed with exit code 2
Log Tail: LifecycleEvent - AfterInstall
Script - /setup.sh
[stderr]python: can't open file 'setup_start.py': [Errno 2] No such file or directory
This is probably because I'm misunderstanding the files section of the AppSpec. Below is a snippet of what I'm doing for that section:
files:
- source: ./BlackBoxes
destination: project/BlackBoxes
- source: ./Documentation
destination: project/Documentation
- source: ./manage.py
destination: project
- source: ./setup_start.py
destination: project
...
I did make the project folder manually on the S3 bucket but none of the subfolders.
And the AfterInstall section:
hooks:
...
AfterInstall:
- location: /setup.sh
timeout: 180
What I originally thought source was supposed to mean was the relative path of the file/directory with respect to the root of the project directory on my local development machine. I also assumed that any folder needed that didn't exist on the S3 bucket would be created automatically. Clearly, I am misunderstanding something about CodeDeploy, most likely pertaining to the AppSpec file. What exactly am I doing wrong with the deployment and what am I supposed to be doing instead?
You're going wrong in the files section. This section is where you tell Code Deploy: "place this directory/file from my repo to this location on my EC2 instance". You only need to do this for stuff your deploying, you don't need to do this for deployment hook scripts. Refer to the docs for the fine details on this section.
In a hook, the location is the relative path to your hook script from the root of your repo. So /setup.sh isn't correct -> you need to give it the relative path. Again, the docs is the place to read more on this.
What I usually do is in the root of my repo, I create a folder called eg. scripts and I store the hook scripts there.
Say my repo directory structure looks like this:
application_code/
scripts/
appspec.yml
I can then set up my appspec.yml like this:
version: 0.0
os: linux
files:
- source: application_code
destination: /desired/code/location #path to where the code should be put on the instance
hooks:
ApplicationStop:
- location: scripts/some_script.sh
timeout: 300
runas: root
Code Deploy is simple to use once you've read the documentation comprehensively. Until then, you're going to keep encountering problems like this.
Best of luck! :)