Deploying Metabase on Amazon Lightsail

Setting up Amazon Lightsail

  1. Access the Lightsail service on your AWS Console and create an OS Only instance running Ubuntu 20.
Choose availability region
Choose instance OS and image
Choose instance plan
Choose a name for your instance and click `Create instance`
Our instance is now up and running.
You will be redirected to this page where you will choose the instance to which to attach the static IP address (‘metabase-demo’ in our case) and also a name for the static IP address (‘metabase-demo-ip’)
Static IP address is now created and attached to our metabase-demo instance.
$ sudo chmod 600 /path/to/ssh-key
$ ssh -i /path/to/ssh-key ubuntu@<static-IP>
$ sudo apt update && sudo apt install apt-transport-https ca-certificates curl software-properties-common && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable" && sudo apt update && apt-cache policy docker-ce && sudo apt install docker-ce && sudo systemctl status docker
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose && docker-compose --version
$ sudo fallocate -l 2G /swapfile && sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152 && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile && sudo nano /etc/fstab
/swapfile swap swap defaults 0 0

Running our Metabase and Postgres Containers

1. So far, we haven’t done anything related to Metabase. But fortunately, we are out of the primary configuration phase and can now proceed to build and run a Metabase container. Create a folder called Metabase, cd into this folder, and create a docker-compose.yml file.

$ mkdir Metabase && cd Metabase/ && nano docker-compose.yml
version: "3"
services:
postgres-db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: metabase
PGDATA: /var/lib/postgresql/data
volumes:
- /home/ubuntu/pgdata:/var/lib/postgresql/data
metabase-app:
image: metabase/metabase
restart: always
ports:
- 3000:3000
volumes:
- /home/ubuntu/metabase-data:/metabase-data
environment:
MB_DB_TYPE: postgres
MB_DB_DBNAME: metabase
MB_DB_PORT: 5432
MB_DB_USER: postgres
MB_DB_PASS: postgres
MB_DB_HOST: postgres-db
depends_on:
- postgres-db
links:
- postgres-db
$ sudo docker-compose up
<static-IP>:3000
$ sudo docker-compose up -d

Configuring a reverse-proxy server and setting up a Subdomain

For performance, security and reliability, we want to use a much more robust web server than our Metabase container’s web server. More specifically, we want to put a much more reliable server between the internet and our Metabase container. We will use nginx.

$ sudo apt-get update && sudo apt-get install nginx && sudo systemctl start nginx && sudo systemctl enable nginx && sudo systemctl status nginx
$ sudo unlink /etc/nginx/sites-enabled/default
$ sudo nano /etc/nginx/sites-available/metabase.conf
server {listen 80;
location / {
proxy_pass http://localhost:3000/; }}
$ sudo ln -s /etc/nginx/sites-available/metabase.conf /etc/nginx/sites-enabled/metabase.conf
$ sudo service nginx configtest && sudo service nginx restart
$ cat /var/log/nginx/error.log
server {
listen 80;
location {
proxy_pass http://localhost:3000/;
}
}
$ cat /var/log/nginx/error.log 
10 2021/05/18 14:42:46 [emerg] 64343#64343: invalid number of arguments in "location" directive in /etc/nginx/sites-enabled/metabase.conf:3
$ sudo rm /etc/nginx/sites-enabled/metabase.conf
$ sudo ln -s /etc/nginx/sites-available/metabase.conf /etc/nginx/sites-enabled/metabase.conf
$ sudo service nginx configtest && sudo service nginx restart

Securing Metabase with an SSL Certificate

  1. Run the following command to install Cerbot.
$ sudo apt update && sudo apt install certbot python3-certbot-nginx
server_name <metabase-domain>;
$ sudo rm /etc/nginx/sites-enabled/metabase.conf
$ sudo ln -s /etc/nginx/sites-available/metabase.conf /etc/nginx/sites-enabled/metabase.conf
$ sudo certbot --nginx -d <metabase-domain>
$ sudo ufw allow 'Nginx HTTPS' && sudo ufw deny 'Nginx HTTP' && sudo ufw allow 'Nginx Full' && sudo ufw enable && sudo ufw status

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store