Resource: aws_amplify_branch

Provides an Amplify Branch resource.

Example Usage

resource "aws_amplify_app" "example" {
  name = "app"
}

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

  framework = "React"
  stage     = "PRODUCTION"

  environment_variables = {
    REACT_APP_API_SERVER = "https://api.example.com"
  }
}

Basic Authentication

resource "aws_amplify_app" "example" {
  name = "app"
}

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

  enable_basic_auth      = true
  basic_auth_credentials = base64encode("username:password")
}

Notifications

Amplify Console uses EventBridge (formerly known as CloudWatch Events) and SNS for email notifications. To implement the same functionality, you need to set enable_notification in a aws_amplify_branch resource, as well as creating an EventBridge Rule, an SNS topic, and SNS subscriptions.

resource "aws_amplify_app" "example" {
  name = "app"
}

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

  # Enable SNS notifications.
  enable_notification = true
}

# EventBridge Rule for Amplify notifications

resource "aws_cloudwatch_event_rule" "amplify_app_master" {
  name        = "amplify-${aws_amplify_app.app.id}-${aws_amplify_branch.master.branch_name}-branch-notification"
  description = "AWS Amplify build notifications for :  App: ${aws_amplify_app.app.id} Branch: ${aws_amplify_branch.master.branch_name}"

  event_pattern = jsonencode({
    "detail" = {
      "appId" = [
        aws_amplify_app.example.id
      ]
      "branchName" = [
        aws_amplify_branch.master.branch_name
      ],
      "jobStatus" = [
        "SUCCEED",
        "FAILED",
        "STARTED"
      ]
    }
    "detail-type" = [
      "Amplify Deployment Status Change"
    ]
    "source" = [
      "aws.amplify"
    ]
  })
}

resource "aws_cloudwatch_event_target" "amplify_app_master" {
  rule      = aws_cloudwatch_event_rule.amplify_app_master.name
  target_id = aws_amplify_branch.master.branch_name
  arn       = aws_sns_topic.amplify_app_master.arn

  input_transformer {
    input_paths = {
      jobId  = "$.detail.jobId"
      appId  = "$.detail.appId"
      region = "$.region"
      branch = "$.detail.branchName"
      status = "$.detail.jobStatus"
    }

    input_template = "\"Build notification from the AWS Amplify Console for app: https://<branch>.<appId>.amplifyapp.com/. Your build status is <status>. Go to https://console.aws.amazon.com/amplify/home?region=<region>#<appId>/<branch>/<jobId> to view details on your build. \""
  }
}

# SNS Topic for Amplify notifications

resource "aws_sns_topic" "amplify_app_master" {
  name = "amplify-${aws_amplify_app.app.id}_${aws_amplify_branch.master.branch_name}"
}

data "aws_iam_policy_document" "amplify_app_master" {
  statement {
    sid = "Allow_Publish_Events ${aws_amplify_branch.master.arn}"

    effect = "Allow"

    actions = [
      "SNS:Publish",
    ]

    principals {
      type = "Service"
      identifiers = [
        "events.amazonaws.com",
      ]
    }

    resources = [
      aws_sns_topic.amplify_app_master.arn,
    ]
  }
}

resource "aws_sns_topic_policy" "amplify_app_master" {
  arn    = aws_sns_topic.amplify_app_master.arn
  policy = data.aws_iam_policy_document.amplify_app_master.json
}

resource "aws_sns_topic_subscription" "this" {
  topic_arn = aws_sns_topic.amplify_app_master.arn
  protocol  = "email"
  endpoint  = "user@acme.com"
}

Argument Reference

This resource supports the following arguments:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Amplify branch using app_id and branch_name. For example:

import {
  to = aws_amplify_branch.master
  id = "d2ypk4k47z8u6/master"
}

Using terraform import, import Amplify branch using app_id and branch_name. For example:

% terraform import aws_amplify_branch.master d2ypk4k47z8u6/master