CloudFormation でアウトバウンドをすべて許可したセキュリティグループを作成する方法を教えてください
困っていた内容
すべてのアウトバウンド通信を許可するセキュリティグループを CloudFormation で作成したいです。
次のテンプレートを実行しましたが、通信が許可されません。
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "hato-CloudFormation-SecurityGroup"
GroupDescription: "SecurityGroup created by CloudFormation"
VpcId: "vpc-123abc"
SecurityGroupEgress:
- IpProtocol: 0
CidrIp: '0.0.0.0/0'
すべてのアウトバウンド通信を許可するセキュリティグループを作成するにはどうしたら良いでしょうか。
どう対応すればいいの?
IpProtocol
で-1
を指定してください。
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "hato-CloudFormation-SecurityGroup"
GroupDescription: "SecurityGroup created by CloudFormation"
VpcId: "vpc-123abc"
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: '0.0.0.0/0'
AWS マネジメントコンソールからセキュリティグループを作成すると、デフォルトですべてのアウトバウンドトラフィックを許可するルールが設定されます。
CloudFormation で同様のセキュリティグループを作成する場合、IpProtocol
で「すべて」を意味する-1
を指定してください。
なお、0
を指定するとプロトコル番号:0
となり、HOPOPT(IPv6 Hop-by-Hop Option)プロトコルの指定となります。
CloudFormation に限らず、IaC ツールを使用する際にはご注意ください。
参考資料
IpProtocol
The IP protocol name (tcp, udp, icmp, icmpv6) or number (see Protocol Numbers).Use -1 to specify all protocols.
IpProtocol
The IP protocol name (tcp, udp, icmp, icmpv6) or number (see Protocol Numbers).
Use -1 to specify all protocols.
Decimal Keyword Protocol IPv6 Extension Header Reference 0 HOPOPT IPv6 Hop-by-Hop Option Y [RFC8200]
新しいセキュリティグループには、すべてのトラフィックがリソースを離れることを許可するアウトバウンドルールのみで開始されます。