This folder contains a sample app for demonstration and educational purposes. You should follow the example in this code in your own apps and then remove this sample app once things are working. The app is built on top of Node.js, but the basic approach works with any technology.
This sample app shows how to:
- Load the config file for the current environment.
- Decrypt the secrets in the config file using gruntkms.
- Use Packer to package the app as an AMI. The AMI includes a script called
run-app.sh
. Terraform will configure each EC2 Instance to execute this script during boot (as part of User Data). - Apply migrations to the Postgres DB before booting.
If you're new to this infrastructure, Terraform, or AWS, make sure to start with the end-to-end Infrastructure Walkthrough Documentation.
cd app
npm install
node server.js
-
Install Packer.
-
Configure your AWS credentials using one of the options supported by the AWS SDK. Usually, the easiest option is to set the
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables. -
packer build packer/build.json
This app has self-signed TLS certificates in the tls
folder. This is used to ensure that all data in transit is
encrypted. There is one private/public key pair for each environment (e.g. stage, prod, etc).
If you are unfamiliar with how TLS certificates work, start with the Background documentation.
There are many ways to generate a certificate, but the easiest option is:
-
Use the private-tls-cert module to generate the certificates. If you are using an ELB or ALB, then you can use any IP addresses or DNS names you wish, as the AWS load balancers will not check. You may want to include
127.0.0.1
andlocalhost
in the cert to make local testing easier. -
Encrypt the private key using gruntkms with the KMS master key for the appropriate environment.
-
Package the TLS cert with the app (the
Dockerfile
already does this). -
Use
gruntkms
to decrypt the private key just before the app boots (therun-app.sh
script already does this).
In order to talk to RDS over SSL, we need a CA certificate we can use to verify that we're actually talking to RDS (see
Using SSL to Encrypt a Connection to a DB
Instance). We've downloaded this file
from AWS and stored it under tls/rds-ca-2015-root.pem
.