IaaS/퍼블릭클라우드

(AWS) CloudFormation Stack Policy란?

armyost 2022. 2. 6. 12:37
728x90

Stack Poilcy란?

스택 업데이트 중 의도치 않게 업데이트되지 않도록 하려는 리소스를 정의합니다. 

 

예를 들어 아래 예시에서 Resources.CriticalSecurityGroup은 업데이트가 되지 않도록 보호하고 싶을때 Stack Policy를 사용하면 좋다.

 

예시

# Read more here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html

Parameters:
  VPCId:
    Description: VPC to create the security group into
    Type: AWS::EC2::VPC::Id
  
  CidrSSH:
    Type: String
    Default: "0.0.0.0/0"

  CidrHTTP:
    Type: String
    Default: "0.0.0.0/0"

Resources:
  SSHSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: Test Drift SSH Security Group
      SecurityGroupIngress:
        - CidrIp: !Ref CidrSSH
          FromPort: 22
          ToPort: 22
          IpProtocol: tcp
      VpcId: !Ref VPCId

  CriticalSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: Test Drift HTTP Security Group
      SecurityGroupIngress:
        - CidrIp: !Ref CidrHTTP
          FromPort: 80
          ToPort: 80
          IpProtocol: tcp
      VpcId: !Ref VPCId

 

이를 위해서는 Stack을 생성/수정할때 Configure Stack Option 단계에서 'Advanced Option'의 'Stack Policy'에 다음 JSON을 입력합니다.

 

{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "Update:*",
            "Principal": "*",
            "Resource": "*"
        },
        {
            "Effect": "Deny",
            "Action": "Update:*",
            "Principal": "*",
            "Resource": "LogicalResourceId/CriticalSecurityGroup"
        },
        {
            "Effect" : "Deny",
            "Action" : "Update:*",
            "Principal": "*",
            "Resource" : "*",
            "Condition" : {
              "StringEquals" : {
                "ResourceType" : ["AWS::RDS::DBInstance"]
              }
            }
        }
    ]
}