

# Guard로 템플릿 검증
<a name="cloudformation-guard"></a>

AWS CloudFormation Guard(`cfn-guard`)는 코드형 정책 도구입니다. 필수 또는 금지된 구성을 설명하는 규칙을 작성한 다음 해당 규칙에 따라 JSON 또는 YAML 데이터를 검사합니다. 예를 들어 템플릿의 모든 Amazon S3 버킷이 암호화를 사용하도록 할 수 있습니다.

로컬 검증의 제한 사항과 배포 전 수행할 단계는 [검증 범위 이해](template-guide.md#template-validation-scope) 섹션을 참조하세요.

**Topics**
+ [Guard 설치](#cloudformation-guard-install)
+ [규칙 작성](#cloudformation-guard-write-rule)
+ [템플릿 확인](#cloudformation-guard-validate)
+ [워크플로에 가드 추가](#cloudformation-guard-integrate)
+ [자세히 알아보기](#cloudformation-guard-learn-more)

## Guard 설치
<a name="cloudformation-guard-install"></a>

macOS에서는 Homebrew를 사용하여 Guard를 설치합니다.

```
brew install cloudformation-guard
```

Linux, Windows, 기타 macOS 설치 방법 및 릴리스 확인은 [AWS CloudFormation Guard 설정](https://docs.aws.amazon.com/cfn-guard/latest/ug/setting-up.html)을 참조하세요.

## 규칙 작성
<a name="cloudformation-guard-write-rule"></a>

규칙을 확장명이 `.guard`인 파일로 저장합니다. 다음 예제에서는 템플릿의 모든 Amazon S3 버킷이 버킷 암호화를 지정해야 합니다.

```
let s3_buckets = Resources.*[ Type == 'AWS::S3::Bucket' ]

rule S3_BUCKET_ENCRYPTED when %s3_buckets !empty {
  %s3_buckets {
    Properties.BucketEncryption exists
  }
}
```

규칙 구문과 추가 예제는 [AWS CloudFormation Guard 규칙 작성](https://docs.aws.amazon.com/cfn-guard/latest/ug/writing-rules.html)을 참조하세요.

## 템플릿 확인
<a name="cloudformation-guard-validate"></a>

`--rules`에 규칙 파일을 전달하고 `--data`에 템플릿을 전달합니다.

```
cfn-guard validate --rules rules.guard --data template.yaml
```

템플릿이 통과하면 명령이 종료 상태 `0`을 반환합니다. 규칙이 실패하면 출력은 실패한 규칙을 식별합니다. `--rules` 및 `--data`에 디렉터리를 전달하여 여러 파일을 검사할 수도 있습니다.

자동화된 워크플로에서 규칙을 사용하기 전에 기본 제공 유닛 테스트 지원으로 규칙을 테스트합니다. 지침은 [AWS CloudFormation Guard 규칙 테스트](https://docs.aws.amazon.com/cfn-guard/latest/ug/testing-rules.html)를 참조하세요.

## 워크플로에 가드 추가
<a name="cloudformation-guard-integrate"></a>

로컬에서, 자동 빌드에서 또는 Git 커밋 전에 Guard를 실행할 수 있습니다. 템플릿을 편집하는 동안 CloudFormation 언어 서버에서 Guard 규칙 팩을 실행할 수도 있습니다. 설정은 [CloudFormation 언어 서버](ide-extension.md) 섹션을 참조하세요.

CloudFormation 및 Cloud Control API 작업 중 규칙을 적용하려면 Guard Hooks를 사용합니다. 자세한 내용은 [Guard Hooks](https://docs.aws.amazon.com/cloudformation-cli/latest/hooks-userguide/guard-hooks.html)를 참조하세요.

## 자세히 알아보기
<a name="cloudformation-guard-learn-more"></a>

전체 언어 및 명령 참조는 [AWS CloudFormation Guard 사용 설명서](https://docs.aws.amazon.com/cfn-guard/latest/ug/what-is-guard.html)를 참조하세요. 소스 코드 및 릴리스 정보는 [AWS CloudFormation Guard GitHub의 리포지토리](https://github.com/aws-cloudformation/cloudformation-guard)에서 확인할 수 있습니다.