AWS CloudFormation を使用して Amazon VPC リソースを設定する - AWS CloudFormation

AWS CloudFormation を使用して Amazon VPC リソースを設定する

このセクションでは、AWS CloudFormation を使用してAmazon VPC リソースを設定するための例について説明します。VPC を使用すると、AWS 内に仮想ネットワークを作成できます。以下のスニペットでは、ネットワーク要件に合わせて VPC のさまざまな要素を設定する方法を示しています。

VPC で IPv6 Egress-Only インターネットアクセスを有効にする

Egress-Only インターネットゲートウェイを使用すると、VPC 内のインスタンスがインターネットにアクセスできるようになり、インターネット上のリソースがインスタンスと通信できなくなります。次のスニペットは、VPC 内からの IPv6 出力専用インターネットアクセスを有効にします。AWS::EC2::VPC リソースを使用する IPv4 アドレス範囲 10.0.0/16 で VPC を作成します。ルートテーブルは AWS::EC2::RouteTable リソースを使用してこの VPC リソースに関連付けられます。ルートテーブルは VPC 内のインスタンスのルートを管理します。AWS::EC2::EgressOnlyInternetGateway は、Egress-Only インターネットゲートウェイを作成するために使用されます。これにより、VPC内のインスタンスからのアウトバウンドトラフィックの IPv6 通信が可能になり、インバウンドトラフィックは防止されます。AWS::EC2::Route リソースを指定して、すべてのアウトバウンド IPv6 トラフィック (::/0) を Egress-Only インターネットゲートウェイに転送する IPv6 ルートをルートテーブルに作成します。

Egress-Only インターネットゲートウェイの詳細については、「エグレス専用インターネットゲートウェイを使用してアウトバウンド IPv6 トラフィックを有効にする」を参照してください。

JSON

"DefaultIpv6Route": { "Type": "AWS::EC2::Route", "Properties": { "DestinationIpv6CidrBlock": "::/0", "EgressOnlyInternetGatewayId": { "Ref": "EgressOnlyInternetGateway" }, "RouteTableId": { "Ref": "RouteTable" } } }, "EgressOnlyInternetGateway": { "Type": "AWS::EC2::EgressOnlyInternetGateway", "Properties": { "VpcId": { "Ref": "VPC" } } }, "RouteTable": { "Type": "AWS::EC2::RouteTable", "Properties": { "VpcId": { "Ref": "VPC" } } }, "VPC": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.0.0.0/16" } }

YAML

DefaultIpv6Route: Type: "AWS::EC2::Route" Properties: DestinationIpv6CidrBlock: "::/0" EgressOnlyInternetGatewayId: Ref: "EgressOnlyInternetGateway" RouteTableId: Ref: "RouteTable" EgressOnlyInternetGateway: Type: "AWS::EC2::EgressOnlyInternetGateway" Properties: VpcId: Ref: "VPC" RouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: Ref: "VPC" VPC: Type: "AWS::EC2::VPC" Properties: CidrBlock: "10.0.0.0/16"

Elastic Network Interface (ENI) のテンプレートスニペット

Elastic Network Interface (ENI) がアタッチされた Amazon EC2 インスタンスを作成する

次のスニペット例は、AWS::EC2::Instance リソースを使用して Amazon EC2 インスタンスを指定された Amazon VPC とサブネットに作成します。2 つの Elastic Network Interface (ENI) をインスタンスにアタッチし、アタッチされた ENI を通じて Elastic IP アドレスをインスタンスに関連付け、SSH と HTTP アクセス用のセキュリティグループを設定します。ユーザーデータは、インスタンスの作成時に起動設定の一部としてインスタンスに提供されます。ユーザーデータには、確実にインスタンスに渡されるように、base64 フォーマットにエンコードされたスクリプトが含まれています。インスタンスが起動すると、ブートストラッププロセスの一環としてスクリプトが自動的に実行されます。ec2-net-utils をインストールし、ネットワークインターフェースを設定し、HTTP サービスを開始します。

選択したリージョンに基づいて適切な Amazon マシンイメージ (AMI) を決定するために、スニペットは RegionMap マッピング内の値を検索する Fn::FindInMap 関数を使用します。このマッピングは大きい方のテンプレートで定義する必要があります。2 つのネットワークインターフェイスは AWS::EC2::NetworkInterface リソースを使用して作成されます。Elastic IP アドレスは、vpc ドメインに割り当てられた AWS::EC2::EIP リソースを使用して指定されます。これらの Elastic IP アドレスは、AWS::EC2::EIPAssociation リソースを使用してネットワークインターフェイスに関連付けられます。

Outputs セクションでは、スタックが作成された後にアクセスする値またはリソースを定義します。次のスニペットで定義されている出力は InstancePublicIp で、スタックによって作成された EC2 インスタンスのパブリック IP アドレスを表すものです。この出力は、AWS CloudFormation コンソールの [出力] タブまたは aws cloudformation describe-stacks コマンドを使用して取得できます。

Elastic Network Interface の詳細については、「Elastic Network Interface」を参照してください。

JSON

"Resources": { "ControlPortAddress": { "Type": "AWS::EC2::EIP", "Properties": { "Domain": "vpc" } }, "AssociateControlPort": { "Type": "AWS::EC2::EIPAssociation", "Properties": { "AllocationId": { "Fn::GetAtt": [ "ControlPortAddress", "AllocationId" ] }, "NetworkInterfaceId": { "Ref": "controlXface" } } }, "WebPortAddress": { "Type": "AWS::EC2::EIP", "Properties": { "Domain": "vpc" } }, "AssociateWebPort": { "Type": "AWS::EC2::EIPAssociation", "Properties": { "AllocationId": { "Fn::GetAtt": [ "WebPortAddress", "AllocationId" ] }, "NetworkInterfaceId": { "Ref": "webXface" } } }, "SSHSecurityGroup": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "VpcId": { "Ref": "VpcId" }, "GroupDescription": "Enable SSH access via port 22", "SecurityGroupIngress": [ { "CidrIp": "0.0.0.0/0", "FromPort": 22, "IpProtocol": "tcp", "ToPort": 22 } ] } }, "WebSecurityGroup": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "VpcId": { "Ref": "VpcId" }, "GroupDescription": "Enable HTTP access via user-defined port", "SecurityGroupIngress": [ { "CidrIp": "0.0.0.0/0", "FromPort": 80, "IpProtocol": "tcp", "ToPort": 80 } ] } }, "controlXface": { "Type": "AWS::EC2::NetworkInterface", "Properties": { "SubnetId": { "Ref": "SubnetId" }, "Description": "Interface for controlling traffic such as SSH", "GroupSet": [ { "Fn::GetAtt": [ "SSHSecurityGroup", "GroupId" ] } ], "SourceDestCheck": true, "Tags": [ { "Key": "Network", "Value": "Control" } ] } }, "webXface": { "Type": "AWS::EC2::NetworkInterface", "Properties": { "SubnetId": { "Ref": "SubnetId" }, "Description": "Interface for web traffic", "GroupSet": [ { "Fn::GetAtt": [ "WebSecurityGroup", "GroupId" ] } ], "SourceDestCheck": true, "Tags": [ { "Key": "Network", "Value": "Web" } ] } }, "Ec2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Fn::FindInMap": [ "RegionMap", { "Ref": "AWS::Region" }, "AMI" ] }, "KeyName": { "Ref": "KeyName" }, "NetworkInterfaces": [ { "NetworkInterfaceId": { "Ref": "controlXface" }, "DeviceIndex": "0" }, { "NetworkInterfaceId": { "Ref": "webXface" }, "DeviceIndex": "1" } ], "Tags": [ { "Key": "Role", "Value": "Test Instance" } ], "UserData": { "Fn::Base64": { "Fn::Sub": "#!/bin/bash -xe\nyum install ec2-net-utils -y\nec2ifup eth1\nservice httpd start\n" } } } } }, "Outputs": { "InstancePublicIp": { "Description": "Public IP Address of the EC2 Instance", "Value": { "Fn::GetAtt": [ "Ec2Instance", "PublicIp" ] } } }

YAML

Resources: ControlPortAddress: Type: 'AWS::EC2::EIP' Properties: Domain: vpc AssociateControlPort: Type: 'AWS::EC2::EIPAssociation' Properties: AllocationId: Fn::GetAtt: - ControlPortAddress - AllocationId NetworkInterfaceId: Ref: controlXface WebPortAddress: Type: 'AWS::EC2::EIP' Properties: Domain: vpc AssociateWebPort: Type: 'AWS::EC2::EIPAssociation' Properties: AllocationId: Fn::GetAtt: - WebPortAddress - AllocationId NetworkInterfaceId: Ref: webXface SSHSecurityGroup: Type: 'AWS::EC2::SecurityGroup' Properties: VpcId: Ref: VpcId GroupDescription: Enable SSH access via port 22 SecurityGroupIngress: - CidrIp: 0.0.0.0/0 FromPort: 22 IpProtocol: tcp ToPort: 22 WebSecurityGroup: Type: 'AWS::EC2::SecurityGroup' Properties: VpcId: Ref: VpcId GroupDescription: Enable HTTP access via user-defined port SecurityGroupIngress: - CidrIp: 0.0.0.0/0 FromPort: 80 IpProtocol: tcp ToPort: 80 controlXface: Type: 'AWS::EC2::NetworkInterface' Properties: SubnetId: Ref: SubnetId Description: Interface for controlling traffic such as SSH GroupSet: - Fn::GetAtt: - SSHSecurityGroup - GroupId SourceDestCheck: true Tags: - Key: Network Value: Control webXface: Type: 'AWS::EC2::NetworkInterface' Properties: SubnetId: Ref: SubnetId Description: Interface for web traffic GroupSet: - Fn::GetAtt: - WebSecurityGroup - GroupId SourceDestCheck: true Tags: - Key: Network Value: Web Ec2Instance: Type: AWS::EC2::Instance Properties: ImageId: Fn::FindInMap: - RegionMap - Ref: AWS::Region - AMI KeyName: Ref: KeyName NetworkInterfaces: - NetworkInterfaceId: Ref: controlXface DeviceIndex: "0" - NetworkInterfaceId: Ref: webXface DeviceIndex: "1" Tags: - Key: Role Value: Test Instance UserData: Fn::Base64: !Sub | #!/bin/bash -xe yum install ec2-net-utils -y ec2ifup eth1 service httpd start Outputs: InstancePublicIp: Description: Public IP Address of the EC2 Instance Value: Fn::GetAtt: - Ec2Instance - PublicIp