This template sets up an AWS Amplify app and associates a domain with it, including redirect rules from a base URL to a www subdomain.

Terraform Template

resource "aws_amplify_app" "example" {

  custom_rule {
    source = "https://example.com"
    status = "302"
    target = "https://www.example.com"
  }
  name = "app"
}

resource "aws_amplify_branch" "master" {
  app_id = aws_amplify_app.example.id
  branch_name = "master"
}

resource "aws_amplify_domain_association" "example" {
  app_id = aws_amplify_app.example.id
  domain_name = "example.com"

  sub_domain {
    branch_name = aws_amplify_branch.master.branch_name
  }

  sub_domain {
    branch_name = aws_amplify_branch.master.branch_name
    prefix = "www"
  }
}