This guide assumes you are running on macOS or Linux. Before starting please install the following:
- NodeJS
- git
- AWS CLI v2
- Visual Studio Code or any text editor that can handle markdown
Getting a copy of this project
Once you have the above installed, open your command line
(terminal
on macOS) and run the following commands:
cd ~/Documents # This is where the project will be downloaded too
git clone git@github.com:pfych/md-to-site.git
Once git
has finished downloading the project run the
following commands:
cd md-to-site
npm install
Once npm
has finished running. Open Visual Studio Code,
and then open the md-to-site
folder.
Creating a post or page
Inside Visual Studio Code, there is a list of files and folders
displayed on the left. All md files inside the content
folder are what will be compiled into your website. Delete all of them,
since by default the project comes with this site you’re reading as its
example files.
Create a file called index.md
inside
content
, an example file might look like the following:
---
title: My Website
---
Hello welcome to my website
## About Me heading
This is stuff about me
### Smaller Heading
This is content under a smaller heading
## My Posts
- [Example post](posts/example.md)
You can create a folder inside content
called
posts
(or anything you’d like) and create a file called
example.md
. It could contain the following:
---
title: This is an example post
summary: Hello world!
---
Hello, this is an example post by me!
There are two spaces after the previous line so this wraps inside the same paragraph!
There is a blank line before this line, so it's a new paragraph!
You can view a list of variables (title, image, summary, etc.) the
default template.html
uses here.
Inside index.md
we linked to this post with the
following syntax:
[This is the link label](posts/example.md)
Once you’ve written your content you can compile it with the following command:
npm run build
To view the site locally, once you’ve run the above command you can run the following:
npm run serve
Your site should now be viewable on your local computer at http://127.0.0.1:8080.
Deploying the site to AWS
We use a toolkit called “Serverless Framework” to automatically deploy and manage a server on Amazon Web Services (AWS). To use it you need an AWS account.
Creating an AWS Account
- Navigate to the AWS homepage
- Click “Create an AWS Account” in the top right of the page
- Enter your email address and pick an account name
- Click verify email address and enter the code sent through to you
- Pick a secure password! I highly recommend making this 16+ characters long with numbers and special characters. Make sure to write this down somewhere!
- Once this is done, select “Personal account” and fill out your details
- Enter your credit card details
- Enter your mobile number again to receive a text confirmation code
- Select the Basic Support free tier
You should now be re-prompted to log in.
- Select “Root user” and enter your email address.
- You’ll then be prompted to enter your password.
Creating AWS Credentials
To deploy your website you’ll need to create AWS IAM Access Keys.
- From the AWS Console Homepage
- In the search bar at the top of the page search for
IAM
- Select “IAM” with the subtitle of “Manage access to AWS resources”
- In the sidebar on the left click “Users”
- Click “Add users”
- Enter a Username (Like
Deploy
, orCLI
) - Check “Access Key”
- Click “Next: Permissions”
- Click “Attach existing policies directly”
- Check “AdministratorAccess”
- Click “Next: Tags”
- Click “Next: Review”
- Click “Create user”
- Click “Download .csv”
Open this CSV in any spreadsheet software or a text editor, keep this file safe as it gives anybody Admin access to your AWS account!
Inside your command line run the following command, it will ask you
for the Key ID and the Access Key from the CSV you just downloaded, when
prompted for region, enter the AWS region closest to you. Mine is
ap-southeast-2
, Amazons Sydney data-center.
aws configure
If you are not interested in having a separate testing environment you can skip the first command. To deploy your site run the following:
npm run deploy:dev # Deploy to "development"
npm run deploy:prod # Deploy to "production" (Live site)
Those commands will take up to 5 minutes to run however subsequent runs will be near instant.
You can run the following commands to see the Automatically assigned domain name:
npm run info:dev # Get domain for "development"
npm run info:prod # Get domain for "production"
Setting a custom domain name (Optional)
Domain Names are handled through AWS Route53, this service costs $0.50/month per domain, however it includes an SSL certificate & other convenient tools.
This is the most involved step, and I am looking at automating as much of it as I can however for now you’ll need to follow these steps.
Adding the domain to AWS
You can either purchase a domain through Amazon or BYO domain, the following steps are for a domain purchased through an external service:
- From the AWS Console Homepage
- In the search bar at the top of the page search for
Route53
- Click “Hosted Zones” in the sidebar on the left
- Click “Create hosted zone”
- Enter your Domain Name
- Ensure the Type is set to “Public”
- Click “Create hosted zone”
- Open the “Hosted zone details” dropdown
- Outside on AWS, in your domain providers dashboard. Change the
Nameserver records to the 4 values under “Name Servers” in AWS.
- WARNING: this will replace your existing records, ensure you copy over all values from your old name servers to Route53 before doing this!
It may take up to 24 hours for these records to change globally, but I’ve seen it update in less than an hour.
Adding the domain to your Distribution
- From the AWS Console Homepage
- In the search bar at the top of the page search for
Cloudfront
- Select the distribution with the origin
md-to-side-prod
in its name - Under “Settings” click “Edit”
- Under “Alternate domain name” add the following (but swap for your
domain)
www.example.com
example.com
- Under “Custom SSL certificate” click “Request certificate”
- Select “Request a public certificate” and click “Next”
- Under “Fully qualified domain name” enter the following (but swap
for your domain)
www.example.com
example.com
- Ensure “DNS validation” is selected
- Click Request
- In the banners up the top, click “View Certificate”
- Under “Domains” click “Create records in Route 53”
- Click “Create records”
- Keep refreshing every few minutes until the status is “Issued”
- Go back to the Cloudfront tab
- Next to the “Custom SSL Certificate” dropdown click refresh
- Select your new Certificate from the dropdown
- Click Save changes
Pointing your domain to your Distribution
- Navigate to your domains Hosted zone you created earlier in Route53
- Click “Create Record”
- If not in the Wizard click “Switch to Wizard”
- Select “Simple Routing”
- Click “Next”
- Click “Define simple record”
- Leave subdomain empty
- Set Record type to
A
(should be the default) - Under “Value/Route traffic to select”Alias to a Cloudfront distribution”
- Select your distribution bellow
- Click Define simple record
- Repeat steps 6 - 10 but set the subdomain value to
www
- Click Create records
It may take up to 24 hours for these records to change globally, but I’ve seen it go live in less than an hour.