DEV Community

Cover image for Hosting n8n on AWS EC2 with Docker
Zahraa Jawad
Zahraa Jawad

Posted on

Hosting n8n on AWS EC2 with Docker

Outline
Introduction to n8n
Why use n8n
Steps to build and run n8n on AWS

Introduction to n8n
It’s open-source, offering a free self-hosted option and affordable cloud plans. It blends no-code simplicity with low-code flexibility, supporting everything from basic automation to custom-coded integrations. This versatility fuels a wide range of n8n use cases, making it a go-to for anyone aiming to automate smarter.

Image description

Why use n8n

n8n (pronounced n-eight-n) helps you to connect any app with an API with any other, and manipulate its data with little or no code.
Customizable: Highly flexible workflows and the option to build custom nodes.
Convenient: Use npm or Docker to try out n8n, or the Cloud hosting option if you want us to handle the infrastructure.
Privacy-focused: Self-host n8n for privacy and security.
from n8n documintation

Steps to build and run n8n on AWS
In this article, we will use AWS to build n8n in simple steps by creating a free tier instance in our AWS account and installing Docker and Docker Compose to build and run n8n.

Before we start, note that this setup is unencrypted (HTTP only) and is suitable for testing or development environments only.

Step 1: "Launch Instance"

When logging into the AWS account, we select the EC2 service through Services or by the search box:

Image description

Click on Launch instance

Image description

Under Name and tags

Enter a name to identify your instance. For this tutorial, name the instance (n8n-server)

Image description

Under Application and OS Images:

From Quick Start, choose an AMI that meets your web server needs
Here we choose Ubuntu (which is free tier eligible)

Image description

Under Instance type:

Choose the type of instance. Here we choose t2.micro(which is a free tier eligible).

Image description

Under Key pair (login):

Choose the key pair

Image description
or create a new key pair:
Give a name to the key pair, then click Create key pair

Image description

Under Network settings: under Firewall (security groups)

Choose to create security groups
To Allow SSH traffic by clicking on the check box

Image description

Leave all other configurations as they are (default settings)

In the Summary panel, review your instance configuration and then choose Launch instance.

Image description

Successfully initiated the launch of the instance, and to see the instance, click on the ID:

Image description

Your instance will first be Pending, and will then go into the Running state.

Image description

Step 2: "Connect to the instance"

To connect to your instance, select the instance and choose Connect.

Image description

There are many ways to connect to EC2, here we will choose the SSH client to connect.
After selecting the "SSH Client" section, copy and execute the following commands in the terminal as per the following steps:

Image description

Open Terminal (here we use Git Bash)

Image description
Change the directory with the cd command (change directory), where you have downloaded your pem file(key pair).

In this article, the pem file is stored in the downloads folder.

Execute the cd command to change the path to the location of the encryption key
cd Download/

Image description

Execute the following commands sequentially

  1. chmod 400 [key pair name].pem
  2. ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name

Image description

After the command is executed you will be prompted to type “Yes” to continue with the connection

Image description
And that’s it! Now we’re logged in to our AWS instance.

Image description
Now, to complete our work, we get root permission by executing the sudo -i command:

Image description

Executing the command "sudo -i" means booting as root on Linux. This command's main feature is that it gives you full admin (root user) privileges, allowing you to perform commands and operations that require root user privileges.
We update the repositories through the command:
sudo apt update && sudo apt upgrade -y

Image description

Image description

Step 3: "Install Docker"

After connecting to the instance via SSH and obtaining the root privilege, use the following command automation to install Docker:
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker

Image description
Docker has been installed on the instance successfully.

Image description

Docker experience: Run a simple test command docker -v to check that Docker is working properly and to see the Docker version:

Image description

Step 4:Install Docker Compose:
Now we install Docker Compose using the command:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose

Image description

Image description
Grant operating permissions:
sudo chmod +x /usr/local/bin/docker-compose

Image description
Then, check the installation by running the command:
docker-compose --version

Image description

Step 5: Install n8n
First, we create a project folder for n8n and prepare the necessary files.

  • Create a folder for our project using the mkdircommand, and enter this folder using the cdcommand, as: mkdir ~/n8n-server && cd ~/n8n-server mkdir n8n_data

Image description

  • Then permit the data folder: sudo chown -R 1000:1000 ./n8n_data

Image description

  • Create the docker-compose.yml file

Now we create the docker-compose.yml file, through the command vim or nano and put the code in the file. Here we will use the command nano:
nano docker-compose.yml

And past the codes:

Image description
Note: N8N_HOST=[Puplic Ip of your instance]
and N8N_WEBHOOK_URL=http://[Puplic Ip of your instance]:5678/

Then exit the file from the keyboard by ctrl x, then y (to save the file) then click Enter

Image description

Step 6: Open the n8n port in AWS:

To open the port for the host n8n, go to your AWS account and select the instance that was created for the server, then select security and open your security group:

Image description

Choose the inboud rules then Edit inbound rules

Image description

Click on Add rule

Image description

Enter the rule:

Image description

We choose the type: Custom TCP, port: 5678, and source: 0.0.0.0/0 (or choose a private IP address for more security).
Note: In practice, it is not preferable to leave the state 0.0.0.0/0 This is easy to hack, but we are here to learn the labs and the building process.

Run the n8n:
To run the n8n, we execute the command:
docker-compose up -d

Image description

Image description
To verify that the container is running, we execute the command:
docker ps

Image description
The container has been launched successfully, and now we can go to the browser to run the application.

Now, go back to the instance and select it, then go to the Details and copy the Public IPv4 address

Paste the public IPv4 address with port 5678 into the browser and press Enter

Image description

Image description

n8n has been successfully built.This is the first account setup page (owner account).

We enter our email, first name, last name, and a strong password, then click Next.

We will complete the rest of the settings according to your work.

Image description

Image description

Image description

Image description

Image description

Image description

Image description

n8n is now fully operational on a public IP of AWS EC2 using Docker and is ready to build your Workflows or connect with your other tools.

References:

Top comments (0)