You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: amplifycli_react_tutorial.md
+32-14Lines changed: 32 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,16 @@ This tutorial will walk you through using the AWS Amplify CLI with a React appli
4
4
5
5
6
6
# 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).
8
13
- Ensure you have NodeJS installed
9
14
- Examples below use `yarn` but `npm` works as well.
- 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`
12
17
- Ensure you have [Create React App](https://github.com/facebook/create-react-app) installed and create a new project:
13
18
```
14
19
yarn create-react-app -g
@@ -39,7 +44,7 @@ You can see the changes are not deployed by running `amplify status` and then si
39
44
40
45
# Add User Registration and Sign-In
41
46
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**.
43
48
44
49
Next, add the Amplify library to your application:
45
50
```
@@ -70,7 +75,7 @@ A login screen is nice but now is the time to add some features, like tracking a
70
75
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.
71
76
72
77
```
73
-
import { Analytics, Storage } from 'aws-amplify';
78
+
import Amplify, { Analytics, Storage } from 'aws-amplify';
74
79
import { withAuthenticator, S3Album } from 'aws-amplify-react';
75
80
```
76
81
@@ -108,27 +113,28 @@ Finally, modify the `render` method so that you can upload files and also view a
108
113
);
109
114
}
110
115
```
111
-
Refresh the web page manually to see the uploaded image in your app.
112
116
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.
114
118
115
119
116
120
# Add REST API calls to a database
117
121
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.
119
125
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.
122
127
123
128
124
129
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.
125
130
126
131
```
127
-
import { Analytics, Storage, API } from 'aws-amplify';
132
+
import Amplify, { Analytics, Storage, API } from 'aws-amplify';
128
133
```
129
134
130
135
131
136
In `App.js` add in the following code before the `render()` method, update **myapi** if you used an alternative name during setup:
137
+
132
138
```
133
139
post = async () => {
134
140
console.log('calling api');
@@ -150,9 +156,10 @@ In `App.js` add in the following code before the `render()` method, update **mya
Update the `render()` method to include calls to these three methods:
162
+
156
163
```
157
164
render() {
158
165
return (
@@ -167,8 +174,19 @@ Update the `render()` method to include calls to these three methods:
167
174
</div>
168
175
);
169
176
}
170
-
```
177
+
```
171
178
172
179
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.
173
180
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