I am trying to Deploy an application from GitHub.I have created a repository in Github with appspec.yml.
The following is the code of my appspec.yml:
version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: ubuntu
- location: scripts/start_server
timeout: 300
runas: ubuntu
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: ubuntu
I am getting an error while deploying the application
Script at specified location: scripts/install_dependencies run as user ubuntu failed with exit code 1
The install_dependencies script exits with code 1, because Apache installation for permission before installing Apache.
To get around this problem use the -y flag with the install command inside your install_dependencies.sh file
#!/bin/bash
sudo apt-get install -y apache2
Related
I'm trying to deploy my spring boot application with AWS CodeDeploy in a EC2 linux machine but the process is stoping in BeforeInstall and throwing the message error: Script does not exist at specified location: /opt/codedeploy-agent/deployment-root/eb41ddde-ad23-492e-b3a0-203e1f77fb93/d-JIGRR5O5J/deployment-archive/server_clear.sh
my appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/server
permissions:
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
BeforeInstall:
- location: script/server_clear.sh
timeout: 300
runas: ec2-user
AfterInstall:
- location: fix_privileges.sh
timeout: 300
runas: ec2-user
ApplicationStart:
- location: server_start.sh
timeout: 20
runas: ec2-user
ApplicationStop:
- location: server_stop.sh
timeout: 20
runas: ec2-user
buildspec.yml
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
build:
commands:
- mvn clean install
post_build:
commands:
- echo Build completed
artifacts:
files:
- target/*.jar
- scripts/*.sh
- appspec.yml
discard-paths: yes
We see you have an AppSpec.yml but you did not mention about "server_clear.sh". Does it exists?
If your answer is not, just create a "script" folder and put a "server_clear.sh" in it with all the commands you need.
If you don't need this, just erase "BeforeInstall" hook from your AppSpec.
It looks like you have copied an AppSpec example but you actually don't need all the hooks it has in it, so you'll have the same problem with the other hooks. If you don't need them, just erase them.
I have a ubuntu on EC2 for which I have configured my appspec.yml to be executed with following config
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/source/testingCI_CD/
file_exists_behavior: OVERWRITE
hooks:
ApplicationStart:
- location: scripts/something.sh
runas: root
with something.sh being
echo "Some text here." > myfile.txt
The deployment works successfully passing all deployment lifecycle events but the bash file is not getting executed.
For me to executed the bash file I have to go into the folder and execute sudo bash something.sh. And this won't work if I don't use sudo and will result in insufficient permission. I have tried using 777 on the file and trying to execute bash something.sh which didn't work. I am worried if being unable to access the file with sudo is the reason codeDeploy is not executing my bash file.
I have a code pipeline setup for React application. Whenever a new build is updated(In client folder) , the files(directories remain) are getting removed the server folder.
Tried seeing the event logs in code deploy, not able to get much info
could someone please help me with this?
appSpec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/client
hooks:
AfterInstall:
- location: scripts/after.sh
timeout: 300
runas: root
buildSpec.yml
version: 0.2
env:
variables:
NODE_ENV: "development"
phases:
install:
runtime-versions:
nodejs: 14
pre_build:
commands:
- npm install
build:
commands:
- npm run build-qa
artifacts:
type: zip
paths:
- './build/**/*'
- './appspec.yml'
- './scripts/**/*'
after.sh
#!/bin/bash
cd /home/ubuntu/client
echo '---Process started---'
echo '---Taking backup---'
sudo rm -r backup_*
sudo mv /home/ubuntu/client/deploy /home/ubuntu/client/backup_$(date +"%d-%m-%Y")
echo '---backup complete---'
echo '---renaming build---'
sudo mv /home/ubuntu/client/build /home/ubuntu/client/deploy
echo '---renaming complete---'
Edit: I was running two CI/CD pipelines (One for Frontend and other was backend) both running for the same EC2 instance, I was using the same deployment group and application for both the CI/CD which was causing the issue, later I created separate deployment application and group for each CI/CD which resolved . Thanks #Shivkumar Mallesappa for the lead
AWS CodeDeploy is used for a simple WordPress application. Installed AWS codedeploy-agent on ubuntu 20.04 with help of the below script
#!/bin/bash
apt update
apt install ruby -y
gem install bundler
git clone https://github.com/aws/aws-codedeploy-agent.git /opt/codedeploy-agent
sudo chown -R root.root /opt/codedeploy-agent
sudo chmod 644 /opt/codedeploy-agent/conf/codedeployagent.yml
sudo chmod 755 /opt/codedeploy-agent/init.d/codedeploy-agent
sudo chmod 644 /opt/codedeploy-agent/init.d/codedeploy-agent.service
cd /opt/codedeploy-agent
bundle install --system
rake clean && rake
cp /opt/codedeploy-agent/init.d/codedeploy-agent /etc/init.d/
systemctl daemon-reload
systemctl start codedeploy-agent
systemctl enable codedeploy-agent
Using the below appspec.yml for code deployment. Its working fine with runas root
Questions :
How to run it as an ubuntu user, ?
Is any issue with while running as root user ?
....
appspec.yaml file
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/
overwrite: true
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/setup_environment.sh
timeout: 300
runas: root
- location: scripts/after_install.sh
timeout: 900
runas: root
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
ApplicationStop:
- location: scripts/stop_server.sh
timeout: 300
ValidateService:
- location: scripts/validate_service.sh
timeout: 300
While runas ubuntu getting the below error.
Error code
ScriptFailed
Script name
scripts/setup_environment.sh
Message
Script at specified location: scripts/setup_environment.sh run as user ubuntu failed with exit code 4
LifecycleEvent - AfterInstall
Script - scripts/setup_environment.sh
[stderr]shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[stderr]shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[stderr]/opt/codedeploy-agent/deployment-root/44d6390b-485e-87ef-b50855bbf251/d-D0RTN7AR5/deployment-archive/scripts/setup_environment.sh: line 4: /var/www/html/.env: Permission denied
[stderr]sed: couldn't open temporary file /var/www/html/scripts/seTwGZAv: Permission denied
If you run it as ubuntu user it will not work due to lack of permissions which you are experiencing:
couldn't open temporary file /var/www/html/scripts/seTwGZAv: Permission denied
The reason is that /var/www/html/ is not accessible by ubuntu user. To make it work you would have to change its default permissions which is a bad practice.
Some things have to be executed as root, unless you want to start changing default configurations and permission model of ubuntu operating system.
As appspec.yml file and scripts are managed by you, there is not any security issue while running our script as root. What you'll write is what you'll get.
While using any non root user it is important to provide all the required permissions to that user. In most of the cases you will have to use sudo before each command and make sure your user is added to sudoers.
You need to make sure that
Your git is secure from any unauthorized changes.
CodeDeploy is only accessible to the trusted resources.
If these two things are checked, there's no way any anomalous command can run on your system
I have two problems deploying via AWS CodeDeploy.
I'm trying to deploy CodeCommit's code to an EC2 ubuntu instance.
At appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu
hooks:
ApplicationStart:
- location: scripts/ApplicationStart.sh
timeout: 300
runas: ubuntu
There are several config files that I need to place at the right place in the application before starting pm2. I also assume since I set runas in appspec.yml as ubuntu, the bash script will at /home/ubuntu.
The my /home/ubuntu has
config/ backend/ frontend/
Looks like Code deploy won't overwrite the previous deployment so if I have backend/ and frontend/ folder at the directory, it will fail at Install stage.
In the ApplicationStart.sh
#!bin/bash
sudo cp config/config1.json backend/config/config1.json
sudo cp config/config2.json backend/config/environments/development/config2.json
sudo cp config/config3.json frontend/config3.json
sudo pm2 kill
cd backend
sudo npm install
sudo pm2 start "strapi start" --name backend
cd ../frontend
sudo npm install
sudo pm2 start "npm start" --name frontend
While the ApplicationStart stage, it gives me the following error.
LifecycleEvent - ApplicationStart
Script - scripts/ApplicationStart.sh
[stderr]bash: /opt/codedeploy-agent/path/to/deployment/scripts/ApplicationStart.sh: bin/bash:
bad interpreter: No such file or directory
I run the same bash file at the /home/ubuntu. It works fine.
Question 1.
- how to run BeforeInstall.sh without the error? Is there the path problems or something else I try to do but I am not supposed to do?
Question 2.
- How can I let code deploy to overwrite the previous deployment when there are already application folders in the directory (/home/ubuntu)?
- Do I manually delete the directory at BeforeInstall stage?
You're missing a slash before bin/bash in #!bin/bash.
It should be #!/bin/bash.