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.

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:

Click on Launch instance

Under Name and tags

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

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)

Under Instance type:

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

Under Key pair (login):

Choose the key pair


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

Under Network settings: under Firewall (security groups)

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

Leave all other configurations as they are (default settings)

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

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

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

Step 2: "Connect to the instance"

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

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:

Open Terminal (here we use Git Bash)


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/

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

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


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


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

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

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


Docker has been installed on the instance successfully.

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

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


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


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

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

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

  • 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:


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

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:

Choose the inboud rules then Edit inbound rules

Click on Add rule

Enter the rule:

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


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


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

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.

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)