Set up fullstack project
Create a new React App
To get started, first create a new React app, and then install and use the Amplify CLI to start adding backend capabilities to your app.
From your projects directory, run the following command and respond the prompts:
npm create vite@latest✔ Project name: react-amplified✔ Select a framework: › React✔ Select a variant: › TypeScript
npm create vite@latest✔ Project name: react-amplified✔ Select a framework: › React✔ Select a variant: › JavaScript
This creates a new React app in a directory called react-amplified. You can then run the following commands to switch into the new react-amplified directory, install the project dependencies, and run the app:
cd react-amplifiednpm installnpm run dev
This runs a development server and allows you to see the output generated by the build. You can see the running app by navigating to http://localhost:5173.
Initialize a new backend
Now that you have a running app, it's time to set up Amplify so that you can create the necessary backend services needed to support the app.
Open a new terminal. From the root of the project, run:
amplify init
When you initialize Amplify you'll be prompted for some information about the app:
? Enter a name for the project reactamplifiedThe following configuration will be applied:
?Project information| Name: reactamplified| Environment: dev| Default editor: Visual Studio Code| App type: javascript| Javascript framework: react| Source Directory Path: src
| Distribution Directory Path: dist
| Build Command: npm run-script build| Start Command: npm run-script start
? Initialize the project with the above configuration? YesUsing default provider awscloudformation? Select the authentication method you want to use: AWS profile
...
? Please choose the profile you want to use default
When you initialize a new Amplify project, a few things happen:
- It creates a top level directory called
amplify
that stores your backend definition. During the tutorial you'll add cloud capabilities, such as GraphQL API and web hosting. As you add these features, theamplify
folder will grow with infrastructure-as-code templates that define your backend stack. Infrastructure-as-code is a best practice way to create a replicable backend stack. - It creates a file called
amplifyconfiguration.json
in your designated Source Directory Path that holds all the configuration for the services you create with Amplify. This is how the Amplify JavaScript client library is able to get the necessary information to connect to your backend services. - It modifies the
.gitignore
file, adding some generated files to the ignore list
Install Amplify Libraries
The aws-amplify
package is the main library for working with Amplify Libraries in your projects:
npm install aws-amplify
Set up frontend
Next, configure the Amplify libraries client-side so it can interact with backend services.
Open src/main.tsx or src/main.jsx and add the following code below the last import:
import { Amplify } from 'aws-amplify';import amplifyconfig from './amplifyconfiguration.json';Amplify.configure(amplifyconfig);
And that's all it takes to configure Amplify. As you add or remove categories and make updates to your backend configuration using the Amplify CLI, the configuration in amplifyconfiguration.json will update automatically.
Now that your app is set up and Amplify is initialized, you can add an API in the next step.