CodeDeploy - BeforeInstall ScriptMissing - amazon-web-services

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.

Related

AWS CodeDeploy, Files getting removed from other folder

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

Parallel Builds with S3 artifacts in codebuild

I am trying to do batch builds in Codebuild with artifacts enabled. However, I have a strange error as listed below.
no objects found under S3 prefix for dependency artifact pre_build pre_build
This is the buildspec file I am using.
version: 0.2
artifacts:
files:
- '**/*'
batch:
fast-fail: true
build-graph:
- identifier: pre_build
buildspec: ci-cd/application-build/pre_build.yml
ignore-failure: false
- identifier: build_mysql
buildspec: ci-cd/application-build/build_mysql.yml
depend-on:
- pre_build
ignore-failure: false
- identifier: build_rabbitmq
buildspec: ci-cd/application-build/build_rabbitmq.yml
depend-on:
- pre_build
ignore-failure: false
PS: Batch build works with artifacts disabled.
Can someone please suggest me on how to resolve this issue.
My pre_build looks something like this.
Looks something like this.
version: 0.2
phases:
install:
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2&
- echo This always runs even if the update or install command fails
pre_build:
commands:
- cd ci-cd
- mkdir -p build
- ls
- echo $BUILD_EXPORT_VARIABLE_FILE
- chmod u+x export_github_variables.sh
- . ./export_github_variables.sh
cache:
paths:
- 'ci-cd/build/**/*'

NextJS with Aws Amplify deployment error

Does anyone has tried AWS-Amplify for next.js deployment? I am getting this error always that "yarn build" successful but didn't deployed and failed.
Git repo is configured with auto-deployment and YML file for aws-amplify is below.
version: 1
frontend:
phases:
preBuild:
commands:
- rm -rf node_modules
- yarn install
build:
commands:
- yarn run build
artifacts:
baseDirectory: .next
files:
- '**/*'
cache:
paths:
- node_modules/**/*
Use this yaml file in your build
version: 1
backend:
phases:
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn run build
artifacts:
baseDirectory: public
files:
- '**/*'
cache:
paths:
- node_modules/**/*

How to codedeploy appspec.yml runas ubuntu user

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

scripts/install_dependencies run as user ubuntu failed with exit code 1

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