Simple Go serverless website on Amazon Web Services (AWS).
Demo: https://6epko5iya8.execute-api.ap-southeast-1.amazonaws.com/dev.
Website's frontend is written in VueJS, which stored on a public AWS S3 bucket.
There're two simple AWS Lambdas written in Go (deployed via apex with NodeJS shim runtime):
less_crawler: Crawls Github trending repositories, parses and persists data toDynamoDB. This function is triggered by aCloudWatchcron job.less_caterer: Receives HTTP request from client and lookup onDynamoDBfor trending repositories by day.
API Gateway serves two APIs, one to GET the index.html page on S3 bucket (forward/proxy request to the URL of index.html file on S3). The other allows client to lookup Gihub trending repositories by day (which calling the less_caterer lambda).
This repo contains the code for Lambda and S3 parts in the image below.
You have to install Go SDK, glide, AWS CLI, apex and configure the AWS credential to deploy your functions on AWS Lambda.
-
Clone the repository to your local
$GOPATHand install Go dependencies:$ go get github.com/lnquy/less $ cd $GOPATH/src/github.com/lnquy/less $ glide install
-
The AWS region is
ap-southeast-1, you have to take a look on thefunctionscode and change theawsRegionvalue to deploy on another region.
-
Create a role for the Lambdas on AWS IAM which have access to
DynamoDBservice. -
Change the role to match your IAM Lambda role: project.json#L4.
-
Deploy to AWS:
$ apex deploy
-
Create a public S3 bucket for static web hosting, get the URL of the bucket.
-
Change the
publicPathto your S3 bucket URL: webpack.config.js#L6 -
Change the
GetCatererto yourPOST /api/v1/trending/API URL: main.js#L22 -
Build frontend:
$ cd frontend $ npm install // or yarn $ npm run build -
Upload all
frontend/distfiles to the root of S3 bucket, make sure all files has public read permission. -
Note the HTTP URL to the
index.htmlfile.
- Create a
24_hoursinterval scheduled job. - Apply that job as the trigger of the
less_crawlerlambda.
-
Create a table with name of
less-crawler-devor anything you want, just make sure to change thedynamoTablevalue in Go code, too. -
Primary partition key:
date(String). -
Primary sort key:
sort(Number).
- Create a root
/GET API to forward/proxy the HTTP request to the URL ofindex.htmlfile on S3 bucket. - Create a
/api/v1/trendingPOST API which called theless_catererlambda. - You may have to allow the CORS permission on APIs, too.
- Deploy the APIs to a stage (E.g:
dev).
Open browser and follow the link to the GET / API, you now have a simple serverless website up and running on AWS. Congrats :)
This project is under the MIT License. See the LICENSE file for the full license text.