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 1 year has passed since last update.

CloudformationでSecurityGroupを記述するときはIngress/EgressとSecurityGroupリソースを分離する

Last updated at Posted at 2022-04-27

はじめに

初心者向けのメモです。

内容

スクリーンショット 2022-04-27 14.40.13.png
上図のような相互通信を許可する場合、AWS::EC2::SecurityGroupのみを使うと循環参照となりデプロイできないため、AWS::EC2::SecurityGroupAWS::EC2::SecurityGroupIngressに分けて書く。

また、分けることで、SecurityGroupのIDを変えずにAWS::EC2::SecurityGroupIngressのみを追加することができるため、ID参照する他リソースに影響しない。

悪い例.yml
Resources:
  SecurityGroup1:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VpcId
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          SourceSecurityGroupId: !Ref SecurityGroup2

  SecurityGroup2:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VpcId
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          SourceSecurityGroupId: !Ref SecurityGroup1
良い例.yml
Resources:
  SecurityGroup1:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VpcId

  SecurityGroup1Ingress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      IpProtocol: tcp
      FromPort: 80
      ToPort: 80
      GroupId: !Ref SecurityGroup1
      SourceSecurityGroupId: !Ref SecurityGroup2


  SecurityGroup2:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VpcId

  SecurityGroup2Ingress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      IpProtocol: tcp
      FromPort: 80
      ToPort: 80
      GroupId: !Ref SecurityGroup2
      SourceSecurityGroupId: !Ref SecurityGroup1

参考

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?