AWS DevOps Blog

Creating Deployable App Archives for Elastic Beanstalk

by Evan Brown | on | in How-to | Permalink | Comments |  Share

AWS Elastic Beanstalk provides a simple, scalable place to host your Ruby, Java, Python, PHP, Java, Node.js, or .NET applications in the AWS cloud. But how do you get your application code to Elastic Beanstalk? In addition to simplicity and scalability, Elastic Beanstalk provides several flexible mechanisms to upload your code, including git, Eclipse, Visual Studio, and uploading an archive via the API or Management Console. Today’s blog post focuses on how to properly package and upload your app to Elastic Beanstalk.

Anatomy of an Archive

Elastic Beanstalk expects your application upload to be formatted as a ZIP file (or WAR if you’re deploying a Java application.) Additionally, your application code should be at the top level of the archive, as opposed to being nested within a folder (i.e., when you unzip the archive, the files shouldn’t be nested in a top-level directory).

For example, here’s my Python app in an Elastic Beanstalk-compatible ZIP file:

Decompressing the file (using unzip) results in all of the files appearing at the top level alongside the original archive:

~/dev/releases$ unzip myapp.zip
Archive:  myapp.zip
creating: .ebextensions/
inflating: .ebextensions/python.config
inflating: application.py
inflating: README.md
inflating: requirements.txt
creating: static/
creating: static/css/
inflating: static/css/style.css
creating: templates/
inflating: templates/index.html

Creating an Archive Manually

It’s important to note that eb, the AWS Tookit for Eclipse, and the AWS Toolkit for Visual Studio will automatically take care of correctly bundling your application. But if you’re not using one of those tools, it’s still easy to create your own ZIP archive. In this example, I use the zip command in OS X to package my application and store it in the ~/dev/releases/ directory:

~/dev/myapp$ zip -r ~/dev/releases/myapp.zip .
updating: .ebextensions/ (stored 0%)
updating: application.py (deflated 55%)
updating: README.md (deflated 25%)
updating: requirements.txt (deflated 11%)
updating: static/ (stored 0%)
updating: static/css/ (stored 0%)
updating: static/css/style.css (deflated 45%)
updating: templates/ (stored 0%)
updating: templates/index.html (deflated 35%)

You can also use OS X Finder or Windows Explorer to select and compress your app, creating a ZIP file that works with Elastic Beanstalk:

Bonus: Bundling with Git

If you happen to use Git to version control your application source, you’re in luck! Git provides a simple mechanism to create archives that work with Elastic Beanstalk via the aptly-named archive command.

The following command creates a ZIP archive of the most recent commit on the current branch:

git archive --format=zip HEAD > myapp.zip

Git Tags

If you’re using Git tags to label releases, you can easily package each tag as a ZIP archive that you can then deploy to Elastic Beanstalk. In my sample application I use git tag to view all of the tags in my repository. The result looks like this:

· ~/dev/myapp$ git tag
v1.0.0
v1.1.0
v1.2.0

To easily package each of these tags I run the following command, creating a ZIP archive for each tag and placing them in the ~/dev/releases/ folder:

git tag | while read line; do
	git archive --format=zip $line > ~/dev/releases/${PWD##*/}-$line.zip
	echo “Created ~/dev/releases/${PWD##*/}-$line.zip”
done

For my sample application, the result of running this command in my app’s git repo looks like this:

· ~/dev/myapp$ git tag | while read line; do
> git archive --format=zip $line > ~/dev/releases/${PWD##*/}-$line.zip
> echo "Created ~/dev/releases/${PWD##*/}-$line.zip"
> done
Created ~/dev/releases/myapp-v1.0.0.zip
Created ~/dev/releases/myapp-v1.0.0.zip
Created ~/dev/releases/myapp-v1.0.0.zip

Summary

Whatever tool you use, just remember: if you’re creating ZIP files of your app to deploy to Elastic Beanstalk, be sure the files are at the top level of the archive and not inside a parent folder. As we saw, you can do this using the zip CLI, in Finder or Windows Explorer, or even with Git. And don’t forget, if you’re using eb, the AWS Tookit for Eclipse, and the AWS Toolkit for Visual Studio to deploy your applications, it’s all taken care of for you.