Lesson 1 of 1 AWS Amplify

Hosting Web Apps and Branch Environments with AWS Amplify

This lesson explains how AWS Amplify provides tools and hosting for full-stack web and mobile apps, with deployments driven directly from Git.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how Amplify Hosting builds and deploys an application on every commit.
  • Explain how Git branches map to separate environments and pull request previews.
  • Describe how redirects, rewrites and custom domains are configured.
  • Describe how Amplify Gen 2 defines a backend in TypeScript.

Amplify Hosting

AWS Amplify Hosting is a managed service for deploying web front ends. The developer connects a Git repository from GitHub, GitLab, Bitbucket or AWS CodeCommit, and Amplify builds and deploys the application on every commit to a connected branch, then serves it through a content delivery network (CDN) built on Amazon CloudFront. No servers, build pipeline or CDN configuration need to be created by hand.

Amplify Hosting supports static sites and single-page applications built with frameworks such as React, Angular and Vue, and server-side rendered (SSR) applications such as Next.js and Nuxt, whose server-side code runs in AWS-managed compute. A build specification file, amplify.yml, defines the build phases, the commands to run in each, the output directory to deploy and any cache paths, in the same way that a buildspec does for AWS CodeBuild. Amplify detects common frameworks and generates a default build specification, which the developer can then adjust.

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: dist
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Branch environments

Amplify Hosting maps Git branches to environments. Each connected branch becomes a separate deployed environment with its own URL, so main can serve production while dev serves a test version. Each branch can have its own environment variables, for example the address of a different back-end API, and its own secrets, so the same code behaves correctly in each environment.

Pull request previews extend this to short-lived work. When a pull request is opened, Amplify builds it and deploys it to a temporary environment with its own URL, so reviewers can try the change before it is merged; the preview is removed when the pull request is closed. Because preview environments are publicly reachable by default, they can be protected with password protection on the branch, which requires a user name and password before the site is shown. For repositories that are public, previews should be used with care, because anyone who can open a pull request could cause a build.

Routing and domains

Amplify Hosting also handles how requests are routed and which domain names serve the application. Redirects and rewrites are rules evaluated for each request. A redirect sends the browser to a different URL with a 301 or 302 status, for example from an old page to its new address. A rewrite serves different content without changing the URL in the browser.

Rewrites matter for single-page applications. Such an application has only one HTML file, index.html, and its JavaScript router handles paths such as /orders/42. If a user loads that path directly, the CDN looks for a file with that name, finds none and returns 404. A rewrite rule that serves index.html with a 200 status for any path that is not a static file solves this.

Custom domains are connected in the Amplify console. Amplify requests and renews an SSL/TLS certificate through AWS Certificate Manager automatically, and when the domain is hosted in Amazon Route 53, it also creates the DNS records. Subdomains can be mapped to branches, such as dev.example.com to the dev branch.

Backend tooling

Besides hosting, Amplify provides tooling for the backend of an application. With Amplify Gen 2, the backend is defined in TypeScript files in the same repository as the front end: authentication with Amazon Cognito, a data model served by AWS AppSync and stored in Amazon DynamoDB, file storage in Amazon S3 and custom functions in AWS Lambda. Amplify deploys these resources through the AWS CDK and AWS CloudFormation, and every branch and every developer can have an isolated copy of the backend, called a sandbox, for testing.

The Amplify client libraries connect the front end to those services, for example to sign users in with Cognito or to call the AppSync API with the signed-in user's credentials. The distinction for the exam is that Amplify Hosting deploys and serves the front end, while the Amplify back-end tooling creates the AWS services the front end uses.

Worked example

A preview for every pull request

A front-end team wants reviewers to see each change running before it is merged. With Amplify Hosting connected to the repository:

Branch or eventAmplify environmentURL
mainProductionwww.example.com
developStagingdevelop.d1abc.amplifyapp.com
Pull request #42Preview, deleted when the PR closespr-42.d1abc.amplifyapp.com

Every push builds and deploys automatically. A single-page app also needs a rewrite rule sending unknown paths to /index.html, otherwise deep links such as /orders/42 return 404.

Check yourself

How does Amplify Hosting deploy a new version of a front end connected to a Git repository?

Show answer

Each push to a connected branch starts a build, and Amplify deploys the result to that branch's environment and URL, so no separate pipeline is needed.

Exam Tips

Remember for the exam

  • A separate hosted environment for each Git branch points to Amplify Hosting branches.
  • To preview each pull request before merging, use Amplify pull request previews.
  • If single-page app routes return 404, configure a rewrite to index.html.
  • To restrict access to a preview environment, use branch password protection.