The following is specific to MacOS users, however similar guides may exist for Linux, or this can be adapted accordingly.
In order to facilitate easier management of a local copy of a website, private github repository, and S3 bucket with Lambda functions as a website we will make use of the serverless framework.
You may need to check you have the latest XCode dev environment installed. You can install it via the App Store.
Your default nodejs on a Mac may be outdated for a serverless install.
$ node --version
v0.10.12
You can update to the latest nodejs environment on a macos using the n package as per instructions found here:
Open Terminal.app and type:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
OR
sudo n lts
- Note that
stable
can be exchanged forlatest
,lts
(long term support) or any specific version number such as0.10.20
. - If the version number doesn't show up when typing node -v, you might have to reboot.
$ node -v
v11.10.1
Then to install serverless run
sudo npm install -g serverless
And you should see output like
$ sudo npm install -g serverless
Password:
/usr/local/bin/slss -> /usr/local/lib/node_modules/serverless/bin/serverless
/usr/local/bin/serverless -> /usr/local/lib/node_modules/serverless/bin/serverless
/usr/local/bin/sls -> /usr/local/lib/node_modules/serverless/bin/serverless
> [email protected] postinstall /usr/local/lib/node_modules/serverless/node_modules/spawn-sync
> node postinstall
> [email protected] postinstall /usr/local/lib/node_modules/serverless
> node ./scripts/postinstall.js
┌────────────────────────────────────────────────────────────┐
│ serverless update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) /Users/USERNAME/.config│
└────────────────────────────────────────────────────────────┘
+ [email protected]
added 386 packages from 261 contributors in 29.713s
Follow the suggestion and run
sudo chown -R $USER:$(id -gn $USER) /Users/USERNAME/.config
And test the serverless install by running serverless
$ serverless
Commands
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--verbose" to this command to get in-depth plugin info
* Pass "--no-color" to disable CLI colors
* Pass "--help" after any <command> for contextual help
Framework
* Documentation: https://serverless.com/framework/docs/
config ........................ Configure Serverless
config credentials ............ Configures a new provider profile for the Serverless Framework
create ........................ Create new Serverless service
install ....................... Install a Serverless service from GitHub or a plugin from the Serverless registry
package ....................... Packages a Serverless service
deploy ........................ Deploy a Serverless service
deploy function ............... Deploy a single function from the service
deploy list ................... List deployed version of your Serverless Service
deploy list functions ......... List all the deployed functions and their versions
invoke ........................ Invoke a deployed function
invoke local .................. Invoke function locally
info .......................... Display information about the service
logs .......................... Output the logs of a deployed function
metrics ....................... Show metrics for a specific function
print ......................... Print your compiled and resolved config file
remove ........................ Remove Serverless service and all resources
rollback ...................... Rollback the Serverless service to a specific deployment
rollback function ............. Rollback the function to the previous version
slstats ....................... Enable or disable stats
plugin ........................ Plugin management for Serverless
plugin install ................ Install and add a plugin to your service
plugin uninstall .............. Uninstall and remove a plugin from your service
plugin list ................... Lists all available plugins
plugin search ................. Search for plugins
Plugins
AwsConfigCredentials, Config, Create, Deploy, Info, Install, Invoke, Logs, Metrics, Package, Plugin, PluginInstall, PluginList, PluginSearch, PluginUninstall, Print, Remove, Rollback, SlStats
And to check the version:
$ serverless --version
1.38.0
Next we need to setup AWS credentials, which is covered in the documentation and this video.
Login to AWS console and search for IAM.
Select Create a new user, and set the username as serverless, with programatic access but not console access and click Next: permissions
.
Select attach existing policies directly
and choose AdministratorAccess
and select Next: Tags
and add any if applicable. Click Next: Review
to finalise.
If happy with everything continue to obtain your access credentials. We need these to update serverless configuration.
serverless config credentials --provider aws --key RANDOMCHARS --secret LONGSTRINGRANDOMCHARS
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "default" profile.
You might be interested in credentials for multiple environments which can be seen here
Then to get a project started change directory to you upper level directory you wish to create the serverless files in, and run
serverless create --template aws-nodejs --path mySite
Thereafter it's a matter of following the documentation to proceed to next steps for setting up a static site generator, and implementing code for Lambda functions.
You can install Go
via brew
but we'll install the binary for mac instead. You can download it here.
Run the downloaded .pkg file and follow the prompts. Make sure you install for all users.
Your go path environment can be accessed with go env
in Terminal.app
and look for the GOPATH bit, but this directory might not exist.
Create it with the following command:
mkdir -p $HOME/go
Along with a binary and source directory
mkdir -p $HOME/go/bin
mkdir -p $HOME/go/src
Also make sure you setup your .profile correctly with
# add go paths
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
and open a fresh terminal to take advantage of the changes.
Hugo is a templating engine which we can utilise to build the static website component according to a configurable theme, and then upload to Github and trigger and update of the S3 bucket to update the public website.
We will be installing from source, having also installed Go
via the downloaded binary.
cd $GPATH/src/
git clone https://github.com/gohugoio/hugo.git
cd hugo
mkdir -p src/github.com/gohugoio
ln -sf $(pwd) src/github.com/gohugoio/hugo
# set the build path for Go
export GOPATH=$GOPATH/bin
go get
This will fetch a lot of dependancies and take a little while to complete. To build Hugo
proceed with:
go build -o hugo main.go
You will find the binary in the directory you're in and also in $GOPATH/bin/
Next we need to change directory to where our website is going to live and run Hugo
to create the frameworks.
cd /parent/folder
hugo new site nameofsite
In the folder /parent/folder/nameofsite
will be all the files.
In order to launch a local instance of a live webserver run
hugo server
and open http://localhost:1313/ to view the website
However there will be a blank page because no content has been added.
Select a theme at https://themes.gohugo.io and follow the documentation for more info on enabling it.
To install a theme
cd themes
git clone <theme-url>
Your theme content will be in /parent/folder/nameofsite/themename/
Next you need to setup a configuration file config.toml
to load the theme and show your site.
This should be located in /parent/folder/nameofsite/config.toml
Open in your choice of editor and copy the sample configuration from your theme and modify to your needs.
To check it running, in terminal:
cd /parent/folder/nameofsite
hugo server
and open http://localhost:1313/ to view the website
to-do: add theme stuff
cd /parent/folder/nameofsite
hugo new post/name-of-post.md
Back in file browser you can see in the directory /parent/folder/nameofsite/content/posts/name-of-post.md
file. You can edit this file with markdown text to add content to this page.
Note, this is theme-dependent and this sort of layout for posts may be specific to a chosen theme.
Editing the .md file:
---
title: "My post"
date:
draft: false:
image: "filename.jpg"
---
Upload image to static/filename.jpg or uploads/filename.jpg
to-do: add more to posts
Hugo & Lambda