Project structure
Amplify Gen 2 backends are defined using TypeScript, and enable you to collocate resources depending on their function. For example, you can author a post confirmation trigger for Amazon Cognito that creates a UserProfile model right next to your auth's resource file.
When you create your first Amplify project using npm create amplify@latest
, it will automatically set up the scaffolding for Data and Authentication resources:
âââ amplify/â âââ auth/â â âââ resource.tsâ âââ data/â â âââ resource.tsâ âââ backend.tsâ âââ package.jsonâââ node_modules/âââ .gitignoreâââ package-lock.jsonâââ package.jsonâââ tsconfig.json
As your project grows and you build out your backend, the structure of your project may look like the following:
âââ amplify/â âââ auth/â â âââ custom-message/â â â âââ custom-message.tsxâ â â âââ handler.tsâ â â âââ package.jsonâ â â âââ resource.tsâ â âââ post-confirmation.tsâ â âââ pre-sign-up.tsâ â âââ resource.tsâ â âââ verification-email.tsxâ âââ data/â â âââ resolvers/â â â âââ list-featured-posts.tsâ â â âââ list-top-10-posts.tsâ â âââ resource.tsâ â âââ schema.tsâ âââ jobs/â â âââ monthly-report/â â â âââ handler.tsâ â â âââ resource.tsâ â âââ process-featured-posts/â â â âââ handler.pyâ â â âââ requirements.txtâ â â âââ resource.tsâ â âââ store-top-10-posts/â â âââ handler.tsâ â âââ resource.tsâ âââ storage/â â âââ photos/â â â âââ resource.tsâ â â âââ trigger.tsâ â âââ reports/â â âââ resource.tsâ âââ backend.tsâ âââ package.jsonâââ node_modules/âââ .gitignoreâââ package-lock.jsonâââ package.jsonâââ tsconfig.json
Backend resources are defined in resource
files using the define*
helpers:
import { defineAuth } from '@aws-amplify/backend';
export const auth = defineAuth({ loginWith: { email: true }});
After the resources are defined, they are set up on the backend:
import { defineBackend } from '@aws-amplify/backend';import { auth } from './auth/resource';import { data } from './data/resource';
defineBackend({ auth, data});
You can extend backends by using the AWS Cloud Development Kit (AWS CDK), which is installed by default as part of the create-amplify
workflow. With the CDK, you can build using any AWS service, such as an Amazon S3 bucket that authenticated users have read and write access to. To get started with the CDK, add it to your backend:
import * as s3 from 'aws-cdk-lib/aws-s3';import { defineBackend } from '@aws-amplify/backend';import { auth } from './auth/resource';import { data } from './data/resource';
const backend = defineBackend({ auth, data});
// create the bucket and its stackconst bucketStack = backend.getStack('BucketStack');const bucket = new s3.Bucket(bucketStack, 'Bucket', { blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL});
// allow any authenticated user to read and write to the bucketconst authRole = backend.auth.resources.authenticatedUserIamRole;bucket.grantReadWrite(authRole);
// allow any guest (unauthenticated) user to read from the bucketconst unauthRole = backend.auth.resources.unauthenticatedUserIamRole;bucket.grantRead(unauthRole);