0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SecurityGroupルールのCidrIpの省略と !Ref "AWS::NoValue" と "0.0.0.0/0"

Posted at

AWS CloudFormationテンプレートで、SecurityGroupルールの CidrIp 指定についてちょっと誤解してたので、メモ。

対象範囲指定がないとルールが作成されない

AWS::EC2::SecurityGroup Ingress - AWS CloudFormation」を見ると CidrIp は省略可能(Required: No)となっています。ここで、省略すればAWSコンソールでいうところの「任意のIP」(0.0.0.0/0::0)になるのかな、と思ったのが私の勘違い。

CidrIp
  The IPv4 address range, in CIDR format.
  Required: No
  Type: String
  Update requires: No interruption

実際には「 CidrIp を含むいくつかのパラメータのうち1つを必ず指定しなければいけない」とIngressルールのドキュメント先頭近くに書かれています。

You must specify only one of the following properties: CidrIp, CidrIpv6, SourcePrefixListId, SourceSecurityGroupId, or SourceSecurityGroupName.
(AWS::EC2::SecurityGroup Ingress - AWS CloudFormation)

Egressルールのドキュメントにも類似のどれか一つ指定必須の注意書きがあり、こちらにははっきりと「これらのパラメータのうち一つを指定しなかった場合、スタックの作成には成功しますが、ルールはセキュリティグループに追加されません」と付記されていました。

You must specify a destination security group (DestinationPrefixListId or DestinationSecurityGroupId) or a CIDR range (CidrIp or CidrIpv6). If you do not specify one of these parameters, the stack will launch successfully but the rule will not be added to the security group.
AWS::EC2::SecurityGroupEgress - AWS CloudFormation

つまりほかのパラメータも指定していない状態で「省略すれば『任意のIP』になる」という期待は的外れで、このルールが作成されません。

省略と !Ref "AWS::NoValue" と "0.0.0.0/0"

実際に次のようなCloudFormationテンプレートで、スタックを作成してみました。 IsRemoteAccessCIDR は今回 false になるようにスタック作成時のパラメータを指定しています。このため各プロトコルに対するルールで、 CidrIp は以下が指定されたことになります。

プロトコル FromPort CidrIP
SSH ingress 22 "0.0.0.0/0"
RDP ingress 3389 !Ref "AWS::NoValue"
NICE DCV ingress 8443 (指定省略)
  EC2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: EC2SecurityGroup
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - 
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: !If [IsRemoteAccessCIDR, !Ref RemoteAccessCIDR, "0.0.0.0/0"]
          Description: "SSH ingress"
        - 
          IpProtocol: tcp
          FromPort: 3389
          ToPort: 3389
          CidrIp: !If [IsRemoteAccessCIDR, !Ref RemoteAccessCIDR, !Ref "AWS::NoValue"]
          Description: "RDP ingress"
        - 
          IpProtocol: tcp
          FromPort: 8443
          ToPort: 8443
          Description: "NICE DCV ingress"

作成されたセキュリティグループのインバウンドルールは CidrIp0.0.0.0/0 を指定したSSHのルールだけが作成されており、 !Ref "AWS::NoValue" を指定したRDPと指定を省略したNICE DCV用のルールは作成されていませんでした。

image.png

指定時と省略時の動作はドキュメントの記載通り、 !Ref "AWS::NoValue" 指定時も、これは「指定すると、対応するリソースプロパティを削除」するので省略時と同等になり、ドキュメントの記載通りの動作でした。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?