Covid Tracker is a web application built using Angular v11.0.0 to track COVID-19
cases around the world.
This application consumes API https://corona.lmao.ninja/v2/countries from covid-19 global stats and hosted on AWS S3 uses CloudFront, Certificate Manager and Route 53.
There are 2 ways you can launch the application :
-
Application is hosted on
Github Pages
, you can launch the application on your browser directly, by clicking here. -
Launch the application on StackBlitz, by clicking here. StackBlitz is a online code editor built on top of vscode editor, where you can edit and see your changes on the fly.
Table of contents:
- Prerequisites and Installation
- Dependencies and Libraries
- Quick Start
- Host the Application on AWS S3 using AWS CLI
- Register and Configure domain
- Create SSL certificate using AWS Certificate Manager
- Configuring CloudFront
- Adding A Record to Route 53 Hosted Zone
- Terraform to Setup Infrastructure
- Publish Angular Application to Github Pages
- Status and Issues
-
Create a free-tier account in AWS.
-
Create an
IAM
user with AdministratorAccess policy and AmazonS3FullAccess policy. It is highly recommended not to useRoot
account. -
Install Python v3.9.x
-
Configure AWS CLI. To configure the AWS CLI you’ll need the following from your AWS account :
-
Access Key Id
-
Secret Key
-
Default AWS region
-
Library | Version | Notes |
---|---|---|
Node | 12.13.x | Recommended NodeJS version |
NPM | 6.12.x | Recommended NPM version |
Angular | 11.x.x. | JavaScript-based open-source front-end SPA framework |
Python | ~3.9.x | programming language |
awscli | ~2.x.x | AWS Command Line Interface |
- Clone repository and enter it
git clone https://github.com/kumaran-is/covid-tracker.git
cd covid-tracker
- Install NPM and Bower dependencies
npm install
- Run the application locally. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.
ng serve --o
- Run below command to create a bucket in S3. This command creates a bucket in
us-east-1
AWS region. 👉 Make sure to replacebucket_name
with your ownyourdomainname
andregion
with the region that you want to create the bucket in the package.json scriptsaws-cb
. 👉 Note: Bucket name must be unique across all existing bucket names in Amazon S3. The bucket name should be same as domain or subdomain. Example something likeyourdomainname.com
ordev.yourdomainname.com
.
npm run aws-cb
- Run below command to enable S3 for static web hosting. 👉 Make sure to replace
bucket_name
with your ownyourdomainname
in package.json scriptaws-website
npm run aws-website
- To make your hosted Angular app available to the public, all objects in the S3 bucket need to be publicly accessible. Go to your bucket in AWS console and create a bucket policy for you S3 bucket as mentioned below. 👉 Make sure to replace
bucket_name
with your ownyourdomainname
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucketname.com/*"
}
]
}
or To avoid going to AWS console and creating the bucket policy manually, run the below command. 👉 Make sure to replace bucket_name
with your own yourdomainname
in the Policy file and in package.json script aws-b-policy
.
npm run aws-b-policy
- Run below command to build and upload the angular application to S3 bucket that you just created. 👉 Note: Make sure to replace
bucket_name
with your ownyourdomain.com
in package.json scriptaws-deploy
npm run aws-deploy
- Verify the setup, by launching the application using Amazon S3 website endpoints <http://yourdomain.com.s3-website-.amazonaws.com>
-
You can register a domain using Route53. Once Domain is registered, it will automatically creates a
Hosted Zones
withNS
Type andSOA
record. -
To host your website on
www
, create a another bucketwww.yourdomain.com
. This can be done by running below command. 👉 Note: Make sure to replacebucket_name
with your ownwww.yourdomain.com
in package.json scriptaws-cb-www
npm run aws-cb-www
-
Go to your AWS S3 console, select the bucket
www.yourdomain.com
, click on properties, and enable “Static website hosting”. Select “Redirect requests”, enteryourdomain.com
as the Host Name and set “Protocol” to https and click Save. 👉 Note: No need to upload Angular application files to this bucketwww.yourdomain.com
, S3 “Redirect requests” will redirect all request internally toyourdomain.com
bucket. -
To make your hosted Angular app (www.yourdomain.com) available to the public, all objects in the S3 bucket need to be publicly accessible. Go to your bucket in AWS console and create a bucket policy for you S3 bucket as mentioned below. 👉 Make sure to replace
bucket_name
with your ownyourdomainname
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::www.bucketname.com/*"
}
]
}
or To avoid going to AWS console and creating the bucket policy manually, run the below command. 👉 Make sure to replace bucket_name
with your own www.yourdomainname
in the Policy file and in package.json script aws-b-policy-www
.
npm run aws-b-policy-www
- Verify the setup, by launching the application using
www
version of Amazon S3 website endpoints <http://www.yourdomainname.s3-website-.amazonaws.com>
-
Go to AWS console and navigate to
AWS Certificate Manager
. -
Click
Request a certificate
button and add two entries under “Add domain names”, one forwww.yourdomain.com
and another one foryourdomain.com
-
Choose the validation method as
DNS validation
. During theDNS Validation
, click add toCNAME
, it will create 2CNAME
records ( one forwww.yourdomain.com
and another one foryourdomain.com
) for certificate in theRoute53 Hosted Zone
. It will take few minutes to issue certificate
-
👉 Amazon S3 website endpoints do not support HTTPS. We need to setup either
CloudFront
orELB
for website to support HTTPS.CloudFront
is an AWS content delivery network (CDN) -
Go to AWS console and navigate to
CloudFront
, clickCreate Distribution
and chooseWeb
distribution and select “Get Started.” -
In the input form field for “Origin Domain Name” some auto-complete options will appear and you will see your bucket listed 👉 but do not select your bucket REST end point <yourdomain.com.s3.amazonaws.com>. Instead enter your Amazon S3 website endpoint <http://yourdomainname.s3-website-.amazonaws.com> as the Origin Domain Name. This is very important because selecting bucket REST end point <yourdomain.com.s3.amazonaws.com> from the drop-down list can lead to issues with CloudFront redirecting to your bucket’s endpoint URL instead of forwarding.
-
Under
Default Cache Behavior Settings
, radio button fieldViewer Protocol Policy
selectRedirect HTTP to HTTPS
to ensure all users utilize secure connections. -
Under
Distribution Settings
, fromPrice Class
dropdown choose the regions you want your CDN to support serving website from edge locations. -
Under
Distribution Settings
, in the textarea fieldAlternate Domain Names (CNAMEs)
enter your domains(www.yourdomain.com and yourdomain.com), one after another (one in each row). -
You can leave the rest of the fields and click
Create Distribution
button. AWS may take few minutes to create your new distribution.
-
We have
CloudFront
configured for static website hosting on S3 bucket. Now we need to map theyourdomain.com
andwww.yourdomain.com
toCloudFront
by creating twoA
alias records in the Route 53 hosted zone. -
Create a record, with record type
A
for hostwww.yourdomain.com
usingsimple routing
policy withvalue/Route traffic
toAlias to CloudFront distribution
and choose corrsponding CloudFront distribution name. -
Similarly create a record, with record type
A
for hostyourdomain.com
usingsimple routing
policy withvalue/Route traffic
toAlias to CloudFront distribution
and choose corresponding CloudFront distribution name. It will take few minutes to issue certificate -
It will take few minutes for Route 53 to complete the process. Now launch the application by navigating to www.yourdomain.com or https://www.yourdomain.com
-
Instead of using
AWS CLI
andAWS console
to setup and configureS3 bucket
,CloudFront
,Route 53
andSSL certificate via AWS Certificate Manager
, we can replicate same infrastructure again and again using Terraform -
Coming soon.
Github Pages
is a Github feature that allows you to deploy any static website or web application or Angular application from your gh-pages
branch to Github Pages
for free
Using angular-cli-ghpages library, you can easily publish your angular application to Github Pages
.
-
Install angular-cli-ghpages as a devDependency to your project
npm install angular-cli-ghpages --save-dev
-
Add below npm script to your
package.json
"scripts": { .... ..... "prod-build": "ng build --prod --base-href=./", "git-publish": "npm run prod-build && npx angular-cli-ghpages --dir=dist/covid-tracker --branch=gh-pages" }
-
Publish your application from
gh-pages
branch toGithub Pages
. Branchgh-pages
is automatically created for you. Command below, first runs production build, automatically commits and pushes the changes from 'dist' folder togh-pages
branch and deploys it toGithub Pages
. Branchgh-pages
act as a staging folder for deployment.npm run git-publish
-
Launch your application on browser by navigating to
https://username.github.io/repo-name/
. Example to launch this application click this link https://kumaran-is.github.io/covid-tracker -
For more detail and options, refer the official angular-cli-ghpages github.