Skip to content

Commit 9bdd9f0

Browse files
LAYTATrezabekf
andauthored
Add Guard Hooks workshop content (#276)
* Add Guard Hooks workshop content - Add Guard Hooks section with 5 lab modules - Include CloudFormation templates for testing - Add Guard rules for S3 security validation - Include comprehensive documentation and images - Fix trailing whitespace issues * Bump version: 3.5.3 → 3.6.0 * Add tracking code --------- Co-authored-by: JJ Lei <jjlei@amazon.com> Co-authored-by: Franco Rezabek <43790446+rezabekf@users.noreply.github.com>
1 parent 27fb954 commit 9bdd9f0

27 files changed

Lines changed: 1214 additions & 1 deletion

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[bumpversion]
2-
current_version = 3.5.3
2+
current_version = 3.6.0
33
commit = True
44
tag = False
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Resources:
3+
CompliantS3Bucket:
4+
Type: "AWS::S3::Bucket"
5+
Properties:
6+
BucketName: compliant-test-bucket-oct-8-1554
7+
# Compliance: Versioning must be enabled
8+
VersioningConfiguration:
9+
Status: Enabled
10+
# Compliance: Public access must be blocked
11+
PublicAccessBlockConfiguration:
12+
BlockPublicAcls: true
13+
BlockPublicPolicy: true
14+
IgnorePublicAcls: true
15+
RestrictPublicBuckets: true
16+
# Compliance: Server-side encryption must be enabled
17+
BucketEncryption:
18+
ServerSideEncryptionConfiguration:
19+
- ServerSideEncryptionByDefault:
20+
SSEAlgorithm: AES256
21+
# Compliance: No public read access (default behavior, no explicit ACL)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Description: "IAM role configuration for Guard Hook (uksb-1q9p31idr) (tag:guard-hook)."
3+
4+
Parameters:
5+
S3BucketName:
6+
Type: String
7+
Default: "<s3-bucket-name>"
8+
Description: "Name of the S3 bucket containing Guard rules"
9+
10+
Resources:
11+
GuardHookExecutionRole:
12+
Type: "AWS::IAM::Role"
13+
Properties:
14+
AssumeRolePolicyDocument:
15+
Version: "2012-10-17"
16+
Statement:
17+
- Effect: Allow
18+
Principal:
19+
Service: hooks.cloudformation.amazonaws.com
20+
Action: "sts:AssumeRole"
21+
Policies:
22+
- PolicyName: GuardHookS3Access
23+
PolicyDocument:
24+
Version: "2012-10-17"
25+
Statement:
26+
- Effect: Allow
27+
Action:
28+
- "s3:ListBucket"
29+
- "s3:GetObject"
30+
- "s3:GetObjectVersion"
31+
Resource:
32+
- !Sub "arn:aws:s3:::${S3BucketName}"
33+
- !Sub "arn:aws:s3:::${S3BucketName}/*"
34+
- Effect: Allow
35+
Action:
36+
- "s3:PutObject"
37+
- "s3:PutObjectAcl"
38+
Resource:
39+
- !Sub "arn:aws:s3:::${S3BucketName}/guard-output/*"
40+
41+
Outputs:
42+
GuardHookExecutionRoleArn:
43+
Description: "ARN of the Guard Hook execution role"
44+
Value: !GetAtt GuardHookExecutionRole.Arn
45+
Export:
46+
Name: !Sub "${AWS::StackName}-GuardHookExecutionRoleArn"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
NonCompliantS3Bucket:
4+
Type: 'AWS::S3::Bucket'
5+
Properties:
6+
BucketName: noncompliant-test-bucket
7+
# Missing VersioningConfiguration - violates s3_versioning_enabled rule
8+
# Missing PublicAccessBlockConfiguration - violates s3_public_access_blocked rule
9+
# Missing BucketEncryption - violates s3_encryption_enabled rule
10+
AccessControl: PublicRead # Violates s3_no_public_read rule
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# S3 Security Guard Rules for CloudFormation Hook
2+
# These rules ensure S3 buckets follow security best practices
3+
4+
# Rule 1: S3 buckets must have versioning enabled
5+
rule s3_versioning_enabled {
6+
Resources.*[ Type == 'AWS::S3::Bucket' ] {
7+
Properties {
8+
VersioningConfiguration exists
9+
VersioningConfiguration.Status == "Enabled"
10+
}
11+
}
12+
}
13+
14+
# Rule 2: S3 buckets must have public access blocked
15+
rule s3_public_access_blocked {
16+
Resources.*[ Type == 'AWS::S3::Bucket' ] {
17+
Properties {
18+
PublicAccessBlockConfiguration exists
19+
PublicAccessBlockConfiguration.BlockPublicAcls == true
20+
PublicAccessBlockConfiguration.BlockPublicPolicy == true
21+
PublicAccessBlockConfiguration.IgnorePublicAcls == true
22+
PublicAccessBlockConfiguration.RestrictPublicBuckets == true
23+
}
24+
}
25+
}
26+
27+
# Rule 3: S3 buckets should have server-side encryption enabled
28+
rule s3_encryption_enabled {
29+
Resources.*[ Type == 'AWS::S3::Bucket' ] {
30+
Properties {
31+
BucketEncryption exists
32+
BucketEncryption.ServerSideEncryptionConfiguration exists
33+
BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault exists
34+
BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault.SSEAlgorithm in ["AES256", "aws:kms"]
35+
}
36+
}
37+
}
38+
39+
# Rule 4: S3 buckets should not allow public read access
40+
rule s3_no_public_read {
41+
Resources.*[ Type == 'AWS::S3::Bucket' ] {
42+
Properties {
43+
# Ensure no public read permissions in ACL
44+
when AccessControl exists {
45+
AccessControl != "PublicRead"
46+
AccessControl != "PublicReadWrite"
47+
}
48+
}
49+
}
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Resources:
3+
TestBucket:
4+
Type: 'AWS::S3::Bucket'
5+
Properties:
6+
BucketName: test-bucket-compliant
7+
VersioningConfiguration:
8+
Status: Enabled
9+
PublicAccessBlockConfiguration:
10+
BlockPublicAcls: true
11+
BlockPublicPolicy: true
12+
IgnorePublicAcls: true
13+
RestrictPublicBuckets: true
14+
BucketEncryption:
15+
ServerSideEncryptionConfiguration:
16+
- ServerSideEncryptionByDefault:
17+
SSEAlgorithm: AES256
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: "Activate a Guard Hook"
3+
weight: 630
4+
---
5+
6+
Japanese translation is not available yet. Please use the English version.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title: "Activate a Guard Hook"
3+
weight: 630
4+
---
5+
6+
### Introduction
7+
8+
To use an AWS Guard Hook in your account, you must first activate the Hook for the account and Region where you want to use it. Activating a Hook makes it usable in stack operations in the account and Region where it's activated.
9+
10+
When you activate a Guard Hook, CloudFormation creates an entry in your account's registry for the activated Hook as a private Hook. This allows you to set any configuration properties the Hook includes. Configuration properties define how the Hook is configured for a given AWS account and Region.
11+
12+
#### Using AWS Console to Create a Guard Hook
13+
14+
##### **Open AWS CloudFormation Hooks**
15+
16+
1. Open **AWS CloudFormation Console**.
17+
2. Navigate to the **Hooks** section.
18+
3. Click on **With Guard** option in the **Create a Hook** dropdown list.
19+
20+
![Create Hook with Guard](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-start-dropdown-option.png)
21+
22+
##### **Step 1: Provide your Guard rules**
23+
24+
Configure the Guard Hook source with your S3-stored rules:
25+
26+
![Provide Guard Rules](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-provide-your-guard-rules.png)
27+
28+
Use the following parameters:
29+
30+
1. **Guard Hook source** – Select **Store your Guard rules in S3**
31+
2. **S3 URI** – Enter the **S3 URI of your Guard rules file** (e.g., `s3://guard-hook-bucket-<your-name>/hooks/s3-security-rules.guard`)
32+
33+
::alert[If you need to find your S3 URI, refer back to the [Write Guard rules for Hook](../write-guard-rules/) section where you uploaded your Guard rules file.]{type="info"}
34+
35+
3. **Object version** – (Optional) If your S3 bucket has versioning enabled, you can specify a version
36+
4. **S3 bucket for Guard output report** – (Optional) You can use the same bucket for output reports
37+
38+
Click **Next** to continue.
39+
40+
##### **Step 2: Hook details and settings**
41+
42+
Configure the Hook behavior and execution settings:
43+
44+
![Hook Details and Settings](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-hook-details-and-settings.png)
45+
46+
Use the following parameters:
47+
48+
1. **Hook Name**`S3SecurityGuardHook`
49+
2. **Hook Targets** – Select **Resources**.
50+
::alert[We choose **Resources** as the target because our Guard rules are designed to evaluate individual CloudFormation resources (specifically S3 buckets) rather than the entire template.]{type="info"}
51+
3. **Hook Actions** – Select **Create**.
52+
::alert[This implements evaluation during CloudFormation **CREATE** operation]{type="info"}
53+
4. **Hook Mode** – Set to **Fail**.
54+
::alert[With Hook Mode being Warn the hook will only emit a warning message when a hook fails, without stopping the provisioning operation. While with Fail mode the hook will stop the provisioning operation when a Hook fails.]{type="info"}
55+
5. **Execution Role** – Choose **Existing Execution Role** and select the **GuardHookExecutionRole** created earlier.
56+
57+
::alert[To find your execution role, look for a role name similar to `GuardHookExecutionRoleStack-GuardHookExecutionRole-<random-string>` that was created in the [Prepare to create a Guard Hook](../prepare-guard-hook/) section.]{type="info"}
58+
59+
Click **Next** to continue.
60+
61+
##### **Step 3: Apply Hook filters (Optional)**
62+
63+
Configure which resources the Hook should target:
64+
65+
![Apply Hook Filters](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-apply-hook-filters.png)
66+
67+
For **Hook filters** we will add **`AWS::S3::Bucket`** to filter the hook to just check for the creations of S3 buckets.
68+
69+
We will use the default options for the other configurations here so there is no need to update them.
70+
71+
Click **Next** to continue.
72+
73+
##### **Step 4: Review and activate**
74+
75+
Review all your settings before creating the Hook:
76+
77+
![Review and Activate](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-review-and-activate.png)
78+
79+
Review the settings:
80+
- **Hook Name**: S3SecurityGuardHook
81+
- **Guard Rules Source**: S3 URI pointing to your rules file
82+
- **Target**: Resources (AWS::S3::Bucket)
83+
- **Actions**: Create
84+
- **Mode**: Fail
85+
- **Execution Role**: GuardHookExecutionRole
86+
87+
Click **Create** to register the Hook and wait for a few seconds for the Hook to be created and activated.
88+
89+
![Successful Hook Creation](/static/advanced/hook/advanced-hook-create-a-hook-with-guard-successful-creation.png)
90+
91+
### Alternative: Using AWS CLI to Activate Guard Hook
92+
93+
You can also activate the Guard Hook using the AWS CLI:
94+
95+
```bash
96+
# First, activate the Hook type
97+
aws cloudformation activate-type \
98+
--type HOOK \
99+
--type-name "AWS::CloudFormation::GuardHook" \
100+
--publisher-id "AWS" \
101+
--region us-east-1
102+
103+
# Then, set the Hook configuration
104+
aws cloudformation set-type-configuration \
105+
--type HOOK \
106+
--type-name "AWS::CloudFormation::GuardHook" \
107+
--configuration '{
108+
"CloudFormationConfiguration": {
109+
"HookConfiguration": {
110+
"TargetStacks": "ALL",
111+
"FailureMode": "FAIL",
112+
"Properties": {
113+
"GuardRuleS3Uri": "s3://your-guard-rules-bucket/hooks/s3-security-rules.guard",
114+
"OutputS3Uri": "s3://your-guard-rules-bucket/guard-output/",
115+
"ExecutionRoleArn": "arn:aws:iam::123456789012:role/GuardHookExecutionRole"
116+
}
117+
},
118+
"TargetOperations": ["CREATE"],
119+
"TargetFilters": {
120+
"Types": ["AWS::S3::Bucket"]
121+
}
122+
}
123+
}' \
124+
--region us-east-1
125+
```
126+
127+
### Understanding Guard Hook Configuration
128+
129+
The Guard Hook configuration includes several important settings:
130+
131+
#### **Guard Rules Source**
132+
- **S3 URI**: Points to your Guard rules file in S3
133+
- **Versioning**: Optionally specify a specific version for consistency
134+
- **Access**: The execution role must have read permissions to this S3 location
135+
136+
#### **Target Configuration**
137+
- **Resources**: Targets individual resource operations
138+
- **Filters**: Limits evaluation to specific resource types (AWS::S3::Bucket)
139+
- **Actions**: Specifies when to run (CREATE, UPDATE, DELETE)
140+
141+
#### **Execution Settings**
142+
- **Failure Mode**: FAIL stops operations on rule violations, WARN allows them to continue
143+
- **Execution Role**: IAM role with necessary S3 permissions
144+
145+
#### **Output Configuration**
146+
- **Output S3 URI**: Optional location for detailed validation reports
147+
- **Report Format**: JSON or YAML format for output reports
148+
149+
### Conclusion
150+
151+
Once the Hook is activated, it will automatically evaluate CloudFormation stack changes based on the defined Guard rules. By enforcing S3 security best practices, the Guard Hook ensures that only compliant S3 bucket configurations are deployed. You can now proceed to the test section and monitor the Hook's behavior in your stack operations using the activated hook.
152+
153+
The Guard Hook will evaluate your S3 resources against the rules we defined:
154+
- ✅ Versioning must be enabled
155+
- ✅ Public access must be blocked
156+
- ✅ Server-side encryption must be configured
157+
- ✅ No public read access allowed
158+
159+
Choose **Next** to test the Guard Hook functionality!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: "Cleanup"
3+
weight: 650
4+
---
5+
6+
Japanese translation is not available yet. Please use the English version.

0 commit comments

Comments
 (0)