PRAwN Stack
🦐 🍤 🦐 🍤 🦐 🍤 🦐 🍤 🦐 🍤 🦐 🍤
A modern page view counter to see how unpopular my project is. Powered by a PRAwN stack (Postgres, React, AWs and Node) in the free tier, deployed using CDK.
- Postgres.
- React.
- Aws.
- Node.
We're also using Typescript and NextJS for the frontend.
Website
Scripts
docker-compose up --buildstarts postgres, the node api development server and the nextjs development server.npm run startrun the development Node server.npm run devrun the NextJS development server.npm run testperform the jest unit tests.npm run buildbuild the NextJS frontend so it's ready to deploy.npx aws-cdk bootstrap --profile account-nameprepare the AWS region for cdk deployments.npm run deploy -- --profile account-namebuild the frontend and deploy the cdk stack.npx aws-cdk destroy --profile account-namedestroy the deployment.
Running Locally
- Install dependencies.
- Run docker-compose.
This will bring up:
docker-compose up --build- Postgres database and run migrations on it using Flyway.
- PgAdmin to access the Postgres database. Available at http://localhost:5050/.
- Node API development server. Available at http://localhost:3001/api/.
- NextJS development server for the frontend. Available at [http://localhost:3000](http://localhost:3000.
Going forward, you can make this faster by skipping the build step. You only need it if your dependencies change.
Accessing the Local Database
- Go to http://localhost:5050/.
- Set a master password.
- Click Add New Server.
- Fill in the local server details.
- General
- Name: local
- Connection
- Host: postgres
- Username: postgres
- Password: changeme
- Save password?: yes
- General
First Deploy
Make sure you setup email forwarding for admin @yourdomain (e.g admin@cadell.dev) you receive the email from aws to validate that you own the domain so it can create a certificate.
- Create an AWS account.
- Click Signin.
- Click Create a new AWS account.
- Enter your details.
- Go to IAM.
- Enable MFA on your Root account. I recommend 1Password.
- Create an IAM user without console access, assign the AdministratorAccess permission and setup an access key.
- Download the credentials.
- Create a
.awsfolder if you don't already have one. - Create a credential file with
vi ~/.aws/credentials.[{account-name}] aws_access_key_id={access_key} aws_secret_access_key={secret_key} - Run
npm installto install dependencies. - Run
npm run buildto build the project. - Set the region in
bin/prawn-cdk.ts. - Update
yourPublicIpAddressinbin/prawn-cdk.tswith your public ip address so you can access the database. - Run
npx aws-cdk bootstrap --profile {account-name}. - Run
npx aws-cdk deploy CertificateStack --profile {account-name}.- This will send an email to
admin@yourdomainto confirm you control the domain for the certificate it's creating. You can also go into CertificateManager inus-east-1region and resend it if you need to. - The command won't finish until you approve the email.
- This will send an email to
- Copy the certificateArn from the output and add it to the
bin/prawn-cdk.ts. - Run
npm run deploy -- --profile account-name.- This takes about about 15 mins.
Future Deploys
This takes about 1-3 mins.
npm run deploy --profile account-name
Access the Database
You'll need to do this to setup the database initially.
Access is currently through a whitelisted ip address which isn't ideal but will work well enough for now.
- Make sure the
yourPublicIpAddressinlib/prawn-stack.tsis up to date and deployed. - Login to the AWS console.
- Go to Secrets Manager.
- Select the region, probably
ap-southeast-2. - Click
PrawnStack-rds-credentials. - Go to the
Secret valuesection then clickRetrieve secret value.
You can put the values into PgAdmin to query the database.
Setup the Database
- Make sure Docker is running.
- Run flyway migrate with connection details and credentials from secrets manager.
HOST=xxx USER=xxx PASSWORD=xxx; docker-compose run flyway -url=jdbc:postgresql://$HOST/postgres -user=$USER -password=$PASSWORD migrate
Setup a Custom Domain
- Sign into your domain registrar. I use Google Domains.
- Setup a new CNAME DNS record using the cloudfront domain in the deploy output.
- Make sure the CNAME aligns with the
customDomaininbin/prawn-cdk.ts.
Destroy the Stack
npx aws-cdk destroy --profile account-name
Why is AWS Charging You?
- Login to the root account.
- On the account dropdown on the top right, click My Billing Dashboard.
- Click Cost Explorer.
- Click Daily Spend View.
- Change the timeframe to the last 7 days.
- Group by Usage Type.
The table below will give you a decent breakdown on your charges.