-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding new chapters for extension * Adding drafts for more new chapters * - Add billing form - Connect billing form * - Automating react deployments - Create a build script - Set up on Netlify - Custom domains on Netlify - Frontend workflow * Adding screenshots for new chapters * Add screenshots monitoring deployments * Adding screenshots for Netlify chapters * Tweaking about page. Editing new chapters. * Editing new chapters * - Styling ToC - Creating two col layout for posts with ToC sections * Styling new lander. Adding recent updates to data and loading from it. * Adding links to forums and styling lander * Tweaking context * Updating to new code branches * Tweaking print css * Updating screenshot for issues page * Switching to new forums link * Tweaking chapter links
- Loading branch information
Showing
236 changed files
with
4,333 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
layout: home | ||
layout: page | ||
title: Page not found | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,13 @@ | |
|
||
[Serverless Stack](https://serverless-stack.com) is an open source guide for building and deploying full-stack apps using Serverless and React on AWS. | ||
|
||
We are going to create a [note taking app](https://demo.serverless-stack.com) from scratch using React.js, AWS Lambda, API Gateway, DynamoDB, and Cognito. | ||
We are going to create a [note taking app](https://demo2.serverless-stack.com) from scratch using React.js, AWS Lambda, API Gateway, DynamoDB, and Cognito. | ||
|
||
![Demo App](assets/completed-app-desktop.png) | ||
|
||
It is a single-page React app powered by a serverless CRUD API. We also cover how add user authentication and handle file uploads. | ||
|
||
The entire guide is hosted on GitHub and we use [GitHub Issues][GHIssues] for our comments. With the help of the community we keep the guide accurate and up to date. | ||
The entire guide is hosted on GitHub and we use [Discourse][Discourse] for our comments. With the help of the community we keep the guide accurate and up to date. | ||
|
||
#### Source for the Demo App | ||
|
||
|
@@ -37,8 +37,9 @@ The entire guide is hosted on GitHub and we use [GitHub Issues][GHIssues] for ou | |
|
||
#### Getting Help | ||
|
||
- If you are running into issues with a specific chapter, post in the comments for that [chapter][GHIssues]. | ||
- Open a [new issue](../../issues/new) if you've found a bug or have some suggestions. | ||
- If you are running into issues with a specific chapter, post in the comments for that [chapter][Discourse]. | ||
- Open a [new issue](../../issues/new) if you've found a bug | ||
- Or if you have a suggestion create a [new topic][Discourse] in our forums | ||
- Send us an [email][Email] if you have any other questions or feedback. | ||
- If you've found a typo, edit the chapter and submit a [pull request][PR]. | ||
|
||
|
@@ -49,9 +50,9 @@ The entire guide is hosted on GitHub and we use [GitHub Issues][GHIssues] for ou | |
|
||
#### Maintainers | ||
|
||
Serverless Stack is authored and maintained by Frank Wang ([@fanjiewang](https://twitter.com/fanjiewang)) & Jay V ([@jayair](https://twitter.com/jayair)). [**Subscribe to our newsletter**](http://eepurl.com/cEaBlf) for updates on Serverless Stack. Send us an [email][Email] if you have any questions. | ||
Serverless Stack is authored and maintained by Frank Wang ([@fanjiewang](https://twitter.com/fanjiewang)) & Jay V ([@jayair](https://twitter.com/jayair)). [**Subscribe to our newsletter**](https://emailoctopus.com/lists/1c11b9a8-1500-11e8-a3c9-06b79b628af2/forms/subscribe) for updates on Serverless Stack. Send us an [email][Email] if you have any questions. | ||
|
||
|
||
[GHIssues]: ../../issues?q=is%3Aissue+is%3Aopen+label%3ADiscussion+sort%3Aupdated-desc | ||
[Discourse]: https://discourse.serverless-stack.com | ||
[PR]: ../../compare | ||
[Email]: mailto:[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
layout: post | ||
title: Add a Billing API | ||
date: 2018-03-07 00:00:00 | ||
description: We are going to create a Lambda function for our serverless billing API. It will take the Stripe token that is passed in from our app and use the Stripe JS SDK to process the payment. | ||
context: true | ||
comments_id: add-a-billing-api/170 | ||
--- | ||
|
||
Now let's get started with creating our billing API. It is going to take a Stripe token and the number of notes the user wants to store. | ||
|
||
### Add a Billing Lambda | ||
|
||
<img class="code-marker" src="/assets/s.png" />Start by installing the Stripe NPM package. Run the following in the root of our project. | ||
|
||
``` bash | ||
$ npm install --save stripe | ||
``` | ||
|
||
<img class="code-marker" src="/assets/s.png" />Next, add the following to `billing.js`. | ||
|
||
``` js | ||
import stripePackage from "stripe"; | ||
import { calculateCost } from "./libs/billing-lib"; | ||
import { success, failure } from "./libs/response-lib"; | ||
|
||
export async function main(event, context, callback) { | ||
const { storage, source } = JSON.parse(event.body); | ||
const amount = calculateCost(storage); | ||
const description = "Scratch charge"; | ||
|
||
// Load our secret key from the environment variables | ||
const stripe = stripePackage(process.env.stripeSecretKey); | ||
|
||
try { | ||
await stripe.charges.create({ | ||
source, | ||
amount, | ||
description, | ||
currency: "usd" | ||
}); | ||
callback(null, success({ status: true })); | ||
} catch (e) { | ||
callback(null, failure({ message: e.message })); | ||
} | ||
} | ||
``` | ||
|
||
Most of this is fairly straightforward but let's go over it quickly: | ||
|
||
- We get the `storage` and `source` from the request body. The `storage` variable is the number of notes the user would like to store in his account. And `source` is the Stripe token for the card that we are going to charge. | ||
|
||
- We are using a `calculateCost(storage)` function (that we are going to add soon) to figure out how much to charge a user based on the number of notes that are going to be stored. | ||
|
||
- We create a new Stripe object using our Stripe Secret key. We are going to get this as an environment variable. We do not want to put our secret keys in our code and commit that to Git. This is a security issue. | ||
|
||
- Finally, we use the `stripe.charges.create` method to charge the user and respond to the request if everything went through successfully. | ||
|
||
### Add the Business Logic | ||
|
||
Now let's implement our `calculateCost` method. This is primarily our *business logic*. | ||
|
||
<img class="code-marker" src="/assets/s.png" />Create a `libs/billing-lib.js` and add the following. | ||
|
||
``` js | ||
export function calculateCost(storage) { | ||
const rate = storage <= 10 | ||
? 4 | ||
: storage <= 100 | ||
? 2 | ||
: 1; | ||
|
||
return rate * storage * 100; | ||
} | ||
``` | ||
|
||
This is basically saying that if a user wants to store 10 or fewer notes, we'll charge them $4 per note. For 100 or fewer, we'll charge $2 and anything more than a 100 is $1 per note. Clearly, our serverless infrastructure might be cheap but our service isn't! | ||
|
||
### Configure the API Endpoint | ||
|
||
Let's add a reference to our new API and Lambda function. | ||
|
||
<img class="code-marker" src="/assets/s.png" />Add the following above the `resources:` block in the `serverless.yml`. | ||
|
||
``` yml | ||
billing: | ||
handler: billing.main | ||
events: | ||
- http: | ||
path: billing | ||
method: post | ||
cors: true | ||
authorizer: aws_iam | ||
``` | ||
Make sure this is indented correctly. | ||
### Commit Our Changes | ||
<img class="code-marker" src="/assets/s.png" />Let's quickly commit these to Git. | ||
``` bash | ||
$ git add . | ||
$ git commit -m "Adding a billing API" | ||
``` | ||
|
||
Now before we can test our API we need to load our Stripe secret key in our environment. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: post | ||
title: Add Stripe Keys to Config | ||
date: 2018-03-22 00:00:00 | ||
description: | ||
comments_id: add-stripe-keys-to-config/185 | ||
--- | ||
|
||
Back in the [Setup a Stripe account]({% link _chapters/setup-a-stripe-account.md %}) chapter, we had two keys in the Stripe console. The **Secret key** that we used in the backend and the **Publishable key**. The **Publishable key** is meant to be used in the frontend. | ||
|
||
We did not complete our Stripe account setup back then, so we don't have the live version of this key. For now we'll just assume that we have two versions of the same key. | ||
|
||
<img class="code-marker" src="/assets/s.png" />Add the following line in the `dev` block of `src/config.js`. | ||
|
||
``` | ||
STRIPE_KEY: "YOUR_STRIPE_DEV_PUBLIC_KEY", | ||
``` | ||
|
||
<img class="code-marker" src="/assets/s.png" />And this in the `prod` block of `src/config.js`. | ||
|
||
``` | ||
STRIPE_KEY: "YOUR_STRIPE_PROD_PUBLIC_KEY", | ||
``` | ||
|
||
Make sure to replace, `YOUR_STRIPE_DEV_PUBLIC_KEY` and `YOUR_STRIPE_PROD_PUBLIC_KEY` with the **Publishable key** from the [Setup a Stripe account]({% link _chapters/setup-a-stripe-account.md %}) chapter. For now they'll be the same. Just make sure to use the live version in the `prod` block when you configure your Stripe account completely. | ||
|
||
### Commit the Changes | ||
|
||
<img class="code-marker" src="/assets/s.png" />Let's quickly commit these to Git. | ||
|
||
``` bash | ||
$ git add . | ||
$ git commit -m "Adding Stripe keys to config" | ||
``` | ||
|
||
Next, we'll build our billing form. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
layout: post | ||
title: Automating React Deployments | ||
date: 2018-03-25 00:00:00 | ||
description: We want to automatically deploy our Create React App when we push any changes to our Git repository. To do this, we will need to set our project up on Netlify. | ||
context: true | ||
comments_id: automating-react-deployments/188 | ||
--- | ||
|
||
If you've followed along with the first part of this guide, you'll have noticed that we deployed our Create React App to S3 and used CloudFront as a CDN in front of it. Then we used Route 53 to configure our domain with it. We also had to configure the www version of our domain and this needed another S3 and CloudFront distribution. This process can be a bit cumbersome. | ||
|
||
In the next few chapters we are going to be using a service called [Netlify](https://www.netlify.com) to automate our deployments. It's a little like what we did for our serverless API backend. We'll configure it so that it'll deploy our React app when we push our changes to Git. However, there are a couple of subtle differences between the way we configure our backend and frontend deployments. | ||
|
||
1. Netlify hosts the React app on their infrastructure. In the case of our serverless API backend, it was hosted on our AWS account. | ||
|
||
2. Any changes that are pushed to our `master` branch will update the production version of our React app. This means that we'll need to use a slightly different workflow than our backend. We'll use a separate branch where we will do most of our development and only push to master once we are ready to update production. | ||
|
||
Just as in the case with our backend, we could use [Travis CI](https://travis-ci.org) or [Circle CI](https://circleci.com) for this but it can take a bit more configuration and we'll cover that in a different chapter. | ||
|
||
So let's get started with setting up your project on Netlify. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
layout: post | ||
title: Automating Serverless Deployments | ||
date: 2018-03-11 00:00:00 | ||
description: We would like to automatically deploy our Serverless Framework project when we commit any changes to our Git repository. To do this we are going to use a service called Seed (https://seed.run) to automate our serverless deployments. It will configure a CI/CD pipeline and setup our environments. | ||
context: true | ||
comments_id: automating-serverless-deployments/174 | ||
--- | ||
|
||
So to recap, this is what we have so far: | ||
|
||
- A serverless project that has all it's infrastructure completely configured in code | ||
- A way to handle secrets locally | ||
- And finally, a way to run unit tests to test our business logic | ||
|
||
All of this is neatly committed in a Git repo. | ||
|
||
Next we are going to use our Git repo to automate our deployments. This essentially means that we can deploy our entire project by simply pushing our changes to Git. This can be incredibly useful since you won't need to create any special scripts or configurations to deploy your code. You can also have multiple people on your team deploy with ease. | ||
|
||
Along with automating deployments, we are also going to look at working with multiple environments. We want to create clear separation between our production environment and our dev environment. We are going to create a workflow where we continually deploy to our dev (or any non-prod) environment. But we will be using a manual promotion step when we promote to production. We'll also look at configuring custom domains for APIs. | ||
|
||
For automating our serverless backend, we are going to be using a service called [Seed](https://seed.run). Full disclosure, we also built Seed. You can replace most of this section with a service like [Travis CI](https://travis-ci.org) or [Circle CI](https://circleci.com). It is a bit more cumbersome and needs some scripting but we might cover this in the future. | ||
|
||
Let's get started with setting up your project on Seed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.