Mini project that allows you to run a server that will receive webhooks.
When the AMI image is launched, the EC2 instance will not have docker installed by default. There are several ways to get docker running on EC2:
- Manually installing docker and docker compose after connecting to the EC2 instance via SSH.
- Automatically installing docker and docker compose when the EC2 instance boots up for the first time via
userdata
. - Creating custom AMI that will have docker and docker compose installed and running the instance through the created AMI.
In this project I first used the first option to test everything manually.
Once I had a working EC2 instance that was able to download and run docker images I wrote a script which I then placed in userdata
.
- I first installed certbot using this tutorial. I used the option to install certbot via python3 because EC2 instances already have python3 installed by default.
- I then generated a TLS certificate using the
sudo certbot certonly --manual
command. - I then updated the docker configuration to mount the generated certificates under path I choose in the container.
- Finally, I updated the server configuration so that TLS encryption was used during the connection establishment.
Real TLS certificates do not live in the live
folder, but in the archive
folder. Files present in the live
folder are links to files in the archive
folder. To be able to read the certificate files in the Node.js application, I had to mount the entire letsencrypt
folder to the docker container running the application. I found the solution to the problem in this post.
In order to be able to mount the /etc/letsencrypt
folder I had to change the user that owns it from root
to ec2-user
. I did this using the sudo chown -R ec2-user:ec2-user /etc/letsencrypt
command.
- 10 best practices to containerize Node.js application.
- Article explaining why I should always set
NODE_ENV
variable toproduction
. - Best practices for building docker images from Node.js docker working group.
- How to use docker on AWS EC2.
- Official docker documentation about installing docker compose on linux operating system.