AWS Amplify Gen 2 Backend

repository·main·Indexed 42 days ago

https://github.com/aws-amplify/amplify-backend

A TypeScript-first experience for defining cloud backends using file conventions and Git branch-based environments. It provides high-level abstractions and L3 CDK constructs for authentication (AmplifyAuth), data (defineData), AI conversation handlers via AWS Bedrock, Lambda functions (defineFunction), and mobile push notifications. Includes the ampx CLI for backend management and deployment.

Tokens
119.6K
Snippets
411
Records
563
Agent score
53%

What's inside AWS Amplify

  1. Understand the purpose of @aws-amplify/backend-output-schemas

    main

    The @aws-amplify/backend-output-schemas package provides zod schemas for the data stored in AWS Amplify stack outputs and metadata. These schemas serve two primary purposes for developers:

    1. Type Generation: They allow for the automatic generation of TypeScript types for backend output data.
    2. Runtime Validation: They enable validation of the expected data structure when reading stack outputs at runtime, ensuring data integrity.
  2. Use @aws-amplify/backend-secret to manage SSM SecureString secrets

    main

    The @aws-amplify/backend-secret package provides an abstraction over AWS Systems Manager (SSM) to perform CRUDL (Create, Read, Update, Delete, List) operations on SSM SecureString values.

    Use this package whenever your backend needs to read or write secrets to SSM. It automatically handles the normalization of SSM path names using a BackendIdentifier, ensuring that secret paths are consistent and correctly scoped within your Amplify backend environment.

  3. Use the AmplifyAuth CDK Construct

    main

    The AmplifyAuth construct is an L3 CDK Construct that simplifies setting up Amazon Cognito for authentication and authorization. It automatically provisions a UserPool, UserPoolClient, IdentityPool, and the necessary Authenticated/Unauthenticated IAM Roles.

    Important Limitation: Currently, only a single instance of the AmplifyAuth construct can be invoked within a single CDK synthesis.

    import { App, Stack } from 'aws-cdk-lib';
    import { AmplifyAuth } from '@aws-amplify/auth-construct';
    
    const app = new App();
    const stack = new Stack(app, 'AuthStack');
    
    new AmplifyAuth(stack, 'Auth');
  4. Understand the amplify-backend repository structure

    main

    The amplify-backend repository is organized as a monorepo. Key directories include:

    • packages/: Contains the individual npm packages published to registry (e.g., @aws-amplify/backend, @aws-amplify/auth-construct). Note that integration-tests is located here but is not published.
    • scripts/: Contains repository tooling scripts, all written in TypeScript.
    • templates/: Contains package templates used to scaffold new packages.
    • .changeset/: Stores changeset configurations and change descriptions for managing releases.
    • .github/: Contains GitHub Actions and workflows.
    • .husky/: Contains git hooks configuration.
  5. Define your Amplify backend with defineBackend

    main
    The @aws-amplify/backend package serves as the primary entry point for defining an Amplify Gen 2 backend. You use the defineBackend function to orchestrate your backend configuration, which pulls in specialized functions for various feature verticals (such as Auth, Data, and Functions). This package implements the interfaces required to orchestrate the AWS CDK Synth process for your Amplify application.
  6. Overview of the Amplify Backend dependency graph

    main

    The architecture follows a layered dependency model:

    1. Customer Project: Declares dependencies on @aws-amplify/backend-cli and @aws-amplify/backend.
    2. Command Line Process: The @aws-amplify/backend-cli package manages subcommands and depends on various internal packages.
    3. Backend Core: The @aws-amplify/backend package acts as an orchestrator, depending on feature vertical packages.
    4. Feature Verticals: Packages like auth, data, storage, and functions implement interfaces defined in plugin-types.
    5. Synth Process: The core logic for synthesizing the backend infrastructure.
  7. What is the Amplify sandbox library?

    main

    The @aws-amplify/sandbox package provides a programmatic library for managing the Amplify sandbox lifecycle. It is designed to automate the development loop by:

    1. Starting a file watching process: Automatically triggers deployments whenever backend files are modified.
    2. Executing deployments: Uses the backend-deployer to perform the actual deployment tasks.
    3. Emitting lifecycle events: Provides hooks that allow developers to attach handlers for additional operations. For example, the Amplify CLI uses these events to automatically regenerate client configuration files whenever a deployment succeeds.
  8. What is the Conversation Handler Function construct?

    main

    The ConversationHandlerFunction is an L3 CDK Construct provided by @aws-amplify/ai-constructs. It provisions the necessary infrastructure to power AI conversation routes (defined via a.ai.conversation() in your data schema) using AWS Bedrock models.

    When you use this construct, it deploys:

    1. An AWS Lambda function: This function acts as the bridge between your AWS AppSync instance (hosting the conversation routes) and AWS Bedrock. It receives conversation turn events via GraphQL, interacts with Bedrock to generate responses, and sends the response back via GraphQL mutations.
    2. An AWS CloudWatch Log Group: Includes an appropriate data protection policy.
    3. An AWS IAM Policy: Grants the Lambda function permission to access the specific AWS Bedrock models you select.

    Usage Modes:

    • Implicit (Default): If you use @aws-amplify/data-construct and do not specify a handler reference in your schema, Amplify automatically deploys a default implementation.
    • Explicit (Custom): You can provide your own Lambda implementation by explicitly including this construct in your backend definition and referencing it in your schema.