title: AWS Linux CodeDeploy Permission Issues (w. Bitbucket, Tomcat, Shell Script)
body: <p>I'm trying to deploy files using CodeDeploy to my AWS Beanstalk server with Tomcat installed. Everything is well configured except for an exception which occurs when appspec.yml calls my .sh script and <em>mvn install</em> command is executed. I've tried all combinations of permissions I've imagined (as well as every StackOverflow answer I've found), but nothing has worked. </p>
  
  <pre><code>Cannot create resource output directory: /opt/codedeploy-agent/deployment-root/f953d455-9712-454b-84b0-2533cf87f79a/d-3UFCDLD0D/deployment-archive/target/classes
  </code></pre>
  
  <p>I also expected the files section of appspec.yml to get executed before the .sh script gets executed. It should have been working like this:</p>
  
  <ol>
  <li>appspec.yml moves all files to webapps folder</li>
  <li>build.sh gets executed</li>
  <li>mvn runs and creates the .war file</li>
  <li>build.sh does some cleaning up </li>
  </ol>
  
  <p>appspec.yml (I've tried multiple other)</p>
  
  <pre><code>version: 0.0
  os: linux
  files:
     - source: /
       destination: /var/lib/tomcat8/webapps
  permissions:
     - object: /opt/codedeploy-agent/deployment-root
       pattern: "**"
       owner: ec2-user
       group: root
       mode: 755
       type:
         - directory
     - object: /var/lib/tomcat8/webapps
       pattern: "**"
       owner: ec2-user
       group: root
       mode: 755
       type:
         - directory
  hooks:
     BeforeInstall:
      - location: scripts/build.sh
        runas: ec2-user
  </code></pre>
  
  <p>build.sh</p>
  
  <pre><code>export LANG=en_US.UTF-8
  
  SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &amp;&amp; pwd )"
  echo "Script path: $SCRIPTPATH"
  
  PROJECT_SOURCE_DIR=$SCRIPTPATH/../
  cd $PROJECT_SOURCE_DIR
  
  mvn clean install
  
  cd $PROJECT_SOURCE_DIR/target
  ls -a
  
  for file in *.war; do
      mv $file /usr/share/tomcat8/webapps/ROOT.war
  done;
  
  rm -rf $PROJECT_SOURCE_DIR/target
  rm -rf $SCRIPTPATH
  </code></pre>
  
  <p>It's obvious from the exception that maven tries to create a folder <em>target</em> without having the permissions. So the questions are why on the first place it's trying to execute it in this folder and then how to gain proper access.</p>