

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS CloudFormation Guard 정책을 사용하여 AWS Config 사용자 지정 규칙 생성
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies"></a>

*Andrew Lok, Nicole Brown, Kailash Havildar, Tanya Howell, Amazon Web Services*

## 요약
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-summary"></a>

[AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 규칙은 AWS 리소스와 대상 구성 상태를 평가하는 데 도움이 됩니다. 규칙에는 관리형 규칙과 사용자 지정 AWS Config 규칙의 두 가지 유형이 있습니다. 는 AWS Lambda 함수를 사용하거나 policy-as-code 언어인 [AWS CloudFormation Guard](https://github.com/aws-cloudformation/cloudformation-guard) (GitHub)를 사용하여 사용자 지정 규칙을 생성할 수 있습니다.

Guard로 생성된 규칙은 관리형 규칙보다 더 세분화된 제어를 제공하며, 일반적으로 완전 사용자 지정 Lambda 규칙보다 구성하기 쉽습니다. 이 접근 방식을 통해 엔지니어와 아키텍트는 Lambda를 통해 사용자 지정 규칙을 배포하는 데 필요한 Python, NodeJS 또는 Java를 알 필요 없이 규칙을 구축할 수 있습니다.

이 패턴은 Guard로 사용자 지정 규칙을 채택하는 데 도움이 되는 실행 가능한 템플릿, 코드 샘플 및 배포 접근 방식을 제공합니다. 관리자는이 패턴을 사용하여를 사용하여 [구성 항목](https://docs.aws.amazon.com/config/latest/developerguide/config-concepts.html#config-items) 속성이 있는 사용자 지정 규정 준수 규칙을 빌드 AWS Config 할 수 있습니다. 예를 들어 개발자는 AWS Config 구성 항목에 대해 Guard 정책을 사용하여 배포된 리소스 AWS 및 비AWS 리소스의 상태를 지속적으로 모니터링하고, 규칙 위반을 감지하고, 문제 해결을 자동으로 시작할 수 있습니다.

**목표**

이 패턴을 읽은 후 다음을 수행할 수 있습니다.
+ Guard 정책 코드가 AWS Config 서비스와 상호 작용하는 방법을 이해합니다.
+ Guard 구문을 사용하여 암호화된 볼륨에 대한 규정 준수를 검증하는 AWS Config 사용자 지정 규칙인 배포 *시나리오 1*입니다. 이 규칙은 드라이브가 사용 중인지 확인하고 드라이브 유형이 [gp3](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/general-purpose.html#gp3-ebs-volume-type)인지 확인합니다.
+ Guard 구문을 사용하여 Amazon GuardDuty 규정 준수를 검증하는 AWS Config 사용자 지정 규칙인 배포 *시나리오 2*입니다. 이 규칙은 GuardDuty 레코더에 [Amazon Simple Storage Service(Amazon S3) 보호](https://docs.aws.amazon.com/guardduty/latest/ug/s3-protection.html) 및 [Amazon Elastic Kubernetes Service(Amazon EKS) 보호](https://docs.aws.amazon.com/guardduty/latest/ug/kubernetes-protection.html)가 활성화되어 있는지 확인합니다.

## 사전 조건 및 제한 사항
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-prereqs"></a>

**사전 조건 **
+ 활성 AWS 계정
+ AWS Config,에서 [설정](https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html) AWS 계정

**제한 사항 **
+ 가드 사용자 지정 규칙은 대상 구성 항목 JSON 레코드의 키-값 페어만 쿼리할 수 있습니다.

## 아키텍처
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-architecture"></a>

Guard 구문을 사용자 지정 정책으로 AWS Config 규칙에 적용합니다. 지정된 각 리소스의 계층적 JSON을 AWS Config 캡처합니다. AWS Config 구성 항목의 JSON에는 키-값 페어가 포함됩니다. 이러한 속성은 Guard 구문에서 해당 값에 할당된 변수로 사용됩니다. 

다음은 Guard 구문에 대한 설명입니다. 구성 항목 JSON의 변수가 사용되고 `%` 문자 앞에 추가됩니다.

```
# declare variable
let <variable name> = <'value'>

# create rule and assign condition and policy
    rule <rule name> when 
        <CI json key> == <"CI json value"> {
            <top level CI json key>.<next level CI json key> == %<variable name>
        }
```

**시나리오 1: Amazon EBS 볼륨**

시나리오 1은 Guard 구문을 사용하여 암호화된 볼륨에 대한 규정 준수를 검증하는 AWS Config 사용자 지정 규칙을 배포합니다. 이 규칙은 드라이브가 사용 중인지 확인하고 드라이브 유형이 gp3인지 확인합니다.

다음은 시나리오 1에 대한 AWS Config 구성 항목의 예입니다.이 구성 항목에는 Guard 정책의 변수로 `volumestatus`, `volumeencryptionstatus`,의 세 가지 키-값 페어가 있습니다`volumetype`. 또한 `resourceType` 키는 Guard 정책에서 필터로 사용됩니다.

```
{
  "version": "1.3",
  "accountId": "111111111111",
  "configurationItemCaptureTime": "2023-01-15T19:04:45.402Z",
  "configurationItemStatus": "ResourceDiscovered",
  "configurationStateId": "4444444444444",
  "configurationItemMD5Hash": "",
  "arn": "arn:aws:ec2:us-west-2:111111111111:volume/vol-222222222222",
  "resourceType": "AWS::EC2::Volume",
  "resourceId": "vol-222222222222",
  "awsRegion": "us-west-2",
  "availabilityZone": "us-west-2b",
  "resourceCreationTime": "2023-01-15T19:03:22.247Z",
  "tags": {},
  "relatedEvents": [],
  "relationships": [
    {
      "resourceType": "AWS::EC2::Instance",
      "resourceId": "i-33333333333333333",
      "relationshipName": "Is attached to Instance"
    }
  ],
  "configuration": {
    "attachments": [
      {
        "attachTime": "2023-01-15T19:03:22.000Z",
        "device": "/dev/xvda",
        "instanceId": "i-33333333333333333",
        "state": "attached",
        "volumeId": "vol-222222222222",
        "deleteOnTermination": true,
        "associatedResource": null,
        "instanceOwningService": null
      }
    ],
    "availabilityZone": "us-west-2b",
    "createTime": "2023-01-15T19:03:22.247Z",
    "encrypted": false,
    "kmsKeyId": null,
    "outpostArn": null,
    "size": 8,
    "snapshotId": "snap-55555555555555555",
    "state": "in-use",
    "volumeId": "vol-222222222222",
    "iops": 100,
    "tags": [],
    "volumeType": "gp2",
    "fastRestored": null,
    "multiAttachEnabled": false,
    "throughput": null,
    "sseType": null
  },
  "supplementaryConfiguration": {}
}
```

다음은 Guard 구문을 사용하여 시나리오 1의 변수와 규칙을 정의하는 예입니다. 이 예에서 다음과 같이 합니다.
+ 처음 세 줄은 `let` 명령을 사용하여 변수를 정의합니다.   구성 항목의 속성에서 파생된 이름과 값이 할당됩니다.
+ `compliancecheck` 규칙 블록은와 일치하는 `resourceType` 키-값 페어를 찾는 조건부 종속성인 경우를 추가합니다`AWS::EC2::Volume`. 일치 항목이 발견되면 규칙은 나머지 JSON 속성을 진행하고 `state`, 및의 세 가지 조건에서 일치 항목을 찾습니다`encrypted``volumeType`.

```
let volumestatus = 'available'
let volumetype = 'gp3'
let volumeencryptionstatus = true

    rule compliancecheck when 
        resourceType == "AWS::EC2::Volume" {
            configuration.state == %volumestatus
            configuration.encrypted == %volumeencryptionstatus
            configuration.volumeType == %volumetype
        }
```

이 사용자 지정 규칙을 구현하는 전체 Guard 사용자 지정 정책은 GitHub 코드 리포지토리의 [awsconfig-guard-cft.yaml](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-cft.yaml) 또는 [awsconfig-guard-tf-ec2vol.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-ec2vol.json)을 참조하세요. Guard에이 사용자 지정 정책을 배포하는 HashiCorp Terraform 코드는 코드 리포지토리의 [awsconfig-guard-tf-example.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-example.json)을 참조하세요.

**시나리오 2: GuardDuty 규정 준수**

시나리오 2는 Guard 구문을 사용하여 Amazon GuardDuty 규정 준수를 검증하는 AWS Config 사용자 지정 규칙을 배포합니다. 이 규칙은 GuardDuty 레코더에 Amazon S3 보호 및 Amazon EKS 보호가 활성화되어 있는지 확인합니다. 또한 GuardDuty 조사 결과가 15분마다 게시되는지 확인합니다. 이 시나리오는 모든 AWS 계정 및 조직() AWS 리전 에 배포할 수 있습니다 AWS Organizations.

다음은 시나리오 2에 대한 AWS Config 구성 항목의 예입니다. 이 구성 항목에는 Guard 정책에서 변수로를 사용한 세 가지 키-값 페어`S3Logs`가 있습니다`Kubernetes`. `FindingPublishingFrequency`, 및 . 또한 `resourceType` 키는 정책에서 필터로 사용됩니다.

```
{
  "version": "1.3",
  "accountId": "111111111111",
  "configurationItemCaptureTime": "2023-11-27T13:34:28.888Z",
  "configurationItemStatus": "OK",
  "configurationStateId": "7777777777777",
  "configurationItemMD5Hash": "",
  "arn": "arn:aws:guardduty:us-west-2:111111111111:detector/66666666666666666666666666666666",
  "resourceType": "AWS::GuardDuty::Detector",
  "resourceId": "66666666666666666666666666666666",
  "resourceName": "66666666666666666666666666666666",
  "awsRegion": "us-west-2",
  "availabilityZone": "Regional",
  "resourceCreationTime": "2020-02-17T02:48:04.511Z",
  "tags": {},
  "relatedEvents": [],
  "relationships": [],
  "configuration": {
    "Enable": true,
    "FindingPublishingFrequency": "FIFTEEN_MINUTES",
    "DataSources": {
      "S3Logs": {
        "Enable": true
      },
      "Kubernetes": {
        "AuditLogs": {
          "Enable": true
        }
      }
    },
    
    "Id": "66666666666666666666666666666666",
    "Tags": []
  },
  "supplementaryConfiguration": {
    "CreatedAt": "2020-02-17T02:48:04.511Z"
  }
}
```

다음은 Guard 구문을 사용하여 시나리오 2의 변수와 규칙을 정의하는 예입니다. 이 예에서 다음과 같이 합니다.
+ 처음 세 줄은 `let` 명령을 사용하여 변수를 정의합니다.   구성 항목의 속성에서 파생된 이름과 값이 할당됩니다.
+ `compliancecheck` 규칙 블록은와 일치하는 `resourceType` 키-값 페어를 찾는 조건부 종속성인 경우를 추가합니다`AWS::GuardDuty::Detector`. 일치 항목이 발견되면 규칙은 나머지 JSON 속성을 진행하고 `S3Logs.Enable`, `Kubernetes.AuditLogs.Enable`및의 세 가지 조건에서 일치 항목을 찾습니다`FindingPublishingFrequency`.

```
let s3protection = true
let kubernetesprotection = true
let publishfrequency = 'FIFTEEN_MINUTES'

    rule compliancecheck when 
        resourceType == "AWS::GuardDuty::Detector" {
            configuration.DataSources.S3Logs.Enable == %s3protection
            configuration.DataSources.Kubernetes.AuditLogs.Enable == %kubernetesprotection
            configuration.FindingPublishingFrequency == %publishfrequency
        }
```

이 사용자 지정 규칙을 구현하는 전체 Guard 사용자 지정 정책은 GitHub 코드 리포지토리의 [awsconfig-guard-cft-gd.yaml](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-cft-gd.yaml)을 참조하세요. Guard에이 사용자 지정 정책을 배포하는 HashiCorp Terraform 코드는 코드 리포지토리의 [awsconfig-guard-tf-gd.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-gd.json)을 참조하세요.

## 도구
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-tools"></a>

**AWS 서비스**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)를 사용하면 AWS 리소스를 설정하고, 빠르고 일관되게 프로비저닝하고, AWS 계정 및 리전의 수명 주기 동안 리소스를 관리할 수 있습니다.
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html)는의 리소스 AWS 계정 와 리소스 구성 방법에 대한 자세한 보기를 제공합니다. 리소스가 서로 관련되는 방식과 리소스의 구성이 시간이 지남에 따라 변경된 방식을 식별하는 데 도움이 됩니다.

**기타 도구**
+ [HashiCorp Terraform](https://www.terraform.io/docs)은 코드를 사용하여 클라우드 인프라 및 리소스를 프로비저닝하고 관리하는 데 도움이 되는 코드형 인프라(IaC) 도구입니다.

**코드 리포지토리**

이 패턴의 코드는 GitHub [AWS Config unloaddb2 AWS CloudFormation Guard](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/tree/main) 리포지토리에서 확인할 수 있습니다. 이 코드 리포지토리에는이 패턴에 설명된 두 시나리오 모두에 대한 샘플이 포함되어 있습니다.

## 에픽
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-epics"></a>

### AWS Config 사용자 지정 규칙 생성
<a name="creating-cc-custom-rules"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
| (선택 사항) 규칙의 키-값 페어를 선택합니다. | 사용자 지정 Guard 정책을 정의하는 경우 다음 단계를 완료합니다. 시나리오 1 또는 2에 샘플 정책 중 하나를 사용하는 경우 다음 단계를 건너뜁니다.[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | 네트워크 관리자, 보안 엔지니어 | 
| 사용자 지정 규칙 만들기 | 이전에 식별한 키-값 페어를 사용하거나 제공된 샘플 Guard 정책 중 하나를 사용하여 [AWS Config 사용자 지정 정책 규칙 생성](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_cfn-guard.html#create-cfn-guard-rule-console)의 지침에 따라 사용자 지정 규칙을 생성합니다. | 네트워크 관리자, 보안 엔지니어 | 
| 사용자 지정 규칙을 검증합니다. | 다음 중 하나를 수행하여 사용자 지정 Guard 규칙을 검증합니다.[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | 네트워크 관리자, 보안 엔지니어 | 

## 문제 해결
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-troubleshooting"></a>


| 문제 | Solution | 
| --- | --- | 
| 외부에서 Guard 정책 테스트 AWS Config | 유닛 테스트는 로컬 디바이스 또는 IDE와 같은 통합 개발 환경( AWS Cloud9 IDE)에서 수행할 수 있습니다. 이렇게 하려면 다음 단계를 수행합니다.[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | 
|  AWS Config 사용자 지정 규칙 디버깅 | Guard 정책에서 `EnableDebugLogDelivery` 값을 로 변경합니다`true`. 기본값은 `false`입니다. 로그 메시지는 Amazon CloudWatch에 저장됩니다. | 

## 관련 리소스
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-resources"></a>

**AWS 설명서**
+ [AWS Config 사용자 지정 정책 규칙 생성](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_cfn-guard.html)(AWS Config 문서)
+ [쓰기 AWS CloudFormation Guard 규칙](https://docs.aws.amazon.com/cfn-guard/latest/ug/writing-rules.html)(Guard 설명서)

**AWS 블로그 게시물 및 워크숍**
+ [Introduction AWS CloudFormation Guard 2.0](https://aws.amazon.com/blogs/mt/introducing-aws-cloudformation-guard-2-0/)(AWS 블로그 게시물)

**기타 리소스**
+ [AWS CloudFormation Guard](https://github.com/aws-cloudformation/cloudformation-guard) Github
+ [AWS CloudFormation Guard CLI 설명서](https://github.com/aws-cloudformation/cloudformation-guard#guard-cli)(GitHub)