

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

# Amazon Virtual Private Cloud AWS CodeBuild で を使用する
<a name="vpc-support"></a>

通常、 AWS CodeBuild は VPC 内のリソースにアクセスできません。アクセスできるようにするには、CodeBuild プロジェクト設定で追加の VPC 固有の設定情報を指定する必要があります。これには、VPC ID、VPC サブネット ID、および VPC セキュリティグループ ID が含まれます。これにより、VPC 対応のビルドは VPC 内のリソースにアクセスできます。Amazon VPC で VPC を設定する方法の詳細については、[Amazon VPC ユーザーガイド](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Introduction.html)を参照してください。

**Topics**
+ [ユースケース](#use-cases)
+ [VPC のベストプラクティス](#best-practices-for-vpcs)
+ [VPC の制限事項](#vpc-limitations)
+ [CodeBuild プロジェクトでの Amazon VPC アクセスを許可](enabling-vpc-access-in-projects.md)
+ [VPC 設定のトラブルシューティング](troubleshooting-vpc.md)
+ [VPC エンドポイントの使用](use-vpc-endpoints-with-codebuild.md)
+ [マネージドプロキシサーバー AWS CodeBuild で を使用する](run-codebuild-in-managed-proxy-server.md)
+ [プロキシサーバー AWS CodeBuild で を使用する](use-proxy-server.md)
+ [CloudFormation VPC テンプレート](cloudformation-vpc-template.md)

## ユースケース
<a name="use-cases"></a>

 AWS CodeBuild ビルドからの VPC 接続により、次のことが可能になります。
+ プライベートサブネット上に分離された Amazon RDS データベース内のデータに対して、ビルドから統合テストを実行する。
+ Amazon ElastiCache クラスターのデータをテストから直接クエリする。
+ Amazon EC2、Amazon ECS、または内部 Elastic Load Balancing を使用するサービスでホストされる内部ウェブサービスを操作する。
+ Python 用 PyPI、Java 用 Maven、Node.js 用 npm など、セルフホスト型の内部アーティファクトリポジトリから依存関係を取得する。
+ Amazon VPC エンドポイント経由でのみアクセスできるように設定された S3 バケット内のオブジェクトにアクセスする。
+ 固定 IP アドレスを必要とする外部ウェブサービスを、サブネットに関連付けられた NAT ゲートウェイまたは NAT インスタンスの Elastic IP アドレスを使用してクエリする。

お客様のビルドは、VPC でホストされている任意のリソースにアクセスできます。

## VPC のベストプラクティス
<a name="best-practices-for-vpcs"></a>

CodeBuild を使用するように VPC を設定する場合は、このチェックリストを使用します。
+ パブリックおよびプライベートサブネットと NAT ゲートウェイを使用して VPC を設定します。NAT ゲートウェイはパブリックサブネットにある必要があります。詳細については、*Amazon VPC ユーザーガイド* の「[パブリックサブネットとプライベートサブネットを持つ VPC (NAT)](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html)」を参照してください。
**重要**  
VPC で CodeBuild を使用するには、NAT ゲートウェイまたは NAT インスタンスが必要です。これにより、CodeBuild はパブリックエンドポイントにアクセスできるようになります (ビルドの実行時に CLI コマンドを実行する場合など)。CodeBuild は、作成したネットワークインターフェイスへの Elastic IP アドレスの割り当てをサポートしていないため、NAT ゲートウェイや NAT インスタンスの代わりにインターネットゲートウェイを使用することはできません。また、Amazon EC2 は、 Amazon EC2 インスタンスの起動以外で作成されたネットワークインターフェイスに対しては、パブリック IP アドレスの自動割り当てをサポートしていません。
+ VPC に複数のアベイラビリティーゾーンを含めます。
+ セキュリティグループに、ビルドに許可されたインバウンド (進入) トラフィックがないことを確認します。CodeBuild にはアウトバウンドトラフィックに関する特定の要件はありませんが、GitHub や Amazon S3 など、ビルドに必要なインターネットリソースへのアクセスを許可する必要があります。

  詳細については、「Amazon VPC ユーザーガイド」の「[セキュリティグループルール](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#SecurityGroupRules)」を参照してください。**
+ ビルド用に別個のサブネットを設定します。
+ VPC にアクセスするように CodeBuild プロジェクトを設定する場合、プライベートサブネットのみを選択します。

Amazon VPC で VPC を設定する方法の詳細については、[Amazon VPC ユーザーガイド](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Introduction.html)を参照してください。

 CloudFormation を使用して CodeBuild VPC 機能を使用するように VPC を設定する方法の詳細については、「」を参照してください[CloudFormation VPC テンプレート](cloudformation-vpc-template.md)。

## VPC の制限事項
<a name="vpc-limitations"></a>
+ CodeBuild からの VPC 接続は、共有 VPC ではサポートされていません。

# CodeBuild プロジェクトでの Amazon VPC アクセスを許可
<a name="enabling-vpc-access-in-projects"></a>

以下の設定を VPC 設定に含めます。
+ **[VPC ID]** で、CodeBuild が使用する VPC ID を選択します。
+ **[サブネット]** で、CodeBuild が使用するリソースへのルートを含む NAT 変換を持つプライベートサブネットを選択します。
+ **[セキュリティグループ]** で、CodeBuild が VPC 内のリソースへのアクセスを許可するために使用するセキュリティグループを選択します。



コンソールを使用してビルドプロジェクトを作成する方法については、「[ビルドプロジェクトの作成 (コンソール)](create-project.md#create-project-console)」を参照してください。CodeBuild プロジェクトを作成または変更する場合、**[VPC] **で、VPC ID、サブネット、セキュリティグループを選択します。



を使用してビルドプロジェクト AWS CLI を作成するには、「」を参照してください[ビルドプロジェクトの作成 (AWS CLI)](create-project.md#create-project-cli)。CodeBuild AWS CLI で を使用している場合は、CodeBuild が IAM ユーザーに代わって サービスとやり取りするために使用するサービスロールにポリシーがアタッチされている必要があります。詳細については、「[VPC ネットワークインターフェイスの作成に必要な AWS サービスへの CodeBuild アクセスを許可する](auth-and-access-control-iam-identity-based-access-control.md#customer-managed-policies-example-create-vpc-network-interface)」を参照してください。

*vpcConfig* オブジェクトには、*vpcId*、*securityGroupIds*、および *subnets* が含まれている必要があります。
+ *vpcId*: 必須。CodeBuild で使用される VPC ID。リージョン内の Amazon VPC ID を一覧表示するには、次のコマンドを実行します。

  ```
  aws ec2 describe-vpcs
  ```
+ *subnets*: 必須。CodeBuild で使用されるリソースを含むサブネット ID。この ID を取得するには、次のコマンドを実行します。

  ```
  aws ec2 describe-subnets --filters "Name=vpc-id,Values=<vpc-id>" --region us-east-1
  ```
**注記**  
`us-east-1` は、実際のリージョンに置き換えます。
+ *securityGroupIds*: 必須。VPC 内のリソースへのアクセスを許可するために CodeBuild で使用されるセキュリティグループ ID。この ID を取得するには、次のコマンドを実行します。

  ```
  aws ec2 describe-security-groups --filters "Name=vpc-id,Values=<vpc-id>" --region us-east-1
  ```
**注記**  
`us-east-1` は、実際のリージョンに置き換えます。

# VPC 設定のトラブルシューティング
<a name="troubleshooting-vpc"></a>

エラーメッセージに表示される情報を、問題の特定、診断、対処に役立てます。

一般的な CodeBuild VPC エラー「`Build does not have internet connectivity. Please check subnet network configuration`」のトラブルシューティングに役立つガイドラインを以下に示します。

1. [インターネットゲートウェイが VPC にアタッチされていることを確認します](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html#Add_IGW_Attach_Gateway)。

1. [パブリックサブネットのルートテーブルがインターネットゲートウェイを参照していることを確認します](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html#route-tables-internet-gateway)。

1. [ネットワーク ACL がトラフィックのフローを許可していることを確認します](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#SecurityGroupRules)。

1. [セキュリティグループがトラフィックのフローを許可していることを確認します](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#SecurityGroupRules)。

1. [NAT ゲートウェイのトラブルシューティングを行います](https://docs.aws.amazon.com/vpc/latest/userguide/VPC-nat-gateway.html#nat-gateway-troubleshooting)。

1. [プライベートサブネットのルートテーブルが NAT ゲートウェイを参照していることを確認します](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html#route-tables-nat)。

1. IAM ユーザーに代わってサービスを操作するために CodeBuild が使用するサービスロールに、[このポリシー](https://docs.aws.amazon.com/codebuild/latest/userguide/auth-and-access-control-iam-identity-based-access-control.html#customer-managed-policies-example-create-vpc-network-interface)のアクセス許可が付与されていることを確認します。詳細については、「[CodeBuild が他の AWS サービスとやり取りすることを許可する](setting-up-service-role.md)」を参照してください。

   CodeBuild にアクセス許可がない場合は、「`Unexpected EC2 error: UnauthorizedOperation`」というエラーが表示されることがあります。このエラーは、VPC を使用するために必要な Amazon EC2 へのアクセス許可を CodeBuild が持っていない場合に発生することがあります。

# VPC エンドポイントの使用
<a name="use-vpc-endpoints-with-codebuild"></a>

インターフェイス VPC エンドポイントを使用する AWS CodeBuild ように を設定することで、ビルドのセキュリティを向上させることができます。インターフェイスエンドポイントは、プライベート IP アドレスを通じて Amazon EC2 および CodeBuild にプライベートにアクセスできるテクノロジーである PrivateLink を使用しています。PrivateLink は、マネージドインスタンス、CodeBuild および Amazon EC2 間のすべてのネットワークトラフィックを Amazon ネットワークに限定します。(マネージドインスタンスはインターネットにアクセスできません)。また、インターネットゲートウェイ、NAT デバイスあるいは仮想プライベートゲートウェイの必要はありません。PrivateLink の設定は要件ではありませんが、推奨されます。PrivateLink エンドポイントと VPC エンドポイントの詳細については、[「 とは AWS PrivateLink](https://docs.aws.amazon.com/vpc/latest/privatelink/what-is-privatelink.html)」を参照してください。

## VPC エンドポイントを作成する前に
<a name="vpc-endpoints-before-you-begin"></a>

 の VPC エンドポイントを設定する前に AWS CodeBuild、次の制限と制約に注意してください。

**注記**  
 Amazon VPC PrivateLink 接続をサポートしていない AWS サービスで CodeBuild を使用する場合は、[NAT ゲートウェイ](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html)を使用します。
+  VPC エンドポイントは、Amazon Route 53 を介してのみ Amazon 提供の DNS をサポートします。独自の DNS を使用する場合には、条件付き DNS 転送を使用できます。詳細については、「Amazon VPC ユーザーガイド」の「[DHCP オプションセット](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_DHCP_Options.html)」を参照してください。
+  現在、VPC エンドポイントはクロスリージョンリクエストをサポートしていません。ビルドの入出力を保存する S3 バケットと同じ AWS リージョンにエンドポイントを作成してください。バケットの場所は、Amazon S3 コンソールまたは [get-bucket-location](https://docs.aws.amazon.com/cli/latest/reference/s3api/get-bucket-location.html)コマンドを使用して確認できます。リージョン固有の Amazon S3 エンドポイントを使用してバケットにアクセスします (例: `<bucket-name>.s3-us-west-2.amazonaws.com`)。Amazon S3 のリージョン固有のエンドポイントの詳細については、「Amazon Web Services 全般のリファレンス」の「[Amazon Simple Storage Service](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region)」を参照してください。を使用して Amazon S3 にリクエスト AWS CLI を行う場合は、デフォルトのリージョンをバケットが作成されたリージョンと同じリージョンに設定するか、リクエストで `--region`パラメータを使用します。

## CodeBuild の VPC エンドポイントを作成
<a name="creating-vpc-endpoints"></a>

「[インターフェイスエンドポイントの作成](https://docs.aws.amazon.com/vpc/latest/userguide/vpce-interface.html#create-interface-endpoint)」の手順に従って、エンドポイント `com.amazonaws.region.codebuild` を作成します。これは の VPC エンドポイントです AWS CodeBuild。

![\[VPC エンドポイント設定。\]](http://docs.aws.amazon.com/ja_jp/codebuild/latest/userguide/images/vpc-endpoint.png)


 *region* は、米国東部 (オハイオ) AWS リージョンなど、CodeBuild でサポートされているリージョン`us-east-2`のリージョン識別子を表します。サポートされている AWS リージョンのリストについては、「 * AWS 全般のリファレンス*」の[CodeBuild](https://docs.aws.amazon.com/general/latest/gr/rande.html#codebuild_region)」を参照してください。エンドポイントには、サインイン時に指定したリージョンがあらかじめ入力されています AWS。リージョンを変更すると、それに応じて VPC エンドポイントが更新されます。

## CodeBuild 用の VPC エンドポイントポリシーを作成する
<a name="creating-vpc-endpoint-policy"></a>

 Amazon VPC エンドポイントのポリシーを作成し、 AWS CodeBuild 以下を指定できます。
+ アクションを実行できるプリンシパル。
+ 実行可能なアクション。
+ 自身に対してアクションを実行できたリソース。

次のポリシー例では、すべてのプリンシパルが `project-name` プロジェクトのビルドの開始と表示のみ行えることを示します。

```
{
    "Statement": [
        {
            "Action": [
                "codebuild:ListBuildsForProject",
                "codebuild:StartBuild",
                "codebuild:BatchGetBuilds"
            ],
            "Effect": "Allow",
            "Resource": "arn:aws:codebuild:region-ID:account-ID:project/project-name",
            "Principal": "*"
        }
    ]
}
```

 詳細については、*Amazon VPC ユーザーガイド*の「[VPC エンドポイントによるサービスのアクセスコントロール](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints-access.html)」を参照してください。

# マネージドプロキシサーバー AWS CodeBuild で を使用する
<a name="run-codebuild-in-managed-proxy-server"></a>

 マネージドプロキシサーバーで AWS CodeBuild リザーブドキャパシティフリートを実行するには、プロキシルールを使用して外部サイトとの間のトラフィックを許可または拒否するようにプロキシサーバーを設定する必要があります。なお、マネージドプロキシサーバーでのリザーブドキャパシティフリートの実行は、VPC、Windows、または MacOS ではサポートされていません。

**重要**  
フリートにプロキシ設定が存在する期間に応じて、追加料金が発生します。詳細については、[https://aws.amazon.com/codebuild/pricing/](https://aws.amazon.com/codebuild/pricing/) を参照してください。

**Topics**
+ [リザーブドキャパシティフリートのマネージドプロキシ設定を構成](#run-codebuild-in-managed-proxy-server-configure)
+ [CodeBuild リザーブドキャパシティフリートを実行](#use-managed-server-run-acb-fleet)

## リザーブドキャパシティフリートのマネージドプロキシ設定を構成
<a name="run-codebuild-in-managed-proxy-server-configure"></a>

 リザーブドキャパシティフリート用にマネージドプロキシサーバーを設定するには、コンソールでフリートを作成するとき、または AWS CLIを使用してこの機能を有効にする必要があります。定義する必要があるプロパティがいくつかあります。

**[プロキシ設定を定義 - オプション]**  
リザーブドキャパシティインスタンスにネットワークアクセスコントロールを適用するプロキシ設定。

**デフォルトの動作**  
送信トラフィックの動作を定義します。    
**許可**  
デフォルトでは、すべての送信先への送信トラフィックを許可します。  
**拒否**  
デフォルトでは、すべての送信先への送信トラフィックを拒否します。

**[プロキシルール]**  
ネットワークアクセスコントロールを制限する送信先ドメインを指定します。

コンソールでプロキシ設定を定義する手順については、「[リザーブドキャパシティフリートを作成](fleets.md#fleets.how-to)」を参照してください。を使用してプロキシ設定を定義するには AWS CLI、次の JSON 構文を変更し、結果を保存します。

```
"proxyConfiguration": {
    "defaultBehavior": "ALLOW_ALL" | "DENY_ALL",
    "orderedProxyRules": [
        {
            "type": "DOMAIN" | "IP",
            "effect": "ALLOW" | "DENY",
            "entities": [
                "destination"
            ]
        }
    ]
}
```

JSON ファイルは次のようになります。

```
"proxyConfiguration": {
    "defaultBehavior": "DENY_ALL",
    "orderedProxyRules": [
        {
            "type": "DOMAIN",
            "effect": "ALLOW",
            "entities": [
                "github.com"
            ]
        }
    ]
}
```

## CodeBuild リザーブドキャパシティフリートを実行
<a name="use-managed-server-run-acb-fleet"></a>

 マネージドプロキシサーバーで AWS CodeBuild リザーブドキャパシティフリートを実行すると、CodeBuild はマネージドプロキシアドレスを使用して `HTTP_PROXY`および `HTTPS_PROXY`環境変数を自動的に設定します。依存関係のあるソフトウェアに独自の設定があり、環境変数に準拠していない場合は、ビルドコマンドでこれらの値を参照し、ソフトウェア設定を更新して、マネージドプロキシ経由でビルドトラフィックを適切にルーティングできます。詳細については、「[でのビルドプロジェクトの作成AWS CodeBuild](create-project.md)」および「[でビルドプロジェクト設定を変更する AWS CodeBuild](change-project.md)」を参照してください。

# プロキシサーバー AWS CodeBuild で を使用する
<a name="use-proxy-server"></a>

 プロキシサーバー AWS CodeBuild で を使用して、インターネットとの間で送受信される HTTP および HTTPS トラフィックを規制できます。プロキシサーバーで CodeBuild を実行するには、パブリックサブネットにプロキシサーバーをインストールし、VPC のプライベートサブネットに CodeBuild をインストールします。

プロキシサーバーで CodeBuild を実行するためのプライマリユースケースが 2 つあります。
+  これにより、VPC で NAT ゲートウェイや NAT インスタンスを使用する必要がなくなります。
+  プロキシサーバーのインスタンスがアクセスを許可する URL と、プロキシサーバーがアクセスを拒否する URL を指定できます。

 CodeBuild は、2 種類のプロキシサーバーで使用できます。どちらの場合も、プロキシサーバーはパブリックサブネットで動作し、CodeBuild はプライベートサブネットで動作します。
+  **明示的なプロキシ**: 明示的なプロキシサーバーを使用する場合は、`NO_PROXY`、`HTTP_PROXY`、および `HTTPS_PROXY` の環境変数を CodeBuild のプロジェクトレベルで設定する必要があります。詳細については、「[でビルドプロジェクト設定を変更する AWS CodeBuild](change-project.md)」および「[でのビルドプロジェクトの作成AWS CodeBuild](create-project.md)」を参照してください。
+  **Transparent Proxy**: 透過的なプロキシサーバーを使用する場合は、特別な設定は必要ありません。

**Topics**
+ [プロキシサーバーで CodeBuild を実行するために必要なコンポーネントを設定](use-proxy-server-transparent-components.md)
+ [明示的なプロキシサーバーでの CodeBuild の実行](run-codebuild-in-explicit-proxy-server.md)
+ [透過的なプロキシサーバーでの CodeBuild の実行](run-codebuild-in-transparent-proxy-server.md)
+ [プロキシサーバーでのパッケージマネージャーなどのツールの実行](use-proxy-server-tools.md)

# プロキシサーバーで CodeBuild を実行するために必要なコンポーネントを設定
<a name="use-proxy-server-transparent-components"></a>

 これらのコンポーネントは、透過的または明示的なプロキシサーバー AWS CodeBuild で実行する必要があります。
+  VPC。
+  プロキシサーバー用に VPC 内の 1 つのパブリックサブネット。
+  CodeBuild 用に VPC 内の 1 つのプライベートサブネット。
+  VPC とインターネットの間の通信を可能にするインターネットゲートウェイ。

 次の図は、これらのコンポーネントがどのように連携するかを示しています。

![\[この図では、コンポーネントがどのように連携するかを示しています。\]](http://docs.aws.amazon.com/ja_jp/codebuild/latest/userguide/images/codebuild-proxy-transparent.png)


## VPC、サブネット、ネットワークゲートウェイのセットアップ
<a name="use-proxy-server-transparent-setup"></a>

 透過的または明示的なプロキシサーバー AWS CodeBuild で を実行するには、次の手順が必要です。

1. VPC を作成します。VPC の作成の詳細については、「[Amazon VPC ユーザーガイド](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-vpcs.html#Create-VPC)」の「*VPC を作成する*」をご参照ください。

1. VPC 内に 2 つのサブネットを作成します。1 つは、プロキシサーバーを実行する `Public Subnet` という名前のパブリックサブネットです。もう 1 つは、CodeBuild を実行する `Private Subnet` という名前のプライベートサブネットです。

   詳細については、「[VPC でのサブネットの作成](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-vpcs.html#AddaSubnet)」を参照してください。

1.  インターネットゲートウェイを作成して VPC にアタッチします。詳細については、「[インターネットゲートウェイの作成とアタッチ](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html#Add_IGW_Attach_Gateway)」を参照してください。

1.  VPC (0.0.0.0/0) からインターネットゲートウェイに送信トラフィックをルーティングするルールをデフォルトルートテーブルに追加します。詳細については、「[ルートテーブルでのルートの追加および削除](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html#AddRemoveRoutes)」を参照してください。

1.  VPC (0.0.0.0/0) からの着信 SSH トラフィック (TCP 22) を許可するルールを VPC のデフォルトセキュリティグループに追加します。

1.  「*Amazon EC2 ユーザーガイド*」の「[コンソールのインスタンス起動ウィザードを使用して EC2 インスタンスを起動する](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html)」の指示に従って Amazon Linux インスタンスを起動します。ウィザードを実行する場合は次のオプションを選択してください。
   +  **[インスタンスタイプの選択]** で、Amazon Linux の Amazon マシンイメージ (AMI) を選択します。
   +  [**サブネット**] で、このトピックで先に作成したパブリックサブネットを選択します。推奨された名前を使用した場合は、[**Public Subnet**] です。
   +  [**Auto-assign Public IP**] で、[**Enable**] を選択します。
   +  [**セキュリティグループの設定**] ページの [**セキュリティグループの割り当て**] で、[**Select an existing security group (既存のセキュリティグループの選択)**] を選択します。次に、デフォルトのセキュリティグループを選択します。
   +  [**起動**] を選択したら、既存のキーペアを選択するか、新しいキーペアを作成します。

    それ以外のオプションについては、デフォルト設定を選択します。

1.  EC2 インスタンスの実行後は、送信元/送信先チェックを無効にします。詳細については、「[Amazon VPC ユーザーガイド](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html#EIP_Disable_SrcDestCheck)」の「*Disabling Source/Destination checks*」を参照してください。

1.  VPC にルートテーブルを作成します。インターネット用のトラフィックをプロキシサーバーにルーティングするためのルールをルートテーブルに追加します。このルートテーブルをプライベートサブネットに関連付けます。これは、CodeBuild が実行されているプライベートサブネット内のインスタンスからのアウトバウンドリクエストを、常にプロキシサーバーを介してルーティングするために必要です。

## プロキシサーバーのインストールと設定
<a name="use-proxy-server-squid-install"></a>

 選択できるプロキシサーバーは多数あります。ここでは、オープンソースのプロキシサーバー Squid を使用して、プロキシサーバーで AWS CodeBuild がどのように実行されるかを示します。同じ概念を他のプロキシサーバーにも適用できます。

 Squid をインストールするには、次のコマンドを実行して yum repo を使用します。

```
sudo yum update -y
sudo yum install -y squid
```

 Squid をインストールしたら、このトピックで後述する手順に従って、その `squid.conf` ファイルを編集します。

## HTTPS トラフィック用の Squid の設定
<a name="use-proxy-server-squid-configure-https"></a>

 HTTPS では、HTTP トラフィックは Transport Layer Security (TLS) 接続でカプセル化されます。Squid では、[SslPeekAndSplice](https://wiki.squid-cache.org/Features/SslPeekAndSplice) と呼ばれる機能を使用して、リクエストされたインターネットホストを含む TLS 初期化から Server Name Indication (SNI) を取得します。これは必須のため、Squid で HTTPS トラフィックを復元する必要はありません。SslPeekAndSplice を有効にするには、Squid に証明書が必要です。OpenSSL を使用してこの証明書を作成する: 

```
sudo mkdir /etc/squid/ssl
cd /etc/squid/ssl
sudo openssl genrsa -out squid.key 2048
sudo openssl req -new -key squid.key -out squid.csr -subj "/C=XX/ST=XX/L=squid/O=squid/CN=squid"
sudo openssl x509 -req -days 3650 -in squid.csr -signkey squid.key -out squid.crt
sudo cat squid.key squid.crt | sudo tee squid.pem
```

**注記**  
 HTTP では、Squid の設定は必要ありません。すべての HTTP/1.1 リクエストメッセージから、ホストヘッダーフィールドを取得することができます。これにより、リクエストされているインターネットホストが指定されます。

# 明示的なプロキシサーバーでの CodeBuild の実行
<a name="run-codebuild-in-explicit-proxy-server"></a>

 明示的なプロキシサーバー AWS CodeBuild で を実行するには、外部サイトとの間のトラフィックを許可または拒否するようにプロキシサーバーを設定し、 `HTTP_PROXY`および `HTTPS_PROXY`環境変数を設定する必要があります。

**Topics**
+ [明示的なプロキシサーバーとしての Squid の設定](#use-proxy-server-explicit-squid-configure)
+ [CodeBuild プロジェクトを作成する](#use-proxy-server-explicit-create-acb-project)
+ [明示的なプロキシサーバーのサンプル `squid.conf` ファイル](#use-proxy-server-explicit-sample-squid-conf)

## 明示的なプロキシサーバーとしての Squid の設定
<a name="use-proxy-server-explicit-squid-configure"></a>

 Squid プロキシサーバーが明示的になるように設定するには、`/etc/squid/squid.conf` ファイルに次の変更を加える必要があります。
+  以下のデフォルトのアクセスコントロールリスト (ACL) ルールを削除します。

  ```
  acl localnet src 10.0.0.0/8     
  acl localnet src 172.16.0.0/12  
  acl localnet src 192.168.0.0/16 
  acl localnet src fc00::/7       
  acl localnet src fe80::/10
  ```

   削除したデフォルトの ACL ルールの代わりに次のコードを追加します。最初の行では VPC からのリクエストを許可します。次の 2 つの行では、 AWS CodeBuildによって使用されている可能性のある送信先 URL へのアクセス権をプロキシサーバーに付与します。最後の行の正規表現を編集して、 AWS リージョンの S3 バケットまたは CodeCommit リポジトリを指定します。例えば、次のようになります。
  + 送信元が Amazon S3 の場合は、**acl download\$1src dstdom\$1regex .\$1s3\$1.us-west-1\$1.amazonaws\$1.com** コマンドを使用して、`us-west-1` リージョンの S3 バケットへのアクセスを許可します。
  +  ソースが の場合は AWS CodeCommit、 `git-codecommit.<your-region>.amazonaws.com` を使用して AWS リージョンを許可リストに追加します。

  ```
  acl localnet src 10.1.0.0/16 #Only allow requests from within the VPC
  acl allowed_sites dstdomain .github.com #Allows to download source from GitHub
  acl allowed_sites dstdomain .bitbucket.com #Allows to download source from Bitbucket
  acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from Amazon S3 or CodeCommit
  ```
+  `http_access allow localnet` を次のように置き換えます。

  ```
  http_access allow localnet allowed_sites
  http_access allow localnet download_src
  ```
+ ビルドでログとアーティファクトをアップロードする場合は、次のいずれかを実行します。

  1. `http_access deny all` ステートメントの前に、次のステートメントを挿入します。これにより、CodeBuild が CloudWatch と Amazon S3 にアクセスできるようになります。CodeBuild が CloudWatch Logs を作成できるようにするには、CloudWatch へのアクセスが必要です。Amazon S3 へのアクセスは、アーティファクトのアップロードと Amazon S3 のキャッシングを行う上で必要です。
     + 

       ```
       https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
       acl SSL_port port 443
       http_access allow SSL_port
       acl allowed_https_sites ssl::server_name .amazonaws.com
       acl step1 at_step SslBump1
       acl step2 at_step SslBump2
       acl step3 at_step SslBump3
       ssl_bump peek step1 all
       ssl_bump peek step2 allowed_https_sites
       ssl_bump splice step3 allowed_https_sites
       ssl_bump terminate step2 all
       ```
     + `squid.conf` を保存した後、次のコマンドを実行します。

       ```
       sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130
       sudo service squid restart
       ```

  1.  `proxy` を buildspec ファイルに追加します。詳細については、「[buildspec の構文](build-spec-ref.md#build-spec-ref-syntax)」を参照してください。

     ```
     version: 0.2
     proxy:
       upload-artifacts: yes
       logs: yes
     phases:
       build:
         commands:
           - command
     ```

**注記**  
RequestError タイムアウトエラーが表示される場合は、「[プロキシサーバーで CodeBuild を実行しているときの RequestError タイムアウトエラー](troubleshooting.md#code-request-timeout-error)」を参照してください。

詳細については、このトピックで後述する「[明示的なプロキシサーバーのサンプル `squid.conf` ファイル](#use-proxy-server-explicit-sample-squid-conf)」を参照してください。

## CodeBuild プロジェクトを作成する
<a name="use-proxy-server-explicit-create-acb-project"></a>

 明示的なプロキシサーバー AWS CodeBuild で を実行するには、プロキシサーバー用に作成した EC2 インスタンスのプライベート IP アドレスと、プロジェクトレベルでポート 3128 を使用して、 および `HTTP_PROXY``HTTPS_PROXY`環境変数を設定します。プライベート IP アドレスは、`http://your-ec2-private-ip-address:3128` のようになります。詳細については、「[でのビルドプロジェクトの作成AWS CodeBuild](create-project.md)」および「[でビルドプロジェクト設定を変更する AWS CodeBuild](change-project.md)」を参照してください。

 Squid プロキシのアクセスログを表示するには、次のコマンドを使用します。

```
sudo tail -f /var/log/squid/access.log
```

## 明示的なプロキシサーバーのサンプル `squid.conf` ファイル
<a name="use-proxy-server-explicit-sample-squid-conf"></a>

 明示的なプロキシサーバー用に設定した `squid.conf` ファイルの例を次に示します。

```
  acl localnet src 10.0.0.0/16 #Only allow requests from within the VPC
  # add all URLS to be whitelisted for download source and commands to be run in build environment
  acl allowed_sites dstdomain .github.com    #Allows to download source from github
  acl allowed_sites dstdomain .bitbucket.com #Allows to download source from bitbucket
  acl allowed_sites dstdomain ppa.launchpad.net #Allows to run apt-get in build environment
  acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from S3 or CodeCommit
  acl SSL_ports port 443
  acl Safe_ports port 80		# http
  acl Safe_ports port 21		# ftp
  acl Safe_ports port 443		# https
  acl Safe_ports port 70		# gopher
  acl Safe_ports port 210		# wais
  acl Safe_ports port 1025-65535	# unregistered ports
  acl Safe_ports port 280		# http-mgmt
  acl Safe_ports port 488		# gss-http
  acl Safe_ports port 591		# filemaker
  acl Safe_ports port 777		# multiling http
  acl CONNECT method CONNECT
  #
  # Recommended minimum Access Permission configuration:
  #
  # Deny requests to certain unsafe ports
  http_access deny !Safe_ports
  # Deny CONNECT to other than secure SSL ports
  http_access deny CONNECT !SSL_ports
  # Only allow cachemgr access from localhost
  http_access allow localhost manager
  http_access deny manager
  # We strongly recommend the following be uncommented to protect innocent
  # web applications running on the proxy server who think the only
  # one who can access services on "localhost" is a local user
  #http_access deny to_localhost
  #
  # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
  #
  # Example rule allowing access from your local networks.
  # Adapt localnet in the ACL section to list your (internal) IP networks
  # from where browsing should be allowed
  http_access allow localnet allowed_sites
  http_access allow localnet download_src
  http_access allow localhost
  # Add this for CodeBuild to access CWL end point, caching and upload artifacts S3 bucket end point
  https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
  acl SSL_port port 443
  http_access allow SSL_port
  acl allowed_https_sites ssl::server_name .amazonaws.com
  acl step1 at_step SslBump1
  acl step2 at_step SslBump2
  acl step3 at_step SslBump3
  ssl_bump peek step1 all
  ssl_bump peek step2 allowed_https_sites
  ssl_bump splice step3 allowed_https_sites
  ssl_bump terminate step2 all
  # And finally deny all other access to this proxy
  http_access deny all
  # Squid normally listens to port 3128
  http_port 3128
  # Uncomment and adjust the following to add a disk cache directory.
  #cache_dir ufs /var/spool/squid 100 16 256
  # Leave coredumps in the first cache dir
  coredump_dir /var/spool/squid
  #
  # Add any of your own refresh_pattern entries above these.
  #
  refresh_pattern ^ftp:		1440	20%	10080
  refresh_pattern ^gopher:	1440	0%	1440
  refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
  refresh_pattern .		0	20%	4320
```

# 透過的なプロキシサーバーでの CodeBuild の実行
<a name="run-codebuild-in-transparent-proxy-server"></a>

 透過的なプロキシサーバー AWS CodeBuild で を実行するには、プロキシサーバーとやり取りするウェブサイトとドメインへのアクセスを設定する必要があります。

**Topics**
+ [透過的なプロキシサーバーとしての Squid の設定](#use-proxy-server-transparent-squid-configure)
+ [CodeBuild プロジェクトを作成する](#use-proxy-server-transparent-create-acb-project)

## 透過的なプロキシサーバーとしての Squid の設定
<a name="use-proxy-server-transparent-squid-configure"></a>

 プロキシサーバーが透過的になるように設定するには、アクセスするドメインやウェブサイトへのアクセス権を付与する必要があります。透過的なプロキシサーバー AWS CodeBuild で を実行するには、 へのアクセスを許可する必要があります`amazonaws.com`。また、CodeBuild で使用する他のウェブサイトへのアクセス権も付与します。これらのアクセス権は、CodeBuild プロジェクトの作成方法によって異なります。ウェブサイトの例は、GitHub、Bitbucket、Yum、Maven などのリポジトリ用です。特定のドメインやウェブサイトへのアクセスを Squid に許可するには、次のようなコマンドを使用して `squid.conf` ファイルを更新します。このサンプルコマンドは `amazonaws.com`、`github.com`、および `bitbucket.com` へのアクセスを許可します。このサンプルは、他のウェブサイトへのアクセス権を付与するように編集できます。

```
cat | sudo tee /etc/squid/squid.conf ≪EOF
visible_hostname squid
#Handling HTTP requests
http_port 3129 intercept
acl allowed_http_sites dstdomain .amazonaws.com
#acl allowed_http_sites dstdomain domain_name [uncomment this line to add another domain]
http_access allow allowed_http_sites
#Handling HTTPS requests
https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
acl SSL_port port 443
http_access allow SSL_port
acl allowed_https_sites ssl::server_name .amazonaws.com
acl allowed_https_sites ssl::server_name .github.com
acl allowed_https_sites ssl::server_name .bitbucket.com
#acl allowed_https_sites ssl::server_name [uncomment this line to add another website]
acl step1 at_step SslBump1
acl step2 at_step SslBump2
acl step3 at_step SslBump3
ssl_bump peek step1 all
ssl_bump peek step2 allowed_https_sites
ssl_bump splice step3 allowed_https_sites
ssl_bump terminate step2 all
http_access deny all
EOF
```

 プライベートサブネット内のインスタンスからの着信リクエストで、Squid ポートにリダイレクトする必要があります。Squid は HTTP トラフィック (80 の代理) をポート 3129、HTTPS トラフィック (443 の代理) をポート 3130 でリッスンします。トラフィックをルーティングするには、**iptables** コマンドを使用します。

```
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3129
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130
sudo service iptables save
sudo service squid start
```

## CodeBuild プロジェクトを作成する
<a name="use-proxy-server-transparent-create-acb-project"></a>

 プロキシサーバーを設定したら、設定しなくてもプライベートサブネット AWS CodeBuild の で使用できます。HTTP および HTTPS リクエストはすべて、パブリックプロキシサーバーを経由します。Squid プロキシのアクセスログを表示するには、次のコマンドを使用します。

```
sudo tail -f /var/log/squid/access.log
```

# プロキシサーバーでのパッケージマネージャーなどのツールの実行
<a name="use-proxy-server-tools"></a>

次の手順で、パッケージマネージャーやその他のツールをプロキシサーバーで実行します。

**パッケージマネージャーなどのツールをプロキシサーバーで実行する方法**

1.  `squid.conf` ファイルにステートメントを追加し、プロキシサーバーの許可リストにツールを追加します。

1.  プロキシサーバーのプライベートエンドポイントを指す行を buildspec ファイルに追加します。

 次の例では、`apt-get`、`curl`、および `maven` でこの作業を行う方法を示しています。別のツールを使用する場合は、同じ原則が適用されます。`squid.conf` ファイルの許可リストに追加し、コマンドを buildspec ファイルに追加して、プロキシサーバーのエンドポイントを CodeBuild に認識させます。

**プロキシサーバーで `apt-get` を実行するには**

1. 次のステートメントを `squid.conf` ファイルに追加し、プロキシサーバーの許可リストに `apt-get` を追加します。最初の 3 行は、`apt-get` がビルド環境で実行できるようにします。

   ```
   acl allowed_sites dstdomain ppa.launchpad.net # Required for apt-get to run in the build environment
   acl apt_get dstdom_regex .*\.launchpad.net # Required for CodeBuild to run apt-get in the build environment
   acl apt_get dstdom_regex .*\.ubuntu.com    # Required for CodeBuild to run apt-get in the build environment
   http_access allow localnet allowed_sites
   http_access allow localnet apt_get
   ```

1. `apt-get` コマンドで `/etc/apt/apt.conf.d/00proxy` のプロキシ設定を検索できるように、次のステートメントを buildspec ファイルを追加します。

   ```
   echo 'Acquire::http::Proxy "http://<private-ip-of-proxy-server>:3128"; Acquire::https::Proxy "http://<private-ip-of-proxy-server>:3128"; Acquire::ftp::Proxy "http://<private-ip-of-proxy-server>:3128";' > /etc/apt/apt.conf.d/00proxy
   ```

**プロキシサーバーで `curl` を実行するには**

1.  次の内容を `squid.conf` ファイルに追加し、ビルド環境の許可リストに `curl` を追加します。

   ```
   acl allowed_sites dstdomain ppa.launchpad.net # Required to run apt-get in the build environment
   acl allowed_sites dstdomain google.com # Required for access to a webiste. This example uses www.google.com.
   http_access allow localnet allowed_sites
   http_access allow localnet apt_get
   ```

1.  `curl` でプライベートプロキシサーバーを使用して `squid.conf` に追加したウェブサイトにアクセスできるように、次のステートメントを buildspec ファイルに追加します。この例では、ウェブサイトは `google.com` です。

   ```
   curl -x <private-ip-of-proxy-server>:3128 https://www.google.com
   ```

**プロキシサーバーで `maven` を実行するには**

1.  次の内容を `squid.conf` ファイルに追加し、ビルド環境の許可リストに `maven` を追加します。

   ```
   acl allowed_sites dstdomain ppa.launchpad.net # Required to run apt-get in the build environment
   acl maven dstdom_regex .*\.maven.org # Allows access to the maven repository in the build environment
   http_access allow localnet allowed_sites
   http_access allow localnet maven
   ```

1. buildspec ファイルに次のステートメントを追加します。

   ```
   maven clean install -DproxySet=true -DproxyHost=<private-ip-of-proxy-server> -DproxyPort=3128
   ```

# CloudFormation VPC テンプレート
<a name="cloudformation-vpc-template"></a>

CloudFormation では、テンプレートファイルを使用してリソース群を単体 (*スタック*) としてまとめて作成および削除することで、AWS インフラストラクチャのデプロイを想定どおりに繰り返し作成およびプロビジョンできます。詳細については、[CloudFormation ユーザーガイド](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)を参照してください。

CloudFormation を使用するように VPC を設定するための AWS CodeBuild YAML テンプレートは次のとおりです。このファイルは「[samples.zip](./samples/samples.zip)」からも入手可能です。

```
Description:  This template deploys a VPC, with a pair of public and private subnets spread
  across two Availability Zones. It deploys an internet gateway, with a default
  route on the public subnets. It deploys a pair of NAT gateways (one in each AZ),
  and default routes for them in the private subnets.

Parameters:
  EnvironmentName:
    Description: An environment name that is prefixed to resource names
    Type: String

  VpcCIDR:
    Description: Please enter the IP range (CIDR notation) for this VPC
    Type: String
    Default: 10.192.0.0/16

  PublicSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
    Type: String
    Default: 10.192.10.0/24

  PublicSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
    Type: String
    Default: 10.192.11.0/24

  PrivateSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
    Type: String
    Default: 10.192.20.0/24

  PrivateSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
    Type: String
    Default: 10.192.21.0/24

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Subnet (AZ1)

  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: !Ref PublicSubnet2CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Subnet (AZ2)

  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet1CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Private Subnet (AZ1)

  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet2CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Private Subnet (AZ2)

  NatGateway1EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

  NatGateway2EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

  NatGateway1:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway1EIP.AllocationId
      SubnetId: !Ref PublicSubnet1

  NatGateway2:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway2EIP.AllocationId
      SubnetId: !Ref PublicSubnet2

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Routes

  DefaultPublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1

  PublicSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet2


  PrivateRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Private Routes (AZ1)

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway1

  PrivateSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      SubnetId: !Ref PrivateSubnet1

  PrivateRouteTable2:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Private Routes (AZ2)

  DefaultPrivateRoute2:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway2

  PrivateSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      SubnetId: !Ref PrivateSubnet2

  NoIngressSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: "no-ingress-sg"
      GroupDescription: "Security group with no ingress rule"
      VpcId: !Ref VPC

Outputs:
  VPC:
    Description: A reference to the created VPC
    Value: !Ref VPC

  PublicSubnets:
    Description: A list of the public subnets
    Value: !Join [ ",", [ !Ref PublicSubnet1, !Ref PublicSubnet2 ]]

  PrivateSubnets:
    Description: A list of the private subnets
    Value: !Join [ ",", [ !Ref PrivateSubnet1, !Ref PrivateSubnet2 ]]

  PublicSubnet1:
    Description: A reference to the public subnet in the 1st Availability Zone
    Value: !Ref PublicSubnet1

  PublicSubnet2:
    Description: A reference to the public subnet in the 2nd Availability Zone
    Value: !Ref PublicSubnet2

  PrivateSubnet1:
    Description: A reference to the private subnet in the 1st Availability Zone
    Value: !Ref PrivateSubnet1

  PrivateSubnet2:
    Description: A reference to the private subnet in the 2nd Availability Zone
    Value: !Ref PrivateSubnet2

  NoIngressSecurityGroup:
    Description: Security group with no ingress rule
    Value: !Ref NoIngressSecurityGroup
```