How to Deploy an App to Elastic Beanstalk

In this guide, we introduce deploying an app whose code is stored in a CodeCommit repository into Elastic Beanstalk. We will build an app — a “Hello World” Express (Node.js) app — and deploy it into Elastic Beanstalk.

This module is a prerequisite for the post “How to Build a Software Factory”.

(Note: You want to make sure you are in the same region throughout these phases.)

I. Set up Cloud9 environment

1. Go to the Cloud9 console: https://console.aws.amazon.com/cloud9

2. On the Cloud9 dashboard, click on Create environment button

3. Follow the wizard

Note: there is no charge for Cloud9, but you are charged for the EC2 instance behind it. As such, if you wish to avoid costs, you could opt for one of the free-tier instance type.

4. Once the environment is created, remain on that page for the next step. If you have exited, go to the the Cloud9 dashboard again and open the IDE for that environment.

II. Create and setup a CodeCommit Repository

5. Go to the CodeCommit console: https://console.aws.amazon.com/codecommit

6. On the console, click on Create repository and follow the wizard. (Note the name of the repo.)

7. On your newly created repository page, click on the Clone URL button

Note: if you don’t have a specific option in mind, you can go with the HTTPS

8. Now, navigate to your Cloud9 environment and into the Command Line Interface

9. On the Cloud9 CLI, do a git clone of your repository:

git clone <theURL-from-step7>

10. Change directory: move into the directory of your repository.

cd <your-repo-name>

11. Then, type the following (If you are not familiar with nano, you can follow the signposts on the screen. It’s beginner-friendly enough.)

nano app.js

12. Copy and paste these six lines (cmd-C, cmd-V on a mac)

const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => res.send('Hello, World!'))
app.listen(port)
console.log(`App listening on port ${port}`)

13. Next, run these two commands to finish the app:

npm init -y
npm install express --save

14. Then, copy and paste this whole block into your Cloud9 Command Line:

git add . \
&& git commit -m "Initial commit" \
&& git push

15. Now we’ll install the Elastic Beanstalk CLI tools. Copy and paste this entire block into your Cloud9 Command Line:

cd .. \
&& git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git \
&& python ./aws-elastic-beanstalk-cli-setup/scripts/ebcli_installer.py \
&& echo 'export PATH="/home/ec2-user/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

16. Next, navigate into your repository folder again, with the following command:

cd <your-repo-name>

17. Type eb init into the Command Line. Then, follow the prompt:

  • Select a default region: [pick your region]
  • Select an application to use: [ Create new Application ]
  • Enter Application Name: [name the app]
  • It appears you are using Node.js. Is this Correct? Y
  • Select a platform branch: [1]
  • Do you wish to continue with Code Commit? Y
  • Select a repository: [pick your repo]
  • Select a branch: [1]
  • Do you want to set up SSH for your instance? n

18. Next, create your EB environment with this command:

eb create my-env

19. Once the process finishes with “successfully launched environment: my-env”, run nano package.json and insert this line of code under “name”:

"engines": { "node": ">=15" },

20. Once you’ve ^X from nano, run this block of code to sync git:

git add . \
&& git commit -m "Fix package.json" \
&& git push

21. Finally, deploy your application into Elastic Beanstalk with this line:

eb deploy

22. Once the process finishes, go to https://console.aws.amazon.com/elasticbeanstalk

Select the Environments from the left-hand menu, and note the URL column of your “my-env”. Click that URL, and see that your app is live.

*To help minimize your AWS bill, you can terminate the Elastic Beanstalk from its console, and then relaunch the environment from the Applications section of the Elastic Beanstalk console when you need one again.

Troubleshooting

If you encounter errors in the command line — such as when running eb deploy — here are some steps that might fix the problem:

  • Try examining the error messages. Sometimes, they may be helpful. At other times, they may not.
  • If those error messages are not helpful, try running eb logs. The first section of the logs should give you an idea of what went wrong, and perhaps even point the direction of what to do to fix it.

Leave a Comment