Set up fullstack project
Create a new Vanilla JavaScript app with vite using the following commands, create the directory (amplify-js-app
) and files for the app.
npm create vite@latest✔ Project name: amplify-js-app✔ Select a framework: › Vanilla✔ Select a variant: › JavaScript
Initialize npm and install dependencies and dev dependencies.
cd amplify-js-appnpm 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.
Add the following to the index.html
file:
<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <title>Amplify</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link href="style.css" rel="stylesheet" /> </head>
<body> <div class="app"> <div class="app-header"> <h1>Welcome to Amplify</h1> </div> <div class="app-body"> <h1>Mutation Results</h1> <button id="MutationEventButton">Add data</button> <div id="MutationResult"></div> <hr />
<h1>Query Results</h1> <div id="QueryResult"></div> <hr />
<h1>Subscription Results</h1> <div id="SubscriptionResult"></div> </div> </div> <script type="module" src="/main.js"></script> </body></html>
Add the following to style.css
file:
html,body { font-family: 'Amazon Ember', 'Helvetica', 'sans-serif'; margin: 0;}a { color: #ff9900;}h1 { font-weight: 300;}hr { height: 1px; background: lightgray; border: none;}.app { width: 100%;}.app-header { color: white; text-align: center; background: linear-gradient(30deg, #f90 55%, #ffc300); width: 100%; margin: 0 0 1em 0; padding: 3em 0 3em 0; box-shadow: 1px 2px 4px rgba(0, 0, 0, 0.3);}.app-body { width: 400px; margin: 0 auto; text-align: center;}.app-body button { background-color: #ff9900; font-size: 14px; color: white; text-transform: uppercase; padding: 1em; border: none;}.app-body button:hover { opacity: 0.8;}
In main.js
remove the boilerplate code and leave it empty. Then refresh the browser to see the changes.
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: amplifyjsappThe following configuration will be applied:
Project information| Name: amplifyjsapp| Environment: dev| Default editor: Visual Studio Code| App type: javascript| Javascript framework: none| 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
For more information on AWS Profiles, see:https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
? 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 main.js and add the following code:
import './style.css'import { Amplify } from 'aws-amplify';import amplifyconfig from './src/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.