Skip to content

Commit d0e0bd1

Browse files
Update amplifycli_react_tutorial.md
1 parent 80db3f3 commit d0e0bd1

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

amplifycli_react_tutorial.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ This tutorial will walk you through using the AWS Amplify CLI with a React appli
44

55

66
# Installation
7-
- An AWS Account and credentials are necessary. See [LINK]().
7+
8+
**NOTE** The steps below will change slightly once the CLI is released publicly.
9+
10+
If you're using Windows, we recommend the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10).
11+
12+
- An AWS Account and credentials are necessary. If you're unfamiliar with finding credentials see the following [LINK](https://docs.aws.amazon.com/aws-mobile/latest/developerguide/aws-mobile-cli-credentials.html).
813
- Ensure you have NodeJS installed
914
- Examples below use `yarn` but `npm` works as well.
10-
- Download [LINK](https://amplify-cli-beta-3dd89264-8c7c-11e8-9eb6-529269fb1459.s3.amazonaws.com/amplify-cli-0.1.0.tgz?AWSAccessKeyId=AKIAIFBGMKZSDB7FRP7A&Signature=l8ieJ9wqXDJN8TJ02D63Taxmgjo%3D&Expires=1533366622)
11-
- Navigate into the unpacked directory and run `npm run setup-dev`
15+
- Download the Amplify CLI from [HERE](https://s3.amazonaws.com/amplify-cli-beta-3dd89264-8c7c-11e8-9eb6-529269fb1459/amplify-cli-0.1.0.tgz)
16+
- Navigate into the unpacked directory (usually called `package`) and run `npm run setup-dev`
1217
- Ensure you have [Create React App](https://github.com/facebook/create-react-app) installed and create a new project:
1318
```
1419
yarn create-react-app -g
@@ -39,7 +44,7 @@ You can see the changes are not deployed by running `amplify status` and then si
3944

4045
# Add User Registration and Sign-In
4146

42-
Now that your app is in the cloud, you can add some features like allowing users to register for your site and login. Run `amplify add auth` and select the **Default**
47+
Now that your app is in the cloud, you can add some features like allowing users to register for your site and login. Run `amplify add auth` and select the **Default configuration**.
4348

4449
Next, add the Amplify library to your application:
4550
```
@@ -70,7 +75,7 @@ A login screen is nice but now is the time to add some features, like tracking a
7075
Edit your `App.js` file in the React project again and modify your imports so that the `Analytics` and `Storage` categories are included as well as the `S3Album` component, which will be used for uploading and downloading photos.
7176

7277
```
73-
import { Analytics, Storage } from 'aws-amplify';
78+
import Amplify, { Analytics, Storage } from 'aws-amplify';
7479
import { withAuthenticator, S3Album } from 'aws-amplify-react';
7580
```
7681

@@ -108,27 +113,28 @@ Finally, modify the `render` method so that you can upload files and also view a
108113
);
109114
}
110115
```
111-
Refresh the web page manually to see the uploaded image in your app.
112116

113-
Save your changes and run `amplify publish`. Since you already pushed the changes earlier just the local build will be created and uploaded to the hosting bucket. Login as before if necessary and you'll be able to upload photos, which are protected by user. You can also refresh the page to view them.
117+
Save your changes and run `amplify publish`. Since you already pushed the changes earlier just the local build will be created and uploaded to the hosting bucket. Login as before if necessary and you'll be able to upload photos, which are protected by user. You can refresh the page to view them after uploading.
114118

115119

116120
# Add REST API calls to a database
117121

118-
Now that your application is setup, the final piece is to add a backend API with data that can be peristed in a database. For this example we will use a REST backend with a NoSQL database. Run `amplify add api` and follow the prompts, giving your API a friendly name such as **myapi** or something else that you remember. Use the default `/items` path and select **Create a new lambda function**. Select the option titled **CRUD function for Amazon DynamoDB table (Integration with Amazon API Gateway and Amazon DynamoDB)** when prompted. This will create an architecture of Amazon API Gateway with Express running in a Lambda function that reads and writes to Amazon DynamoDB. You'll be able to later modify the routes in the Lambda function to meet your needs and update it in the cloud.
122+
Now that your application is setup, the final piece is to add a backend API with data that can be peristed in a database. For this example we will use a REST backend with a NoSQL database. Run `amplify add api` and follow the prompts, select the **API Gateway** provider, giving your API a friendly name such as **myapi** or something else that you remember. Use the default `/items` path and select **Create a new lambda function**. Select the option titled **CRUD function for Amazon DynamoDB table (Integration with Amazon API Gateway and Amazon DynamoDB)** when prompted. This will create an architecture of Amazon API Gateway with Express running in a Lambda function that reads and writes to Amazon DynamoDB. You'll be able to later modify the routes in the Lambda function to meet your needs and update it in the cloud.
123+
124+
Since you do not have a database provisioned yet, the CLI workflow will prompt you for this. Alternatively, you could have run `amplify add storage` beforehand to create a DynamoDB table and use it in this setup. When the CLI asks you for the Primary Key structure use an attribute named `id` of type `String`. Don't select any other options like Sort keys or GSIs.
119125

120-
Since you do not have a database provisioned yet, the CLI workflow will prompt you for this. Alternatively, you could have run `amplify add storage` beforehand to create a DynamoDB table and use it in this setup. When the CLI asks you for the Primary Key structure use an attribute named `id` of type `String`.
121-
Next, you would need to select the security type for the API. Select **Authenticated - AWS IAM (Signature Version 4 signing** option.
126+
Next, you would need to select the security type for the API. Select **Authenticated - AWS IAM (Signature Version 4 signing)** option.
122127

123128

124129
Edit your `App.js` file in the React project again and modify your imports so that the `API` category is included as well to make API calls from the app.
125130

126131
```
127-
import { Analytics, Storage, API } from 'aws-amplify';
132+
import Amplify, { Analytics, Storage, API } from 'aws-amplify';
128133
```
129134

130135

131136
In `App.js` add in the following code before the `render()` method, update **myapi** if you used an alternative name during setup:
137+
132138
```
133139
post = async () => {
134140
console.log('calling api');
@@ -150,9 +156,10 @@ In `App.js` add in the following code before the `render()` method, update **mya
150156
const response = await API.get('myapi', '/items/1');
151157
alert(JSON.stringify(response, null, 2));
152158
}
153-
```
159+
```
154160

155161
Update the `render()` method to include calls to these three methods:
162+
156163
```
157164
render() {
158165
return (
@@ -167,8 +174,19 @@ Update the `render()` method to include calls to these three methods:
167174
</div>
168175
);
169176
}
170-
```
177+
```
171178

172179
Save the file and run `amplify publish`. After the API is deployed along with the Lambda function and database table, your app will be built and updated in the cloud. You can then add a record to the database by clicking **POST** and use **GET** or **LIST** to retrieve the record, which has been hard coded in this simple example.
173180

174-
In your project directory, open `./amplify/backend/function` and you will see the Lambda function that you created. The `app.js` file runs the Express function and all of the HTTP method routes are available for you to manipulate. For instance the `API.post()` in your React app corresponded to the `app.post(path, function(req, res){...})` code in this Lambda function. If you choose to customize the Lambda you can always update it in the cloud with `amplify push`.
181+
In your project directory, open `./amplify/backend/function` and you will see the Lambda function that you created. The `app.js` file runs the Express function and all of the HTTP method routes are available for you to manipulate. For instance the `API.post()` in your React app corresponded to the `app.post(path, function(req, res){...})` code in this Lambda function. If you choose to customize the Lambda you can always update it in the cloud with `amplify push`.
182+
183+
184+
# Testing your Function
185+
186+
Amplify CLI supports local testing of Lambda functions. Run `amplfiy status` to get the resource name of the Lambda function created earlier, and execute:
187+
188+
```
189+
amplify function invoke <resourcename>
190+
```
191+
192+
Notice that the function runs but doesn't exit, this is because this Lambda example starts an Express server which you need to manually close when testing from the CLI. `ctrl-c` to close and open the `./amplify/backend/function/resourcename` directory to see the local structure that is packaged for Lambda invocation from API Gateway. Inside the `src` directory is the Lambda function itself, as well as an `event.json` which is used for the `amplify function invoke` command testing you just ran. Also in this directory is `index.js` which is the main entry point for the Serverless Express library that echoed out the test event and instantiated the server inside `app.js`. Since the Express routes defined in `app.js` doesn't have a path that was called via the test event, it responded with a basic 404 message. The details of Express route modifications are left as an exercise to the reader. For more info on it, checkout - https://github.com/awslabs/aws-serverless-express

0 commit comments

Comments
 (0)