Set up fullstack project
To set up the project, you'll first create a new Next.js app with Create Next App, a simple CLI tool that enables you to quickly start building a new Next.js application, with everything set up for you. You'll then add Amplify and initialize a new project.
From your projects directory, run the following commands:
npm create next-app@14 -- next-amplified --ts --no-eslint --src-dir --import-alias '@/*' --no-tailwind --appcd next-amplifiedThis command will bootstrap the project with the following options:
- App Router
- TypeScript
- No ESLint
- With the
srcdirectory - No Tailwind CSS
- imports aliased to
@/(the default)
Now that you're in the root of the project, you can run the app by using the following command:
npm run devThis runs a development server and allows us to see the output generated by the build, you can see the running app by navigating to \http://localhost:3000>.
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 initWhen you initialize Amplify you'll be prompted for some information about the app:
? Enter a name for the project (nextamplified)The following configuration will be applied:
Project information| Name: next-amplified| Environment: dev| Default editor: Visual Studio Code| App type: javascript| Javascript framework: react| Source Directory Path: src| Distribution Directory Path: build| 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: defaultWhen you initialize a new Amplify project, a few things happen:
- It creates a top level directory called
amplifythat stores your backend definition. During the tutorial you'll add cloud capabilities, such as GraphQL API and web hosting. As you add these features, theamplifyfolder 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.jsonin 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
.gitignorefile, 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. The @aws-amplify/adapter-nextjs package includes additional Next.js-specific functions for using the Amplify libraries server-side:
npm install aws-amplify @aws-amplify/adapter-nextjsThat's all it takes to install Amplify libraries with your Next.js application. Next, we'll create a new backend API and database tables. Then, we'll use the Amplify library's Next.js adapter to make API requests to your new backend.