

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS AppSync プライベート APIs
<a name="using-private-apis"></a>

Amazon Virtual Private Cloud (Amazon VPC) を使用する場合は、VPC からのみアクセスできる APIs である AWS AppSync APIs を作成できます。プライベート API を使用すると、データを公開することなく、内部アプリケーションへの API アクセスを制限し、GraphQL および Realtime エンドポイントに接続できます。

VPC と AWS AppSync サービス間のプライベート接続を確立するには、インターフェイス VPC エンドポイントを作成する必要があります。インターフェイスエンドポイントは を利用しており[AWS PrivateLink](https://aws.amazon.com/privatelink)、インターネットゲートウェイ、NAT デバイス、VPN 接続、または Direct Connect 接続なしで AWS AppSync APIs にプライベートにアクセスできます。VPC のインスタンスは、パブリック IP アドレスがなくても AWS AppSync API と通信できます。VPC と の間のトラフィック AWS AppSync は AWS ネットワークを離れません。

AWS AppSync は AWS PrivateLink 、データプレーンオペレーションとコントロールプレーンオペレーションの両方で をサポートします。
+ **データプレーンエンドポイント** (`com.amazonaws.{region}.appsync-api`): クエリ、ミューテーション、サブスクリプション用の GraphQL およびリアルタイム API へのプライベートアクセスを提供します。
+ **コントロールプレーンエンドポイント** (`com.amazonaws.{region}.appsync`): APIs の作成、スキーマの更新、データソースの設定などの AWS AppSync 管理オペレーションへのプライベートアクセスを提供します。

![\[AWS クラウド architecture showing VPC with public and private subnets connecting to AWS AppSync via PrivateLink.\]](http://docs.aws.amazon.com/ja_jp/appsync/latest/devguide/images/private-api-architecture.png)


プライベート API 機能を有効にする前に考慮すべき要素は他にもいくつかあります。
+ プライベート DNS 機能を有効に AWS AppSync して の VPC インターフェイスエンドポイントを設定すると、VPC 内のリソースが AWS AppSync 生成された APIs URL を使用して他の AWS AppSync パブリック API を呼び出すことができなくなります。これは、パブリック API へのリクエストがインターフェイスエンドポイント経由でルーティングされるためで、パブリック API では許可されていません。このシナリオでパブリック API を呼び出すには、パブリック API にカスタムドメイン名を設定し、VPC 内のリソースがそのドメイン名を使用してパブリック API を呼び出すことをお勧めします。
+ プライベート APIs AWS AppSync は VPC からのみ使用できます。 AWS AppSync コンソールクエリエディタは、ブラウザのネットワーク設定が VPC にトラフィックをルーティングできる (VPN 経由の接続など Direct Connect) 場合にのみ API に到達できます。
+ の VPC インターフェイスエンドポイントを使用すると AWS AppSync、同じ AWS アカウントとリージョン内の任意のプライベート API にアクセスできます。プライベート API へのアクセスをさらに制限するには、以下のオプションを検討してください。
  + 必要な管理者のみが VPC エンドポイントインターフェイスを作成できるようにします AWS AppSync。
  + VPC エンドポイントのカスタムポリシーを使用して、VPC 内のリソースから呼び出せる API を制限します。
  + VPC 内のリソースについては、IAM 認可を使用して AWS AppSync API を呼び出すことをお勧めします。そのためには、リソースに API に対してスコープダウンされたロールが割り当てられていることを確認する必要があります。
+ IAM プリンシパルを制限するポリシーを作成または使用するときは、メソッドの `authorizationType` を `AWS_IAM` または `NONE` に設定する必要があります。

## AWS AppSync プライベート APIsの作成
<a name="creating-private-apis"></a>

次の手順は、 AWS AppSync サービスでプライベート APIsを作成する方法を示しています。

**警告**  
プライベート API 機能は API の作成中にのみ有効にできます。この設定は、API AWS AppSync または AWS AppSync プライベート API の作成後に変更することはできません。

1. にサインイン AWS マネジメントコンソール し、[AppSync コンソール](https://console.aws.amazon.com/appsync/)を開きます。

   1. **ダッシュボード**で、**[API の作成]** を選択します。

1. **[API を最初から設計する]** を選択し、**[次へ]** を選択します。

1. **[プライベート API]** セクションで、**[プライベート API 機能を使用する]** を選択します。

1. 残りのオプションを設定し、API のデータを確認して、**[作成]** を選択します。

プライベート API を使用する前に、VPC AWS AppSync AWS AppSync で のインターフェイスエンドポイントを設定する必要があります。プライベート API と VPC の両方が同じ AWS アカウントとリージョンに存在する必要があることに注意してください。

## のインターフェイスエンドポイントの作成 AWS AppSync
<a name="creating-interface-endpoint"></a>

Amazon VPC コンソールまたは AWS Command Line Interface () AWS AppSync を使用して、 のインターフェイスエンドポイントを作成できますAWS CLI。ユースケースによって、一方または両方のエンドポイントタイプを作成する必要があります。
+ **データプレーンエンドポイント**: VPC からプライベート API にアクセスするために必要です
+ **コントロールプレーンエンドポイント**: AWS CLI または SDKs を使用して VPC から AWS AppSync リソースを管理するために必要です

詳細については、「Amazon VPC ユーザーガイド」の[インターフェイスエンドポイントの作成](https://docs.aws.amazon.com/vpc/latest/privatelink/vpce-interface.html#create-interface-endpoint)を参照してください。

**注記**  
必ず適切な VPC エンドポイントサービスを選択してください。AppSync には 2 つのサービスがあります。`com.amazonaws.{region}.appsync-api` はプライベート API に必要なサービスで、`com.amazonaws.{region}.appsync` は API 管理に使用されます。

------
#### [ Console ]

1. にサインイン AWS マネジメントコンソール し、Amazon VPC コンソールの[エンドポイント](https://console.aws.amazon.com/vpc/home?#Endpoints)ページを開きます。

1. **エンドポイントの作成** を選択します。

   1. **[サービスカテゴリ]** フィールドで、**[AWS のサービス]** が選択されていることを確認します。

   1. **[サービス]** テーブルで、次のいずれかのサービスを選択します。
      + データプレーンアクセスの場合: `com.amazonaws.{region}.appsync-api`
      + コントロールプレーンアクセスの場合: `com.amazonaws.{region}.appsync`

      **Type** 列の値が `Interface` であることを確認します。

   1. **VPC** フィールドで、VPC とそのサブネットを選択します。

   1. インターフェイスエンドポイントのプライベート DNS を有効にするには、**[DNS 名を有効にする]** チェックボックスをオンにします。

   1. **[セキュリティグループ]** フィールドで、1 つ以上のセキュリティグループを選択します。

1. **エンドポイントの作成** を選択します。

1. 必要に応じて、このプロセスを繰り返して 2 番目のエンドポイントタイプを作成します。

------
#### [ CLI ]

`[create-vpc-endpoint](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-vpc-endpoint.html)` コマンドを使用し、VPC ID、VPC エンドポイントタイプ (インターフェイス)、サービス名、エンドポイントを使用するサブネット、およびエンドポイントネットワークインターフェイスに関連付けるセキュリティグループを指定します。

**データプレーンエンドポイントを作成する:**

```
$ aws ec2 create-vpc-endpoint —vpc-id vpc-ec43eb89 \
  —vpc-endpoint-type Interface \
  —service-name com.amazonaws.{region}.appsync-api \
  —subnet-id subnet-abababab —security-group-id sg-1a2b3c4d
```

**コントロールプレーンエンドポイントを作成する:**

```
$ aws ec2 create-vpc-endpoint —vpc-id vpc-ec43eb89 \
  —vpc-endpoint-type Interface \
  —service-name com.amazonaws.{region}.appsync \
  —subnet-id subnet-abababab —security-group-id sg-1a2b3c4d
```

------

プライベート DNS オプションを使用するには、VPC の `enableDnsHostnames` および `enableDnsSupportattributes` を設定する必要があります。詳細については、「*Amazon VPC ユーザーガイド*」の「[VPC の DNS サポートを表示および更新する](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html#vpc-dns-updating)」を参照してください。インターフェイスエンドポイントのプライベート DNS 機能を有効にすると、以下の形式を使用して、デフォルトのパブリック DNS エンドポイントを使用して AWS AppSync API GraphQL およびリアルタイムエンドポイントにリクエストを行うことができます。

```
https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql
```

コントロールプレーンオペレーションでは、標準 AWS AppSync サービスエンドポイントを使用できます。

```
https://appsync.{region}.amazonaws.com
```

サービスエンドポイントの詳細については、「*AWS 全般のリファレンスガイド*」の「[サービスエンドポイントとクォータ](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)」を参照してください。

詳細については、*Amazon VPC ユーザーガイド*の「[インターフェイスエンドポイントを介したサービスへのアクセス](https://docs.aws.amazon.com/vpc/latest/privatelink/vpce-interface.html#access-service-though-endpoint)」を参照してください。

 AWS CloudFormation を使用してエンドポイントを作成および設定する方法については、*AWS CloudFormation ユーザーガイド*の [AWS::EC2::VPCEndpoint](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpcendpoint.html) リソースを参照してください。

## 高度な の例
<a name="advanced-example"></a>

インターフェイスエンドポイントのプライベート DNS 機能を有効にすると、以下の形式を使用して、デフォルトのパブリック DNS エンドポイントを使用して AWS AppSync API GraphQL およびリアルタイムエンドポイントにリクエストを行うことができます。

```
https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql
```

インターフェイスの VPC エンドポイントのパブリック DNS ホスト名を使用すると、API を呼び出すためのベース URL は次の形式になります。

```
https://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql
```

AZ にエンドポイントをデプロイしている場合は、AZ 固有の DNS ホスト名を使用することもできます。

```
https://{vpc_endpoint_id}-{endpoint_dns_identifier}-{az_id}.appsync-api.{region}.vpce.amazonaws.com/graphql.
```

VPC エンドポイントのパブリック DNS 名を使用するには、 AWS AppSync API エンドポイントのホスト名をリクエストのヘッダーとして、`Host`または` x-appsync-domain`ヘッダーとして渡す必要があります。以下の例では、[サンプルスキーマの起動](https://docs.aws.amazon.com/appsync/latest/devguide/quickstart-launch-a-sample-schema.html)`TodoAPI`ガイドで作成されたものを使用しています。

```
curl https://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql \
-H "Content-Type:application/graphql" \
-H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \
-H "Host:{api_url_identifier}.appsync-api.{region}.amazonaws.com" \
-d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'
```

以下の例では、[サンプルスキーマの起動](https://docs.aws.amazon.com/appsync/latest/devguide/quickstart-launch-a-sample-schema.html)ガイドで生成された *Todo* アプリを使用します。サンプル Todo API をテストするために、プライベート DNS を使用して API を呼び出します。任意のコマンドラインツールを使用できます。この例では [curl](https://curl.se/) を使用してクエリとミューテーションを送信し、[wscat](https://www.npmjs.com/package/wscat) を使用してサブスクリプションを設定します。この例をエミュレートするには、以下のコマンド`{ }`の角括弧内の値を AWS 、アカウントの対応する値に置き換えます。

**ミューテーション操作のテスト — `createTodo` リクエスト **

```
curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \
-H "Content-Type:application/graphql" \
-H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \
-d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'
```

**ミューテーション操作のテスト — `createTodo` 応答**

```
{
    "data": {
        "createTodo": {
            "id": "<todo-id>",
            "name": "My first GraphQL task",
            "where": "Day 1",
            "when": "Friday Night",
            "description": "Learn more about GraphQL"
        }
    }
}
```

**クエリ操作のテスト — `listTodos` リクエスト**

```
curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \
-H "Content-Type:application/graphql" \
-H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \
-d '{"query":"query ListTodos {\n listTodos {\n items {\n description\n id\n name\n when\n where\n }\n }\n}\n","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'
```

**クエリ操作のテスト — `listTodos` リクエスト**

```
{
  "data": {
    "listTodos": {
      "items": [
        {
          "description": "Learn more about GraphQL",
          "id": "<todo-id>",
          "name": "My first GraphQL task",
          "when": "Friday night",
          "where": "Day 1"
        }
      ]
    }
  }
}
```

**サブスクリプション操作のテスト — `createTodo` ミューテーションへのサブスクライブ**

で GraphQL サブスクリプションをセットアップするには AWS AppSync、[「リアルタイム WebSocket クライアントの構築」を参照してください。](https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html)VPC の Amazon EC2 インスタンスから、[wscat](https://github.com/websockets/wscat) を使用して AWS AppSync プライベート API サブスクリプションエンドポイントをテストできます。以下の例では、認可に `API KEY` を使用しています。

```
$ header=`echo '{"host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com","x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}"}' | base64 | tr -d '\n'`
$ wscat -p 13 -s graphql-ws -c  "wss://{api_url_identifier}.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=$header&payload=e30="
Connected (press CTRL+C to quit)
> {"type": "connection_init"}
< {"type":"connection_ack","payload":{"connectionTimeoutMs":300000}}
< {"type":"ka"}
> {"id":"f7a49717","payload":{"data":"{\"query\":\"subscription onCreateTodo {onCreateTodo {description id name where when}}\",\"variables\":{}}","extensions":{"authorization":{"x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}","host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com"}}},"type":"start"}
< {"id":"f7a49717","type":"start_ack"}
```

または、VPC エンドポイントのドメイン名を使用し、`wscat`ウェブソケットを確立するコマンドで必ず **Host** ヘッダーを指定してください。

```
$ header=`echo '{"host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com","x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}"}' | base64 | tr -d '\n'`
$ wscat -p 13 -s graphql-ws -c  "wss://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql?header=$header&payload=e30=" --header Host:{api_url_identifier}.appsync-realtime-api.us-west-2.amazonaws.com
Connected (press CTRL+C to quit)
> {"type": "connection_init"}
< {"type":"connection_ack","payload":{"connectionTimeoutMs":300000}}
< {"type":"ka"}
> {"id":"f7a49717","payload":{"data":"{\"query\":\"subscription onCreateTodo {onCreateTodo {description id priority title}}\",\"variables\":{}}","extensions":{"authorization":{"x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}","host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com"}}},"type":"start"}
< {"id":"f7a49717","type":"start_ack"}
```

以下のミューテーションコードを実行します。

```
curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \
-H "Content-Type:application/graphql" \
-H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \
-d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'
```

その後、サブスクリプションがトリガーされ、次のようなメッセージ通知が表示されます。

```
< {"id":"f7a49717","type":"data","payload":{"data":{"onCreateTodo":{"description":"Go to the shops","id":"169ce516-b7e8-4a6a-88c1-ab840184359f","priority":5,"title":"Go to the shops"}}}}
```

## コントロールプレーンの例
<a name="control-plane-examples"></a>

コントロールプレーン VPC エンドポイントを設定すると、 AWS CLI または SDKs を使用して VPC 内から AWS AppSync リソースを管理できます。一般的なコントロールプレーンオペレーションの例を次に示します。

**を使用した API の作成 AWS CLI**

```
aws appsync create-graphql-api \
  --name "MyPrivateAPI" \
  --authentication-type API_KEY \
  --visibility PRIVATE
```

**スキーマの更新**

```
aws appsync start-schema-creation \
  --api-id {api-id} \
  --definition file://schema.graphql
```

**データソースの作成**

```
aws appsync create-data-source \
  --api-id {api-id} \
  --name "MyDataSource" \
  --type AWS_LAMBDA \
  --lambda-config lambdaFunctionArn=arn:aws:lambda:{region}:{account}:function:MyFunction
```

プライベート DNS を有効にしてコントロールプレーンエンドポイントを使用する場合、これらのコマンドは VPC エンドポイントを介して自動的にルーティングします。プライベート DNS を有効にしない場合は、エンドポイント URL を指定できます。

```
aws appsync create-graphql-api \
  --endpoint-url https://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync.{region}.vpce.amazonaws.com \
  --name "MyPrivateAPI" \
  --authentication-type API_KEY \
  --visibility PRIVATE
```

## IAM ポリシーを使用してパブリック API の作成を制限する
<a name="blocking-public-apis"></a>

AWS AppSync は、プライベート APIs で使用する IAM [`Condition`ステートメント](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html)をサポートしています。`visibility`フィールドを `appsync:CreateGraphqlApi` オペレーションの IAM ポリシーステートメントに含めて、どの IAM ロールとユーザーがプライベート API とパブリック API を作成できるかを制御できます。これにより、IAM 管理者は、ユーザーにプライベート GraphQL API の作成のみを許可する IAM ポリシーを定義できます。ユーザーがパブリック API を作成しようとすると、許可されていないメッセージが届きます。

たとえば、IAM 管理者はプライベート API の作成を許可する次の IAM ポリシーステートメントを作成できます。

```
{
    "Sid": "AllowPrivateAppSyncApis",
    "Effect": "Allow",
    "Action": "appsync:CreateGraphqlApi",
    "Resource": "*",
    "Condition": {
        "ForAnyValue:StringEquals": {
            "appsync:Visibility": "PRIVATE"
        }
    }
}
```

IAM 管理者は、次の[サービスコントロールポリシー](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html)を追加して、 AWS 組織内のすべてのユーザーがプライベート AWS AppSync APIs以外の API を作成するAPIsブロックすることもできます。

```
{
    "Sid": "BlockNonPrivateAppSyncApis",
    "Effect": "Deny",
    "Action": "appsync:CreateGraphqlApi",
    "Resource": "*",
    "Condition": {
        "ForAnyValue:StringNotEquals": {
            "appsync:Visibility": "PRIVATE"
        }
    }
}
```

## VPC PrivateLink のサポート
<a name="privatelink-support"></a>

VPC プライベートリンクのサポートは で利用できます AWS AppSync。PrivateLink を使用すると、トラフィックが AWS ネットワークを離れることなく、 AWS サービスを使用および操作できます。

AWS AppSync は、データプレーンオペレーションとコントロールプレーンオペレーションの両方 AWS PrivateLink をサポートします。
+ **VPCE エンドポイント** (`appsync.<region>.vpce.amazonaws.com`): 次のように、データプレーンおよびコントロールプレーンオペレーションへの VPC アクセスを提供します。
  + コントロールプレーンオペレーション用の **appsync**
  + データプレーンオペレーション用の **appsync-api**