Dan K
Dan K

Reputation: 803

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?

Upvotes: 0

Views: 2336

Answers (1)

mickzer
mickzer

Reputation: 6338

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! :)

Upvotes: 2

Related Questions