

# Connect your app to private resources

Use a network connector when your app must reach a private resource in an existing Amazon Virtual Private Cloud (VPC), for example a database with no public endpoint. A network connector sends the outbound traffic of an environment through subnets and security groups that you choose.

Amplify hosting does not need a VPC or security groups. Amplify provides the networking that serves your app. Do not create a network connector unless your app must reach a private resource. If you are not sure that you need one, find out what the VPC is for first.

## How a network connector works
<a name="customize-your-app-vpc-how"></a>

A network connector belongs to your AWS account and Region. It is not part of one app. You can attach one connector to many environments.

An environment has at most one connector for each deployment stage. The two stages are build, while Amplify builds your app, and runtime, while your app serves requests. By default, you attach a connector to both stages.

When a connector is attached, the build and runtime compute of the environment send outbound traffic through your VPC. Traffic to the public internet then leaves through a NAT gateway in your VPC. This includes the following traffic:
+ Requests from your server code to the backend of your app, at `AMPLIFY_BACKEND_URL`. The backend has no private VPC endpoint.
+ Requests to public APIs.
+ Downloads from npm during the build.

Calls that the browser makes to your backend are not affected.

The connector name and the VPC cannot change. To change either one, delete the connector and create a new one. You can change the subnets, the security groups, and the operator role with `aws amplifygen3 network edit`.

## Before you begin
<a name="customize-your-app-vpc-prereqs"></a>

You need the following items before you create a connector.
+ One or more subnets in your VPC. You can use up to 16 subnets. All subnets must be in the same VPC.
+ One or more security groups in the same VPC. You can use up to 5 security groups.
+ An IAM role, called the operator role, that Amplify assumes to create network interfaces in your subnets.

If your app has a backend, the subnets and security groups must also meet these requirements. Without them, the server code of your app cannot reach its backend.
+ Each subnet has a route for `0.0.0.0/0` to an available NAT gateway. A route to an internet gateway does not work, because the network interfaces of the connector do not get a public IP address.
+ At least one security group allows outbound traffic on port 443.

### Create the operator role
<a name="customize-your-app-vpc-role"></a>

You own the operator role. No Amplify command creates it. The operator role is not the execution role of your app.

The trust policy of the role must allow the `network-connectors.amplifygen3.amazonaws.com` service principal to call `sts:AssumeRole`. Do not add an `aws:SourceAccount` condition to the trust policy. Amplify cannot assume a role that has this condition.

Save the following trust policy as `trust.json`.

```json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {"Service": "network-connectors.amplifygen3.amazonaws.com"},
    "Action": "sts:AssumeRole"
  }]
}
```

The role needs four Amazon EC2 actions. Replace `<region>`, `<account>`, and `<vpc-id>` with your values. Save the following permissions policy as `perms.json`.

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ec2:CreateNetworkInterface",
      "Resource": [
        "arn:aws:ec2:<region>:<account>:subnet/*",
        "arn:aws:ec2:<region>:<account>:security-group/*",
        "arn:aws:ec2:<region>:<account>:network-interface/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": "ec2:DeleteNetworkInterface",
      "Resource": "arn:aws:ec2:<region>:<account>:network-interface/*",
      "Condition": {
        "StringEquals": {
          "ec2:Vpc": "arn:aws:ec2:<region>:<account>:vpc/<vpc-id>"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": ["ec2:DescribeSubnets", "ec2:DescribeSecurityGroups"],
      "Resource": "*"
    }
  ]
}
```

Keep `"Resource": "*"` for the two `Describe` actions. Amazon EC2 does not support resource-level permissions for them. If you scope them to ARNs, Amazon EC2 denies them and the connector cannot be created.

The `ec2:Vpc` condition limits the delete action to network interfaces in the VPC of the connector. Do not add an `ec2:ManagedResourceOperator` condition. The network interfaces that the connector creates do not carry that value. Amazon EC2 then refuses every delete, but `aws amplifygen3 network delete` still reports success. The network interfaces stay in your subnets and block the deletion of your security groups.

Create the role.

```bash
aws iam create-role \
  --role-name AmplifyVpcOperator \
  --assume-role-policy-document file://trust.json

aws iam put-role-policy \
  --role-name AmplifyVpcOperator \
  --policy-name VpcOperatorEni \
  --policy-document file://perms.json
```

## Create a network connector
<a name="customize-your-app-vpc-create"></a>

Run `aws amplifygen3 network create`. Amplify reads the VPC from the subnets that you give. Repeat `--subnet` and `--security-group` for more than one value.

```bash
aws amplifygen3 network create \
  --name my-private-network \
  --subnet subnet-0abc1234 \
  --subnet subnet-0def5678 \
  --security-group sg-0abc1234 \
  --operator-role-arn arn:aws:iam::123456789012:role/AmplifyVpcOperator \
  --watch
```

The name must be unique in your account and Region, with 1 to 64 letters, digits, hyphens, or underscores. With `--watch`, the command waits until the connector is `ACTIVE` or `CREATE_FAILED`. In CI or an agent session, the wait stops after 60 seconds. Add `--timeout 5m` to wait longer. If you leave out a required flag, the command prompts for it. In CI (`CI=true`), the command fails instead.

Amplify creates network interfaces in your subnets as soon as you run the command. It does not wait for the first deploy. Creation takes a few minutes.

Before it creates anything, the command checks the operator role. The command stops if the role does not exist, is in another account, or does not trust `network-connectors.amplifygen3.amazonaws.com`. If you do not have the `iam:GetRole` permission, the command skips this check. A wrong role then leaves the connector in the `CREATE_FAILED` state.

## Connect the network connector to an environment
<a name="customize-your-app-vpc-connect"></a>

Run `aws amplifygen3 network connect`. The following command attaches the connector to the `production` environment of the app in the current directory.

```bash
aws amplifygen3 network connect --name my-private-network --env production
```

By default, Amplify attaches the connector to both stages. To attach one stage only, add `--stage build` or `--stage runtime`.

The attachment applies on the next deploy of the environment. A deployment that is already running is not affected. Deploy the environment to put the change into effect. For `production`, confirm with the owner of the app first.

```bash
aws amplifygen3 deploy -e production
```

Attaching a connector replaces the connector that is already attached to each stage that you name. Amplify saves the attachment on the environment. It does not save the attachment in `amplify.jsonc`.

Before you connect an environment that has a backend, check the three requirements in [Before you begin](#customize-your-app-vpc-prereqs). Without a NAT route and outbound access on port 443, the server code of your app times out when it calls `AMPLIFY_BACKEND_URL`. Calls from the browser still work.

## Check the connector
<a name="customize-your-app-vpc-verify"></a>

Show the status of one connector. The output includes the subnets, the security groups, the operator role, and each environment and stage that uses the connector.

```bash
aws amplifygen3 network get --name my-private-network
```

The status must be `ACTIVE`. Then deploy the environment and test that your app reaches the private resource.

List all connectors in your account and Region. Add `--show-attachments` to show how many environments use each connector.

```bash
aws amplifygen3 network list --show-attachments
```

## Change a network connector
<a name="customize-your-app-vpc-edit"></a>

Use `aws amplifygen3 network edit` to change the subnets, the security groups, or the operator role. The flags `--subnet` and `--security-group` each replace the whole current list. A flag that you leave out keeps its current value.

```bash
aws amplifygen3 network edit \
  --name my-private-network \
  --subnet subnet-0aaa1111 \
  --subnet subnet-0bbb2222 \
  --watch
```

To use a different operator role, pass `--operator-role-arn`. The command checks the new role in the same way as `network create`.

The connector is `UPDATING` while the change is made. If the update fails, the connector status is `UPDATE_FAILED` and the connector keeps its previous settings. The change applies on the next deploy of each environment that uses the connector.

## Disconnect a network connector from an environment
<a name="customize-your-app-vpc-disconnect"></a>

Disconnect a connector to send the traffic of the environment over the public internet again.

```bash
aws amplifygen3 network disconnect --env production
```

You do not pass a connector name. The environment and the stages identify what to remove. Use `--stage build` or `--stage runtime` to remove one stage only. If you leave out `--stage`, Amplify removes both stages. If a stage has no connector, nothing changes.

The change applies on the next deploy of the environment. Deployments that are already running are not affected. The connector is not deleted. Other environments can still use it.

If your app depends on the private resource, it cannot reach that resource after the next deploy. Check what the app needs before you disconnect.

## Delete a network connector
<a name="customize-your-app-vpc-delete"></a>

**Warning**  
Deleting a network connector removes the network interfaces that Amplify created in your subnets. The command asks you to confirm. Make sure that no environment needs the connector before you confirm.

You cannot delete a connector that is attached to an environment. Disconnect the connector from each environment first. If you run `aws amplifygen3 network delete` while the connector is attached, the command lists the environments that still use it.

```bash
aws amplifygen3 network delete --name my-private-network
```

The command asks `y/N` before it deletes. To skip the prompt, pass `--yes`. In CI (`CI=true`), you must pass `--yes`, because no prompt is available. Use `--yes` only after the owner of the app has confirmed the deletion.

The connector is `DELETING` while Amplify removes it. Delete the connector before you delete the subnets or the VPC that it uses.

## Troubleshooting
<a name="customize-your-app-vpc-troubleshooting"></a>

The server code times out when it calls `AMPLIFY_BACKEND_URL` after you connect an environment  
The connector sends all outbound traffic through your VPC, and the backend has no private VPC endpoint. Run `aws amplifygen3 network get --name <name>` to see the subnets and security groups. Then check the VPC with the AWS CLI. Each subnet needs a `0.0.0.0/0` route to an available NAT gateway. A route to an internet gateway does not work. A security group must allow outbound traffic on port 443. If the VPC cannot have internet access, the server code of your app cannot reach its backend while the connector is attached.

 `network create` fails with "The service is unable to assume the provided NetworkConnectorOperatorRole"  
The trust policy of the operator role has an `aws:SourceAccount` condition. Remove the condition.

 `network create` fails with "--operator-role-arn names an IAM role Amplify cannot assume"  
The role does not exist, is in another account, or its trust policy does not allow `network-connectors.amplifygen3.amazonaws.com` to call `sts:AssumeRole`. Use the trust policy in [Create the operator role](#customize-your-app-vpc-role).

 `network create` fails with "unauthorized operation while calling EC2 due to invalid ConnectorOperatorRole permissions"  
Amplify assumed the operator role, but Amazon EC2 refused a call. Check that the role has `ec2:CreateNetworkInterface`, `ec2:DescribeSubnets`, and `ec2:DescribeSecurityGroups`. Check that the two `Describe` actions use `"Resource": "*"`.

The connector status is `CREATE_FAILED`   
You can get this status when the operator role is wrong and you do not have the `iam:GetRole` permission. Check the operator role against [Create the operator role](#customize-your-app-vpc-role).

Network interfaces remain in your subnets after you delete a connector  
The delete permission of the operator role has an `ec2:ManagedResourceOperator` condition. Remove it and use the `ec2:Vpc` condition from [Create the operator role](#customize-your-app-vpc-role). Then remove the network interfaces that remain, which are in the `available` state. They block the deletion of your security groups.

For more information about how Amplify isolates your resources, see [Infrastructure security in AWS Amplify](infrastructure-security.md). For managed connections to other AWS services, see [Integrations](integrations.md).