visit
When it comes to performance these days, it’s not uncommon to hear the term “JAMstack”. JAMstack stands for JavaScript, APIs, and Markup. The idea being to create static markup with JavaScript that is powered by communicating with APIs. The reason this stack has become so popular is because it allows websites or apps to be bundled up and served at “the edge” via a CDN. Couple this with what is ideally a performant app that is talking to performant APIs and you get a speedy user experience. is a great solution for creating markup with JavaScript, but what about the APIs part and serving your site to the world? Well really, that’s up to you, but in this post I’ll walk through how you can:
Amplify lets you create sophisticated serverless backends fast. The CLI includes support for authentication, analytics, functions, REST/GraphQL APIs, and . It uses AWS CloudFormation and enables you to add, modify, and share configurations.
Amplify Console is a continuous deployment and hosting service for mobile web applications. The AWS Amplify Console makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of simultaneously updating the frontend and backend of your applications.
AppSync makes it easy to build data driven mobile and web applications by handling securely all the application data management tasks like online and offline data access, data synchronization, and data manipulation across multiple data sources. AWS AppSync uses GraphQL, an API query language designed to build client applications by providing an intuitive and flexible syntax for describing their data requirement.
Example of dynamic blog
The first thing that needs to be done is making sure you have the gatsby-cli
package installed so that you can create a new Gatsby project.
You can view all available starters .Next you need to set up the client-side app where users can sign in and see what posts they have liked.
To create client-side only routes you first need to update gatsby-node.js
with the following snippet (for more on client-side only routes, head ):
This tells Gatsby that these routes should only be rendered on the client. While you don’t necessarily need this functionality in this example, if your dashboard had many routes this would prevent Gatsby from trying to render those pages, which would error since they don’t exist in the
pages
directory where Gatsby looks for pages to render.
Next you need to add a page for the dashboard. In src/pages
create a new file called dashboard.js
and add the following:
If you run yarn start
and visit [//localhost:8000/dashboard](//localhost:8000/dasboard)
you should see something that looks like this:
Once the CLI is installed you need to initialize Amplify in your Gatsby blog project. From the root of the project run amplify init
.
For a more in-depth walkthrough of creating a new user profile, check out this informative video from .
Now that Amplify is properly configured you can start setting up AWS services in your blog!
Once you run the command you will be asked if you want to use the default configuration to set up authentication for your app. This is the recommended auth settings for AWS. (Choose this option if you aren’t familiar with how to configure your own user pools and Cognito configuration.) Next you need to run amplify push
so that the resources can be created and configured in AWS.
npm i aws-amplify aws-amplify-react
# OR
yarn add aws-amplify aws-amplify-react
Next you need to import the configuration created by Amplify in src/aws-exports.js
and pass it to the Amplify client. In gatsby-browser.js
add the following:
Then you can update the dashboard with the “” HOC. This component takes care of the entire sign up flow for you! It’s a really great component, but if you need something more specific you can always create your own sign in flow and use the [Auth](//aws-amplify.github.io/docs/js/authentication)
module available in aws-amplify
package.
For an example of using the Auth module go .
Update src/pages/dashboard.js
with the following:
Once you restart Gatsby you should see the following when you visit [//localhost:8000/dashboard](//localhost:8000/dashboard)
:
withAuthenticator rendered by Gatsby
The last thing we want to do is add a link to the dashboard from all other pages. We can add that to the Layout
component. Notice in the updated Dashboard
component we are now passing in a isDashboard
prop. We can use this in the Layout
component to determine if we should render the dashboard link or not. Update the return statement in src/components/Layout.js
with the following:
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. —
To add an API
service to your blog run amplify add api
. You will be prompted with a few options:
Now replace the TODO
type with the following:
If you look at the type, you will notice there are two directives being used on the
PostLike
type. The first one ismodel
, this tells AWS that this is a data type and as such, will create a table for this entity. The second one isauth
, this directive allows us to set up granular authentication or access on our GraphQL API. Here we are specifying that only an owner can access their own PostLikes and the owner is determined by theuserId
value for that entity. Go for more about GraphQL transforms.
Once that is done, hit enter in the terminal and then run amplify push
again. You will have the option to have the necessary code generated to make queries and mutations, as well as subscriptions. I recommend letting Amplify create the code for you.
In order to be able to like a post you first need to add an id
field to the frontmatter
of each blog post. To do so, open the content/blog
folder and add an id
property to each blog post (I used incrementing numbers like 1, 2, 3, etc). It should look something like this:
Replace src/templates/blog-post.js
with the following:
Here we are using the API
module provided by aws-amplify
, but there is a Connect
component that comes in aws-amplify-react
as well. We’ll make use of that in the next section.
One thing to note is that we don’t have to specify the
userId
property in our mutation input. The reason is because AppSync will use the user that is found on the associated identity making the request. You can learn more about that .
So now that visitors can like your posts it would be nice for them to see all the posts they have liked. Let’s update src/pages/dashboard.js
with the following:
A list of liked posts rendered by Gatsby Sweet! Now that all the functionality is set up, the only thing left is to deploy this awesome likable blog!
Picking a source control provider in Amplify Console Then you can choose the repository and branch you wish to deploy:
Selecting a repository and branch in Amplify Console Amplify Console will auto-detect that this is a Gatsby project so for the next step about build configuration, just press “Next”! Lastly, review your settings and hit “Save and Deploy”:
Reviewing settings for the deployment in Amplify Console
Amplify Console will build the backend infrastructure needed that you have specified in your Amplify config and then it will build and deploy your static site across CloudFront. Amplify Console supports branch deployments, which means you can create an environment for any branch. This makes things like testing, or creating staging environments very easy. It also allows you to set up basic auth for any deployment, making it possible to keep things private and only give access to those who need it.Your blog is now live and ready for the whole world to enjoy! Congratulations! 🎊 🎉
The code for this example can be found , and the deployed blog can be found .
You can play with the code in CodeSandbox here:
If you enjoyed this post I highly recommend checking out some more of the amazing stuff you can do with Amplify and AppSync! You can find a bunch more awesome resources and .
Also, if you want to stay up on the latest things I’m writing about or working on, give me a follow on !