
AWS Elastic Beanstalk is a very popular cloud service that allows you to create an environment for your web app to run in. For example, let's say you have a NodeJS app, and you want to deploy it to the cloud. You could manually create a virtual network, security groups, and then instances for dev, test and production. Or, you could simply have 3 Beanstalk environments. It simplifies the infrastructure, because you tell the service what kind of web app you're going to be running, and the environment is going to include all the prerequisites. Also, updating the app is very easy to automate. This is what we're going to look at here.
Before you start, we'll assume you already created your Beanstalk environment. If you didn't, go to the AWS Console and search for Beanstalk. There, create an environment and an application name, making sure you pick NodeJS as the platform, and write down both the application and environment names since we'll need them. By default, the environments will be created with some default content, but we'll change that soon enough. You'll also need a S3 bucket where to store your application files, which will be deployed to Beanstalk. This should have been created during the Beanstalk creation process.
Once the environment is up, we'll need to set some environment variables. Clone the git repository that contains your app, and set the environment variables. Here we'll assume that your app is called myapp and you're deploying to the production environment:
export APP_NAME=myapp
export APP_VERSION=001
export S3_BUCKET=mybucket
export ENV_NAME=production
Note that the way Beanstalk works, every app must have a version. This is how the service knows whether it should be updating the environment or not. The actual number doesn't matter, it just needs to constantly increase. Here we're manually setting the version to 001 but if you were to use a CI/CD pipeline, all the popular automation tools have a way to provide a version variable automatically.
Now that our environment variables are set, you can pull the latest changes from your app and make sure it runs correctly:
git pull
npm install
npm run app
Once everything is green here, let's zip up your application and upload it to the S3 bucket:
zip app.zip -r * .[^.]* -x '*node_modules/*' -x '*.git*'
aws s3 cp app.zip s3://$S3_BUCKET/$APP_NAME/$APP_NAME-$APP_VERSION.zip
Note that to use the AWS command line tool, you need to already have your credentials saved. If not, use aws configure to set them up. The user you set up needs to have permission to use S3 and Beanstalk commands.
Once your application has been uploaded to S3, we need to update the Beanstalk service and tell it where to find the latest version of the app:
aws elasticbeanstalk create-application-version --application-name $APP_NAME --version-label $APP_NAME-$APP_VERSION --source-bundle S3Bucket=$S3_BUCKET,S3Key=$APP_NAME/$APP_NAME-$APP_VERSION.zip
aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name $ENV_NAME-test --version-label $APP_NAME-$APP_VERSION
And that's it. If everything went well, you can go to the AWS Console and you should see the Beanstalk environment you created being updated.