How to Deploy a NodeJS App with AWS CloudFormation: Complete Guide
By Braincuber Team
Published on March 24, 2026
Imagine you have built a great product and its user base is growing rapidly. You want to scale your product to be available to people around the world. To do this, you need good cloud infrastructure. But managing your cloud infrastructure manually can be exhausting. You might start wondering, "How are enterprise companies doing it?" The answer is infrastructure as code, and AWS CloudFormation is one of the most powerful tools to achieve it.
What You'll Learn:
- What AWS CloudFormation is and how it works
- Creating CloudFormation templates in YAML format
- Provisioning EC2 instances with CloudFormation
- Configuring Security Groups for ports 22, 80, and 8000
- Attaching UserData scripts to deploy NodeJS apps automatically
- Using CloudFormation parameters for reusable templates
- Creating and deleting CloudFormation stacks
- Validating templates with CloudFormation Designer
What is AWS CloudFormation?
AWS CloudFormation is a service that helps you automate creating and managing your cloud resources. Think of it like building a house - before construction, you create a blueprint specifying exactly what you want and where. CloudFormation works the same way for your cloud infrastructure.
You can specify what resources you want to create (EC2 servers, databases, storage, and more) and how they should be configured. CloudFormation takes care of creating and managing those resources for you automatically.
When CloudFormation is Most Helpful
Multi-Environment Management
Manage infrastructure changes across Development, Staging, and Production environments consistently.
Cross-Region Replication
Re-create the same infrastructure in a different region or account with a single template deployment.
Instant Resource Recovery
Re-create accidentally deleted resources with exact configurations in seconds, not hours of manual work.
Easy Updates
Update your blueprint and CloudFormation handles all changes automatically to your infrastructure.
How CloudFormation Works
CloudFormation works by letting you upload templates (in YAML or JSON format) that describe your desired infrastructure. These templates are stored in Amazon S3 behind the scenes, and CloudFormation uses them to provision and manage your resources.
Template - YAML or JSON file describing your infrastructure
Stack - Collection of AWS resources managed as a single unit
Change Set - Summary of proposed changes before execution
Important: You cannot edit templates after upload. Re-upload the updated version and CloudFormation will compare it with existing infrastructure to make necessary changes.
How to Deploy CloudFormation Templates
There are two ways to deploy CloudFormation templates:
| Method | Description | Best For |
|---|---|---|
| CloudFormation Designer | Visual drag-and-drop interface | Beginners unfamiliar with YAML/JSON |
| YAML/JSON Templates | Write code directly | Developers and DevOps engineers |
This complete tutorial guide focuses on writing YAML templates, which are more readable and easier to maintain than JSON.
Creating a CloudFormation Template to Deploy a NodeJS App
AWS CloudFormation supports over 224 resource types. In this beginner guide, we will create an EC2 instance, configure a Security Group, and add a startup script to deploy a NodeJS application.
CloudFormation Template to Create an EC2 Instance
Resources in CloudFormation are defined using the format AWS::aws-product-name::data-type-name. For EC2 instances, the resource type is AWS::EC2::Instance.
Create Your First CloudFormation Template
Create a new file named template.yaml and add the following basic EC2 instance configuration:
Resources:
SampleNodejsDeploy:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-a4c7edb2
InstanceType: t2.micro
Understanding EC2 Instance Properties
| Property | Description | Example Value |
|---|---|---|
| AvailabilityZone | AWS data center location | us-east-1a |
| ImageId | Amazon Machine Image ID | ami-a4c7edb2 |
| InstanceType | EC2 instance size/type | t2.micro |
How to Deploy a NodeJS App using UserData
The UserData property allows you to pass scripts that execute during EC2 instance launch. This is how we automate NodeJS app deployment - the script runs automatically when the instance starts.
The NodeJS Deployment Script
Add UserData to Your Template
The script below installs NodeJS, Yarn, PM2 process manager, clones a repository, and starts the application on port 8000.
#!/bin/bash
set -e
curl -sL https://deb.nodesource.com/setup_16.x | bash -
sudo apt install nodejs
node -v
npm -v
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
yarn --version
sudo -i -u ubuntu bash << EOF
set -e
cd /home/ubuntu
sudo npm install -g pm2
git clone https://github.com/5minslearn/node_with_docker.git
cd node_with_docker
yarn install
pm2 start yarn --time --interpreter bash --name sample_node -- start -p 8000
EOF
Important Note
EC2 UserData takes time to complete. The first time you access your application, it may take several minutes to load while dependencies are installed. Be patient - the script is running automatically in the background.
How to Create a Security Group using CloudFormation
Security Groups act as virtual firewalls for your EC2 instances. They control inbound and outbound traffic. We need to open ports 22 (SSH), 80 (HTTP), and 8000 (NodeJS application).
Add Security Group to Template
The Security Group resource type is AWS::EC2::SecurityGroup. Add this configuration to your template:
SampleNodejsDeploySG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: for the app nodes that allow ssh, http, 8000
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '8000'
ToPort: '8000'
CidrIp: 0.0.0.0/0
How to Attach the Security Group to EC2
CloudFormation provides the !Ref intrinsic function to reference resources within your template. We use this to attach the Security Group to our EC2 instance.
Add SecurityGroups Property
Add the SecurityGroups property to your EC2 instance and reference the Security Group using !Ref:
Resources:
SampleNodejsDeploy:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-014d05e6b24240371
SecurityGroups:
- !Ref SampleNodejsDeploySG
UserData:
Fn::Base64:
|
#!/bin/bash
set -e
curl -sL https://deb.nodesource.com/setup_16.x | bash -
sudo apt install nodejs
node -v
npm -v
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
yarn --version
sudo -i -u ubuntu bash << EOF
set -e
cd /home/ubuntu
sudo npm install -g pm2
git clone https://github.com/5minslearn/node_with_docker.git
cd node_with_docker
yarn install
pm2 start yarn --time --interpreter bash --name sample_node -- start -p 8000
EOF
SampleNodejsDeploySG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: for the app nodes that allow ssh, http
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '8000'
ToPort: '8000'
CidrIp: 0.0.0.0/0
How to Use Parameters in CloudFormation Templates
Instead of hardcoding values like key pair names, use Parameters to make your templates reusable. Parameters allow users to input values when creating a stack.
Add Parameters Section
Add a Parameters section at the top of your template to accept the key pair name dynamically:
Parameters:
SSHKey:
Type: AWS::EC2::KeyPair::KeyName
Description: name of the key pair to ssh into the instance
Now add KeyName: !Ref SSHKey to your EC2 instance Properties, and reference it using the !Ref function.
How to Create a CloudFormation Stack
Now that your template is ready, let us deploy it to AWS. Follow these steps to create your CloudFormation stack.
Access CloudFormation Console
Log in to the AWS Console and search for CloudFormation in the search bar. Click on Stacks in the left sidebar to get started.
Click Create Stack
Select Template is ready and choose Upload a template file. Upload your template.yaml file.
Validate Using Designer
Click View in Designer to visualize your template. Click the tick icon to validate your template and check for errors.
Enter Stack Details
Enter a Stack name and select your SSH Key Pair. If you do not have one, create an EC2 key pair first.
Review and Submit
Review all settings and click Create stack. Wait for resources to be provisioned - this may take several minutes.
Accessing Your Deployed NodeJS App
Once your stack creation completes, follow these steps to access your NodeJS application:
Get Public IPv4 Address
Go to the Resources tab in CloudFormation. Click on the EC2 instance link to view its details and copy the Public IPv4 address.
Open in Browser
Open your browser and navigate to http://[YOUR-IP-ADDRESS]:8000. Your NodeJS app is now live on AWS!
First Load Delay
EC2 UserData takes several minutes to complete installation. If the page does not load immediately, wait 5-10 minutes and try again. The script is running automatically in the background.
How to Delete the CloudFormation Stack
When you no longer need the infrastructure, you can delete the entire stack with one click. This automatically deletes all resources created by the stack - both the EC2 instance and Security Group.
Delete Stack
Go to the CloudFormation console, select your stack, click Delete Stack, and confirm the action. All resources will be automatically terminated.
Key Takeaways:
- CloudFormation automates cloud infrastructure as code
- Templates in YAML format describe resources declaratively
- EC2 UserData scripts automate application deployment
- Security Groups control inbound/outbound traffic
- Parameters make templates reusable across environments
- Deleting a stack removes all associated resources
Frequently Asked Questions
What is the difference between CloudFormation and Terraform?
CloudFormation is AWS-native and fully managed by AWS. Terraform by HashiCorp is multi-cloud and uses its own state file. CloudFormation integrates deeper with AWS services, while Terraform offers more flexibility across cloud providers.
Can I update a CloudFormation template after creating a stack?
You cannot edit templates directly after upload. Instead, update your local YAML file and re-upload it to CloudFormation. CloudFormation will compare the new template with the current stack and show you what changes will be made before applying them.
What happens if CloudFormation stack creation fails?
CloudFormation automatically rolls back changes if stack creation fails, deleting any resources that were partially created. You can view detailed error messages in the CloudFormation console Events tab.
How do I pass sensitive data like passwords to CloudFormation?
Use AWS Systems Manager Parameter Store with the SSM parameter type, or AWS Secrets Manager. Never hardcode sensitive values in CloudFormation templates. The Fn::Base64 function encodes UserData, but it is not encrypted.
How much does CloudFormation cost?
CloudFormation itself is free. You only pay for the AWS resources it creates (EC2, S3, etc.). There are no charges for creating, updating, or deleting stacks.
Need Help with AWS CloudFormation Deployment?
Our AWS experts can help you create production-ready CloudFormation templates, set up complete CI/CD pipelines, and automate your entire infrastructure deployment process.
