How to add customization files in Elastic Beanstalk

There is an entire section in the AWS Beanstalk documentation about configuring your application with customization files, but for us 2 aspects are actually missing: how to actually add those files and how to check for the running output of those files.

Let’s say you deploy with Beanstalk a Java app that is build with Maven. The generated artifact is a jar that is uploaded in Beanstalk as a new application version and then deployed to all hosts. Now let’s say we want to have gcc compiler installed on all hosts. Of course, we cannot ssh on each host and install this. What happens if a host is replaced?

Fortunately, Beanstalk comes with an option: to add a file inside your artifact where to specify what package to install or to provide a script to run during deployment. But the part that we found difficult or maybe not very clear is how to add these config files in the jar. Initially you could believe these files are under a wrong path or in a wrong format. In our case it took a while to realize the problem: by default, they are not included in the jar during build.

The solution

The solution is quite simple: use a Maven plugin to add them in the jar.

Let’s see exactly the problem:

Under the package root, there is a folder named .ebextensions. In that folder there are files, one with 2 packages to install and one with a bash command to execute. As always, the code is available here.

Then, in the pom.xml, we setup a plugin to actually add these 2 files in the artifact.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
       <execution>
           <id>prepare</id>
           <phase>package</phase>
           <configuration>
               <tasks>
                   <unzip src="${project.build.directory}/${project.build.finalName}.jar" dest="${project.build.directory}/${project.build.finalName}" />
                   <copy todir="${project.build.directory}/${project.build.finalName}/" overwrite="false">
                       <fileset dir="./" includes=".ebextensions/**"/>
                   </copy>
                    <zip compress="false" destfile="${project.build.directory}/${project.build.finalName}.jar"
                                    basedir="${project.build.directory}/${project.build.finalName}"/>
               </tasks>
           </configuration>
           <goals>
               <goal>run</goal>
           </goals>
       </execution>
    </executions>
</plugin>

Then, when you deploy the new artifact, you can grep the /var/log/eb-activity.log and there you’ll find the output of the commands added in your file. Or why a package hadn’t been installed. But in this case, anything should go fine and in the end you’ll have both git and gcc commands available on your ec2 host.

Really hope this article was useful for you. Don’t forget to share it with anyone interested!

Happy cloud computing!

6 thoughts on “How to add customization files in Elastic Beanstalk

  1. We are a group of volunteers and starting a new scheme in our community.
    Your website provided us with valuable info to work on. You have done an impressive job and our entire community will be thankful to you.

Comments are closed.