This repo can be used to deploy an immutable Nightscout Docker container in t2.micro EC2 instance by using Terraform automation.
The Nightscout image is from my Docker Repo and it contains modified Nightscout version 14.2.6 for easy AWS deployment.
Terraform CLI >= v1.3.1 installed on your workstation.
MongoDB instance with M0 sandbox (The Free tier)
- Clone this repo locally to a folder of your choice
git clone https://github.com/51r/terraform-aws-nightscout.git
- Make sure you are in the main directory of the repo:
cd terraform-aws-nightscout
- Make sure you have allowed Terraform to access your IAM user credentials, set your AWS access key ID as an environment variable
export AWS_ACCESS_KEY_ID="<YOUR_AWS_ACCESS_KEY_ID>"
- Then set your secret key:
export AWS_SECRET_ACCESS_KEY="<YOUR_AWS_SECRET_ACCESS_KEY>"
Mandatory:
Modify the init.sh script file:
-
Under #Configuring Nginx reverse proxy, you have to specify your domain:
server_name cgm.example.com;
-
Add your MongoDB_URI, MongoDB_DB name and your API_Secret to the docker command:
docker run -e "MONGODB_URI=your-mongodb-URI" -e "MONGODB_DB=your-database-name" -e "API_SECRET=your-api-secret" --name 51r-nightscout -p 1337:1337 -d 51rspasov/cgm-nightscout
The MONGODB_URI should be something similar:
"MONGODB_URI=mongodb+srv://user:[email protected]/database-name?retryWrites=true&w=majority"
IMPORTANT:
In case you want to use it with Shuggah, you will need to issue an SSL certificate, which can be done for free by LetsEncrypt. I have included a script in the configuration, that automatically issues a certificate after 5 minutes (300 seconds). You only need to replace your-domain (e.g. domain.com) in the command in the init.sh script file :
(sleep 300; echo 'Triggering certbot' ; sudo certbot --nginx --register-unsafely-without-email --agree-tos -n -d your-domain) &
If you need more time, you can adjust the sleep command to more or fewer seconds. I have configured my subdomain TTL to be 1 minute, and it propagates for less than 5 minutes and it is enough for me.
Optional:
If you wish you can modify the region in which the EC2 instance will be deployed. I have used eu-central-1 (Frankfurt) as it is closest to me. Keep in mind that if you change the AWS Region, you will need to supply a new AMI, that is available in the region.
- Initialize the Terraform:
terraform init
You should see the following message:
Terraform has been successfully initialized!
- Apply the plan which terraform is going to execute based on our configuration
terraform apply
- Terraform will output your Public IP. Use it to enter it as an A record to your DNS records, you have 5 minutes to do it, since the LE certbot will be triggered and will issue the certificate, so you can use the Nightscout over HTTPS(443 port).
Do not hesitate to open an issue in the repo if you have questions or you have troubles with the installation.