Have problem with appspec.yml for deploy ruby on rails to ec2 instance with AWS CodeDeploy and AWS CodePipeline.

0

Hi I new on AWS here. I want to try to deploy some ruby on rails project to ec2 instance and my use case I want is when I deploy code to GitHub to my specific branch (like branch master etc.) I want it to deploy to my ec2 instance right after push to that branch. and I run into this guide (link) they use AWS CodeDeploy and AWS CodePipeline in this guide. I try to following they step and I got struck at appspec.yml. I try to following appspec.yml in this guide and make some modify to following code.

appspec.yml

version: 0.0
os: linux
files:
- source: /
destination: /var/www/eb-rails/deployment
file_exists_behavior: OVERWRITE
permissions:
- object: /var/www/eb-rails/deployment
pattern: "**"
owner: ubuntu
group: ubuntu
mode: 775
hooks:
AfterInstall:
- location: scripts/dependencies_install.sh
runas: ubuntu

and my scripts/dependencies_install.sh

#!/bin/bash
export PATH=/home/ubuntu/.rvm/gems/ruby-2.7.8/bin:/home/ubuntu/.rvm/gems/ruby-2.7.8@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.7.8/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.rvm/bin:/home/ubuntu/.rvm/bin
sudo kill -9 $(cat /var/www/my-app/tmp/pids/server.pid)
cd /var/www/my-app/
#sudo apt-get install ruby2.3-dev libffi-dev -y
gem install nokogiri — — use-system-libraries
bundle config build.nokogiri — use-system-libraries
gem install bundler — user-install
bundle install

note my ruby-version in this test project is 2.7.8

so I following guide and when I push code to my github it trigger my both AWS CodeDeploy and AWS CodePipeline but the deploy failed and when I try to see in log in my ec2 instance with less /var/log/aws/codedeploy-agent/codedeploy-agent.log I found this

2023-07-25T03:07:12 INFO  [codedeploy-agent(713)]: [Aws::CodeDeployCommand::Client 200 0.020335 0 retries] put_host_command_complete(command_status:"Failed",diagnostics:{format:"JSON",payload:"{\"error_code\":5,\"script_name\":\"\",\"message\":\"The deployment failed because the application specification file specifies only a source file (/). Add the name of the destination file to the files section of the AppSpec file, and then try again.\",\"log\":\"\"}"},host_command_identifier:"some base64 word here.......")

so I want to know where I gone wrong in my appspec.yml? I try to look into another guide they can set source: / normally but I get this in log.

已提問 9 個月前檢視次數 483 次
1 個回答
3
已接受的答案

Hello.
Since the structure of the file is yaml, I thought it would be an error if I did not set the indentation as follows.
The error message says that "destination" is not listed under "files".
So, we set the indentation as follows so that "destination" is set inside "files".
Can you confirm that this works?
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure.html

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/eb-rails/deployment
    file_exists_behavior: OVERWRITE
permissions:
  - object: /var/www/eb-rails/deployment
    pattern: "**"
    owner: ubuntu
    group: ubuntu
    mode: 775
hooks:
  AfterInstall:
    - location: scripts/dependencies_install.sh
      runas: ubuntu
profile picture
專家
已回答 9 個月前
profile pictureAWS
專家
已審閱 9 個月前
profile picture
專家
已審閱 9 個月前
  • I see. My code not give a space correctly so it can't find my destination in next line. Thank You

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南