Deploying and Updating Node.js Applications with AWS Elastic Beanstalk: A Guide to URL Swapping
AWS Elastic Beanstalk simplifies the deployment and management of applications. It handles the infrastructure setup, allows easy updates to your application code, and provides URL-swapping functionality for seamless transitions between different versions of your application. This enables you to focus on developing your Node.js application while leveraging the power and scalability of AWS infrastructure.
Let us start with our Project:
First, log in to AWS Console and go to AWS Elastic Beanstalk and Select "Create application"
Then, the Configuration page will open, Select "web server environment" and give the application name
In the environment, information gives a name to the environment or selects the default environment
In Platform, select "Managed platform" and we will be choosing "Node.js"
In the Application code, we can upload our own code or we can choose a sample application, here we will be using sample code for practice
And Select Presets as "single instance (free tier) and select - Next
Next, we can configure additional settings, but we also have the option to "skip to review" because we are using the sample application
Here, I have given the Service name and EC2 Key pair, and IAM policies which are not required and you can skip this step because it automatically configures the same.
We can use an additional configuration more efficiently if we have to deploy an application with specific requirements or needs, for now, we are using samples for practice.
On the Review page, just review the information provided by you
And submit
Creating an environment takes a few minutes, just be patient
As we can see our environment is created with a domain URL to access the application
We can check the environment and also
We can check the application
Now, as we created our environment, we can check all the resources which are required to create the environment and deploy our application in " AWS CLOUD FORMATION"
As we can see a Stack is being created we can check the stack to get information
We can check Events, Resources, logs, and more information in the stack
Let us go back to AWS Beanstalk Application,
Now Go to our environment "test-env" - go to the application and Click on "Domain" - to Access our sample application
Here, we go we have deployed our first AWS Beanstalk Application.
Go to our environment, Select your "test-env"
Here, we can see the option available for "Upload and deploy" and on the panel, we see several options for - configuration, events, logs, etc
Next, we will "Upload and deploy" and push the updated version of the code let's start.
Now you might be wondering we do not have the code since we used a sample application, No worries.
Open a new tab and search - AWS Elastic Beanstalk Sample code, we will get an AWS documents link - go in there.
And scroll down and search for Node.js, Now AWS itself uses this same sample code for deployment, - Download the Node.js code Zip file
Go to downloads and Extract the Zip file, And Thats "stuff" folder you see is my backup folder don't worry about that - As you extracted the Zip file Node.js folder is extracted - go inside the node.js folder
As we go inside the folder we can see a few files And we need the "Index.html" file Now open the same in the terminal or for a better working environment open it in VS CODE.
Here, we can see the index file - go to line 38 - we will see the Background color - we will be changing that
We change the Background color to "Orangered - from - Green"
Dont worry once you edit you will get the option to change color select any one or just type the color you want but make sure don't mess with the code syntax
Now, Go inside the same node.js folder
Select All the files we show earlier and compress them to zip
Here, we compress our file and named it "nodejs.orange" , Why we compressed the file - because we need all the files including the index.html for our application
Now Go back to our AWS Beanstalk and to "test-env" We see the "upload and deploy" option, select it
Give a version name, Choose the Zip file from our system, and select "Deploy"
It might take few minutes to Deploy the the updated version of the code
Here we see the Deployment is successful and now we can access the application - Click the Domain URL
Here we can see the updated version of our application, whose background color is now "Orange" From "Green"
All this is being deployed by using n EC2 resources, So if we go and check the EC2 instance, here we see an Instance is running for our Test-environment
We created our application and also pushed updates. Now what
It's usually not recommended to push your updated code version directly to your live server or application. It is best to use another server or application to test the updated code and check for any errors and bugs if all is good and then push it to your live Server or application
So we will create a new environment and deploy the same node.js sample application there
Go to your AWS Beanstalk and don't just create the environment yet, First go to your "Test-env" And go to the application, there you will see an option for "create new environment"
And provide a name for the Second Environment and follow the step same as we did to build our first Environment.
Here we can see the build is in progress and will take a few minutes
The build is successful and we can access the Second environment application by clicking on the domain URL
Here is the New Second node.js Application
Now, We have Deployed Two Environments, So that we can push our updates onto Second Server instead of to the Live Server
Now we will see how Swapping URLs works and why it is important.
Note: URL swapping in AWS Elastic Beanstalk enables seamless updates and traffic redirection. It redirects incoming traffic from the current application version to the updated version, ensuring an uninterrupted user experience during the transition. This allows us to push updates without disrupting users, keeping them connected to the latest version while maintaining service availability and reliability.
We are in our Second Environment - here, Go to "Actions" And select "Swap environment domain"
The config page will open
Here, we do not need to do much just - Select the environment "our first environment - Test-env" And Select "Swap".
Swapping is Successful and We can see the domain URL is changed and replaced by the first environment URL,if we Click on the URL
The First Environment Updated application is opened, but we were in the second environment remember And that's how swapping URLs works. And by now you know why swapping is important
So now if we go to our First environment "test-env" and Click on the domain
We are re-directed to the Second Environment Node.js Application
So, that is all from my side, I hope you got some idea about AWS Elastic Beanstalk How to create an Environment and push updates, and the correct way to push updates and Swpping of URLs
Thank you for Reading the Article!!!!