@jjrawlins/cdk-diff-pr-github-action 1.6.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +8 -46
- package/cdkdiffprgithubaction/CdkDiffIamTemplate.go +48 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateGenerator.go +82 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateGeneratorProps.go +45 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateGenerator__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateGenerator__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateProps.go +51 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSet.go +50 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetCommandsProps.go +29 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetGenerator.go +87 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetGeneratorProps.go +33 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetGenerator__checks.go +29 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetGenerator__no_checks.go +14 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSetProps.go +58 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSet__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplateStackSet__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplate__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDiffIamTemplate__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDiffStack.go +17 -0
- package/cdkdiffprgithubaction/CdkDiffStackWorkflow.go +45 -0
- package/cdkdiffprgithubaction/CdkDiffStackWorkflowProps.go +21 -0
- package/cdkdiffprgithubaction/CdkDiffStackWorkflow__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDiffStackWorkflow__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDriftDetectionWorkflow.go +45 -0
- package/cdkdiffprgithubaction/CdkDriftDetectionWorkflowProps.go +32 -0
- package/cdkdiffprgithubaction/CdkDriftDetectionWorkflow__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDriftDetectionWorkflow__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplate.go +48 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplateGenerator.go +82 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplateGeneratorProps.go +17 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplateGenerator__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplateGenerator__no_checks.go +10 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplateProps.go +23 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplate__checks.go +21 -0
- package/cdkdiffprgithubaction/CdkDriftIamTemplate__no_checks.go +10 -0
- package/cdkdiffprgithubaction/GitHubOidcConfig.go +20 -0
- package/cdkdiffprgithubaction/LICENSE +202 -0
- package/cdkdiffprgithubaction/README.md +789 -0
- package/cdkdiffprgithubaction/Stack.go +19 -0
- package/cdkdiffprgithubaction/StackSetAutoDeployment.go +14 -0
- package/cdkdiffprgithubaction/StackSetRoleSelection.go +19 -0
- package/cdkdiffprgithubaction/go.mod +12 -0
- package/cdkdiffprgithubaction/jsii/jsii.go +28 -0
- package/cdkdiffprgithubaction/main.go +136 -0
- package/cdkdiffprgithubaction/version +1 -0
- package/lib/CdkDiffIamTemplate.js +2 -2
- package/lib/CdkDiffIamTemplateStackSet.js +2 -2
- package/lib/CdkDiffStackWorkflow.js +1 -1
- package/lib/CdkDriftDetectionWorkflow.js +1 -1
- package/lib/CdkDriftIamTemplate.js +2 -2
- package/node_modules/@aws-sdk/client-cloudformation/package.json +1 -1
- package/package.json +8 -4
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/LICENSE +0 -0
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/README.md +0 -0
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/argparse.js +0 -0
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/lib/sub.js +0 -0
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/lib/textwrap.js +0 -0
- /package/node_modules/{js-yaml/node_modules/argparse → argparse}/package.json +0 -0
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
# cdk-diff-pr-github-action
|
|
2
|
+
|
|
3
|
+
A [Projen](https://projen.io/) construct library that surfaces **CloudFormation change set diffs and drift status directly on your pull requests** so reviewers can see exactly what will change before merging.
|
|
4
|
+
|
|
5
|
+
## Why this exists
|
|
6
|
+
|
|
7
|
+
`cdk diff` output disappears into CI logs that nobody reads. Meanwhile, a single property change on an RDS instance or EC2 "pet" server can trigger a **resource replacement** — destroying the database or instance and recreating it from scratch. If that replacement slips through code review unnoticed, the result is data loss and downtime.
|
|
8
|
+
|
|
9
|
+
This construct was built to make those dangerous changes impossible to miss:
|
|
10
|
+
|
|
11
|
+
* **Replacement column front and center** — Every change set row shows whether CloudFormation will modify the resource in place or **replace** it, with before/after property values so reviewers can understand *why*.
|
|
12
|
+
* **Comment appears on the PR itself** — No digging through workflow logs. The diff table is posted (and updated in place) as a PR comment and in the GitHub Step Summary.
|
|
13
|
+
* **Drift banner** — If the stack has drifted from its template, a warning banner is prepended to the comment so reviewers know the baseline is already out of sync.
|
|
14
|
+
|
|
15
|
+
If you have ever lost an EC2 instance, an RDS database, or an ElastiCache cluster to an unexpected CloudFormation replacement, this tool is for you.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
A library that provides GitHub workflows and IAM templates for:
|
|
21
|
+
|
|
22
|
+
* Creating CloudFormation Change Sets for your CDK stacks on pull requests and commenting a formatted diff back on the PR.
|
|
23
|
+
* Detecting CloudFormation drift on a schedule or manual trigger and producing a consolidated summary (optionally creating an issue).
|
|
24
|
+
* Deploying IAM roles across AWS Organizations using StackSets.
|
|
25
|
+
|
|
26
|
+
It also provides ready-to-deploy IAM templates with the minimal permissions required for each workflow.
|
|
27
|
+
|
|
28
|
+
**Works with or without Projen** -- The StackSet generator can be used standalone in any Node.js project.
|
|
29
|
+
|
|
30
|
+
This package exposes five constructs:
|
|
31
|
+
|
|
32
|
+
* `CdkDiffStackWorkflow` — Generates one GitHub Actions workflow per stack to create a change set and render the diff back to the PR and Step Summary.
|
|
33
|
+
* `CdkDiffIamTemplate` — Emits a CloudFormation template file with minimal permissions for the Change Set workflow.
|
|
34
|
+
* `CdkDriftDetectionWorkflow` — Generates a GitHub Actions workflow to detect CloudFormation drift per stack, upload machine‑readable results, and aggregate a summary.
|
|
35
|
+
* `CdkDriftIamTemplate` — Emits a CloudFormation template file with minimal permissions for the Drift Detection workflow.
|
|
36
|
+
* `CdkDiffIamTemplateStackSet` — Creates a CloudFormation StackSet template for org-wide deployment of GitHub OIDC and IAM roles (Projen integration).
|
|
37
|
+
* `CdkDiffIamTemplateStackSetGenerator` — Pure generator class for StackSet templates (no Projen dependency).
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
1) Add the constructs to your Projen project (in `.projenrc.ts`).
|
|
42
|
+
2) Synthesize with `npx projen`.
|
|
43
|
+
3) Commit the generated files.
|
|
44
|
+
4) Open a pull request or run the drift detection workflow.
|
|
45
|
+
|
|
46
|
+
## End-to-end example
|
|
47
|
+
|
|
48
|
+
A realistic setup for a CDK Pipelines project with multiple stages and accounts. This generates one diff workflow per stack, a shared IAM template, and a scheduled drift detection workflow.
|
|
49
|
+
|
|
50
|
+
```go
|
|
51
|
+
// .projenrc.ts
|
|
52
|
+
import { awscdk } from 'projen';
|
|
53
|
+
import {
|
|
54
|
+
CdkDiffStackWorkflow,
|
|
55
|
+
CdkDiffIamTemplate,
|
|
56
|
+
CdkDriftDetectionWorkflow,
|
|
57
|
+
} from '@jjrawlins/cdk-diff-pr-github-action';
|
|
58
|
+
|
|
59
|
+
const project = new awscdk.AwsCdkTypeScriptApp({
|
|
60
|
+
name: 'my-data-platform',
|
|
61
|
+
defaultReleaseBranch: 'main',
|
|
62
|
+
cdkVersion: '2.170.0',
|
|
63
|
+
github: true,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// --- Change Set Diff Workflows (one per stack) ---
|
|
67
|
+
// stackName is the CDK construct path used with `cdk deploy <target>`.
|
|
68
|
+
// For CDK Pipelines, this is typically: pipeline/Stage/Stack
|
|
69
|
+
// The construct automatically resolves the real CloudFormation stack name
|
|
70
|
+
// from cdk.out at runtime (e.g., "my-stage-dev-my-stack").
|
|
71
|
+
|
|
72
|
+
new CdkDiffStackWorkflow({
|
|
73
|
+
project,
|
|
74
|
+
oidcRoleArn: 'arn:aws:iam::111111111111:role/GitHubOIDCRole',
|
|
75
|
+
oidcRegion: 'us-east-1',
|
|
76
|
+
stacks: [
|
|
77
|
+
{
|
|
78
|
+
stackName: 'my-pipeline/dev/DatabaseStack',
|
|
79
|
+
changesetRoleToAssumeArn: 'arn:aws:iam::111111111111:role/CdkChangesetRole',
|
|
80
|
+
changesetRoleToAssumeRegion: 'us-east-1',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
stackName: 'my-pipeline/dev/ComputeStack',
|
|
84
|
+
changesetRoleToAssumeArn: 'arn:aws:iam::111111111111:role/CdkChangesetRole',
|
|
85
|
+
changesetRoleToAssumeRegion: 'us-east-1',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
// Cross-account: prod stacks can use a different OIDC role and region
|
|
89
|
+
stackName: 'my-pipeline/prod/DatabaseStack',
|
|
90
|
+
changesetRoleToAssumeArn: 'arn:aws:iam::222222222222:role/CdkChangesetRole',
|
|
91
|
+
changesetRoleToAssumeRegion: 'us-east-1',
|
|
92
|
+
oidcRoleArn: 'arn:aws:iam::222222222222:role/GitHubOIDCRole',
|
|
93
|
+
oidcRegion: 'us-east-1',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// --- IAM Template (deploy once per account) ---
|
|
99
|
+
new CdkDiffIamTemplate({
|
|
100
|
+
project,
|
|
101
|
+
roleName: 'CdkChangesetRole',
|
|
102
|
+
createOidcRole: true,
|
|
103
|
+
githubOidc: {
|
|
104
|
+
owner: 'my-org',
|
|
105
|
+
repositories: ['my-data-platform'],
|
|
106
|
+
branches: ['main'],
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// --- Drift Detection (scheduled + manual) ---
|
|
111
|
+
new CdkDriftDetectionWorkflow({
|
|
112
|
+
project,
|
|
113
|
+
schedule: '0 6 * * 1', // Every Monday at 6 AM UTC
|
|
114
|
+
oidcRoleArn: 'arn:aws:iam::111111111111:role/GitHubOIDCRole',
|
|
115
|
+
oidcRegion: 'us-east-1',
|
|
116
|
+
stacks: [
|
|
117
|
+
{
|
|
118
|
+
stackName: 'dev-DatabaseStack',
|
|
119
|
+
driftDetectionRoleToAssumeArn: 'arn:aws:iam::111111111111:role/CdkDriftRole',
|
|
120
|
+
driftDetectionRoleToAssumeRegion: 'us-east-1',
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
project.synth();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
After `npx projen`, this generates:
|
|
129
|
+
|
|
130
|
+
* `.github/workflows/diff-my-pipeline-dev-databasestack.yml`
|
|
131
|
+
* `.github/workflows/diff-my-pipeline-dev-computestack.yml`
|
|
132
|
+
* `.github/workflows/diff-my-pipeline-prod-databasestack.yml`
|
|
133
|
+
* `.github/workflows/scripts/describe-cfn-changeset.ts`
|
|
134
|
+
* `.github/workflows/drift-detection.yml`
|
|
135
|
+
* `.github/workflows/scripts/detect-drift.ts`
|
|
136
|
+
* `cdk-diff-workflow-iam-template.yaml`
|
|
137
|
+
|
|
138
|
+
When a pull request is opened, each diff workflow runs automatically and posts a comment like this:
|
|
139
|
+
|
|
140
|
+
| Action | ID | Type | Replacement | Details |
|
|
141
|
+
|--------|-----|------|-------------|---------|
|
|
142
|
+
| 🔵 Modify | MyDatabase | AWS::RDS::DBInstance | **True** | 🔵 **DBInstanceClass**: `db.t3.medium` -> `db.t3.large` |
|
|
143
|
+
| 🔵 Modify | MyFunction | AWS::Lambda::Function | False | 🔵 **Runtime**: `nodejs18.x` -> `nodejs20.x` |
|
|
144
|
+
|
|
145
|
+
The **Replacement: True** on the RDS instance is exactly the kind of change this tool is designed to catch before it reaches production.
|
|
146
|
+
|
|
147
|
+
## Usage: CdkDiffStackWorkflow
|
|
148
|
+
|
|
149
|
+
`CdkDiffStackWorkflow` renders a workflow per stack named `diff-<StackName>.yml` under `.github/workflows/`. It also generates a helper script at `.github/workflows/scripts/describe-cfn-changeset.ts` that formats the change set output and takes care of posting the PR comment and Step Summary.
|
|
150
|
+
|
|
151
|
+
Example `.projenrc.ts`:
|
|
152
|
+
|
|
153
|
+
```go
|
|
154
|
+
import { awscdk } from 'projen';
|
|
155
|
+
import { CdkDiffStackWorkflow } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
156
|
+
|
|
157
|
+
const project = new awscdk.AwsCdkConstructLibrary({
|
|
158
|
+
// ... your usual settings ...
|
|
159
|
+
workflowName: 'my-lib',
|
|
160
|
+
defaultReleaseBranch: 'main',
|
|
161
|
+
cdkVersion: '2.85.0',
|
|
162
|
+
github: true,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
new CdkDiffStackWorkflow({
|
|
166
|
+
project,
|
|
167
|
+
stacks: [
|
|
168
|
+
{
|
|
169
|
+
stackName: 'MyAppStack',
|
|
170
|
+
changesetRoleToAssumeArn: 'arn:aws:iam::123456789012:role/cdk-diff-role',
|
|
171
|
+
changesetRoleToAssumeRegion: 'us-east-1',
|
|
172
|
+
// Optional per‑stack OIDC override (if not using the defaults below)
|
|
173
|
+
// oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
174
|
+
// oidcRegion: 'us-east-1',
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
// Default OIDC role/region used by all stacks unless overridden per‑stack
|
|
178
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
179
|
+
oidcRegion: 'us-east-1',
|
|
180
|
+
// Optional: Node version used in the workflow (default: '24.x')
|
|
181
|
+
// nodeVersion: '24.x',
|
|
182
|
+
// Optional: Yarn command to run CDK (default: 'cdk')
|
|
183
|
+
// cdkYarnCommand: 'cdk',
|
|
184
|
+
// Optional: Where to place the helper script (default: '.github/workflows/scripts/describe-cfn-changeset.ts')
|
|
185
|
+
// scriptOutputPath: '.github/workflows/scripts/describe-cfn-changeset.ts',
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
project.synth();
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### CdkDiffStackWorkflow props
|
|
192
|
+
|
|
193
|
+
* `project` (required) — Your Projen project instance.
|
|
194
|
+
* `stacks` (required) — Array of stack entries.
|
|
195
|
+
* `oidcRoleArn` (required unless provided per‑stack) — Default OIDC role ARN.
|
|
196
|
+
* `oidcRegion` (required unless provided per‑stack) — Default OIDC region.
|
|
197
|
+
* `nodeVersion` (optional, default `'24.x'`) — Node.js version for the workflow runner.
|
|
198
|
+
* `cdkYarnCommand` (optional, default `'cdk'`) — Yarn script/command to invoke CDK.
|
|
199
|
+
* `scriptOutputPath` (optional, default `'.github/workflows/scripts/describe-cfn-changeset.ts'`) — Where to write the helper script.
|
|
200
|
+
|
|
201
|
+
If neither top‑level OIDC defaults nor all per‑stack values are supplied, the construct throws a helpful error.
|
|
202
|
+
|
|
203
|
+
### Stack item fields
|
|
204
|
+
|
|
205
|
+
* `stackName` (required) — The CDK stack name to create the change set for.
|
|
206
|
+
* `changesetRoleToAssumeArn` (required) — The ARN of the role used to create the change set (role chaining after OIDC).
|
|
207
|
+
* `changesetRoleToAssumeRegion` (required) — The region for that role.
|
|
208
|
+
* `oidcRoleArn` (optional) — Per‑stack override for the OIDC role.
|
|
209
|
+
* `oidcRegion` (optional) — Per‑stack override for the OIDC region.
|
|
210
|
+
|
|
211
|
+
### What gets generated
|
|
212
|
+
|
|
213
|
+
* `.github/workflows/diff-<StackName>.yml` — One workflow per stack, triggered on PR open/sync/reopen.
|
|
214
|
+
* `.github/workflows/scripts/describe-cfn-changeset.ts` — A helper script that:
|
|
215
|
+
|
|
216
|
+
* Polls `DescribeChangeSet` until terminal
|
|
217
|
+
* Filters out ignorable logical IDs or resource types using environment variables `IGNORE_LOGICAL_IDS` and `IGNORE_RESOURCE_TYPES`
|
|
218
|
+
* Renders an HTML table with actions, logical IDs, types, replacements, and changed properties
|
|
219
|
+
* **Checks cached drift status** via `DescribeStacks` — if the stack has drifted, a warning banner with drifted resource details is prepended to the output (non-fatal; degrades gracefully if IAM permissions are missing)
|
|
220
|
+
* Prints the HTML and appends to the GitHub Step Summary
|
|
221
|
+
* **Upserts the PR comment** — uses an HTML marker (`<!-- cdk-diff:stack:STACK_NAME -->`) to find and update an existing comment instead of creating duplicates on every push
|
|
222
|
+
|
|
223
|
+
### Change Set Output Format
|
|
224
|
+
|
|
225
|
+
The change set script uses the CloudFormation `IncludePropertyValues` API feature to show **actual before/after values** for changed properties, not just property names.
|
|
226
|
+
|
|
227
|
+
**Example PR comment:**
|
|
228
|
+
|
|
229
|
+
> **Warning: Stack has drifted (2 resources out of sync)**
|
|
230
|
+
>
|
|
231
|
+
> Last drift check: 2025-01-15T10:30:00.000Z
|
|
232
|
+
>
|
|
233
|
+
> <details><summary>View drifted resources</summary>
|
|
234
|
+
>
|
|
235
|
+
> | Resource | Type | Drift Status |
|
|
236
|
+
> |----------|------|-------------|
|
|
237
|
+
> | MySecurityGroup | AWS::EC2::SecurityGroup | MODIFIED |
|
|
238
|
+
> | OldBucket | AWS::S3::Bucket | DELETED |
|
|
239
|
+
>
|
|
240
|
+
> </details>
|
|
241
|
+
|
|
242
|
+
| Action | ID | Type | Replacement | Details |
|
|
243
|
+
|--------|-----|------|-------------|---------|
|
|
244
|
+
| 🔵 Modify | MyDatabase | AWS::RDS::DBInstance | **True** | 🔵 **DBInstanceClass**: `db.t3.medium` -> `db.t3.large` |
|
|
245
|
+
| 🔵 Modify | MyLambdaFunction | AWS::Lambda::Function | False | 🔵 **Runtime**: `nodejs18.x` -> `nodejs20.x` |
|
|
246
|
+
| 🟢 Add | NewSecurityGroup | AWS::EC2::SecurityGroup | - | |
|
|
247
|
+
| 🔴 Remove | OldRole | AWS::IAM::Role | - | |
|
|
248
|
+
|
|
249
|
+
**Features:**
|
|
250
|
+
|
|
251
|
+
* **Replacement column** highlights when CloudFormation will **destroy and recreate** a resource — critical for stateful resources like databases, EC2 instances, and file systems
|
|
252
|
+
* **Drift banner** — if the stack has drifted, a warning with drifted resource details appears above the change set table so reviewers know the baseline state
|
|
253
|
+
* **Comment upsert** — each stack's comment is updated in place on subsequent pushes instead of creating duplicates; uses an HTML marker for reliable find-and-replace
|
|
254
|
+
* **Color-coded indicators**: 🟢 Added, 🔵 Modified, 🔴 Removed
|
|
255
|
+
* **Inline values for small changes**: Shows `before -> after` directly in the table
|
|
256
|
+
* **Collapsible details for large values**: IAM policies, tags, and other large JSON values are wrapped in expandable `<details>` elements to keep the table readable
|
|
257
|
+
* **All attribute types supported**: Properties, Tags, Metadata, etc.
|
|
258
|
+
* **HTML-escaped values**: Prevents XSS from property values
|
|
259
|
+
|
|
260
|
+
### Environment variables used by the change set script
|
|
261
|
+
|
|
262
|
+
* `STACK_NAME` (required) — Stack name to describe.
|
|
263
|
+
* `CHANGE_SET_NAME` (default: same as `STACK_NAME`).
|
|
264
|
+
* `AWS_REGION` — Region for CloudFormation API calls. The workflow sets this via the credentials action(s).
|
|
265
|
+
* `GITHUB_TOKEN` (optional) — If set with `GITHUB_COMMENT_URL`, posts a PR comment.
|
|
266
|
+
* `GITHUB_COMMENT_URL` (optional) — PR comments URL.
|
|
267
|
+
* `GITHUB_STEP_SUMMARY` (optional) — When present, appends the HTML to the step summary file.
|
|
268
|
+
* `IGNORE_LOGICAL_IDS` (optional) — Comma‑separated logical IDs to ignore (default includes `CDKMetadata`).
|
|
269
|
+
* `IGNORE_RESOURCE_TYPES` (optional) — Comma‑separated resource types to ignore (e.g., `AWS::CDK::Metadata`).
|
|
270
|
+
|
|
271
|
+
## Usage: CdkDiffIamTemplate
|
|
272
|
+
|
|
273
|
+
Emit an IAM template you can deploy in your account for the Change Set workflow. Supports two modes:
|
|
274
|
+
|
|
275
|
+
1. **External OIDC Role** — Reference an existing GitHub OIDC role (original behavior)
|
|
276
|
+
2. **Self-Contained** — Create the GitHub OIDC provider and role within the same template (new)
|
|
277
|
+
|
|
278
|
+
### Option 1: Using an Existing OIDC Role (External)
|
|
279
|
+
|
|
280
|
+
Use this when you already have a GitHub OIDC provider and role set up in your account.
|
|
281
|
+
|
|
282
|
+
#### With Projen
|
|
283
|
+
|
|
284
|
+
```go
|
|
285
|
+
import { awscdk } from 'projen';
|
|
286
|
+
import { CdkDiffIamTemplate } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
287
|
+
|
|
288
|
+
const project = new awscdk.AwsCdkConstructLibrary({
|
|
289
|
+
// ...
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
new CdkDiffIamTemplate({
|
|
293
|
+
project,
|
|
294
|
+
roleName: 'cdk-diff-role',
|
|
295
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
296
|
+
oidcRegion: 'us-east-1',
|
|
297
|
+
// Optional: custom output path (default: 'cdk-diff-workflow-iam-template.yaml')
|
|
298
|
+
// outputPath: 'infra/cdk-diff-iam.yaml',
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
project.synth();
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### Without Projen (Standalone Generator)
|
|
305
|
+
|
|
306
|
+
```go
|
|
307
|
+
import { CdkDiffIamTemplateGenerator } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
308
|
+
import * as fs from 'fs';
|
|
309
|
+
|
|
310
|
+
const template = CdkDiffIamTemplateGenerator.generateTemplate({
|
|
311
|
+
roleName: 'cdk-diff-role',
|
|
312
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
313
|
+
oidcRegion: 'us-east-1',
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
fs.writeFileSync('cdk-diff-iam-template.yaml', template);
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Option 2: Self-Contained Template (Create OIDC Role)
|
|
320
|
+
|
|
321
|
+
Use this when you want a single template that creates everything needed — the GitHub OIDC provider, OIDC role, and changeset role. This simplifies deployment and pairs well with the `CdkDiffStackWorkflow`.
|
|
322
|
+
|
|
323
|
+
#### With Projen
|
|
324
|
+
|
|
325
|
+
```go
|
|
326
|
+
import { awscdk } from 'projen';
|
|
327
|
+
import { CdkDiffIamTemplate } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
328
|
+
|
|
329
|
+
const project = new awscdk.AwsCdkConstructLibrary({
|
|
330
|
+
// ...
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
new CdkDiffIamTemplate({
|
|
334
|
+
project,
|
|
335
|
+
roleName: 'CdkChangesetRole',
|
|
336
|
+
createOidcRole: true,
|
|
337
|
+
oidcRoleName: 'GitHubOIDCRole', // Optional, default: 'GitHubOIDCRole'
|
|
338
|
+
githubOidc: {
|
|
339
|
+
owner: 'my-org', // GitHub org or username
|
|
340
|
+
repositories: ['infra-repo', 'app-repo'], // Repos allowed to assume roles
|
|
341
|
+
branches: ['main', 'release/*'], // Branch patterns (default: ['*'])
|
|
342
|
+
},
|
|
343
|
+
// Optional: Skip OIDC provider creation if it already exists
|
|
344
|
+
// skipOidcProviderCreation: true,
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
project.synth();
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### Without Projen (Standalone Generator)
|
|
351
|
+
|
|
352
|
+
```go
|
|
353
|
+
import { CdkDiffIamTemplateGenerator } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
354
|
+
import * as fs from 'fs';
|
|
355
|
+
|
|
356
|
+
const template = CdkDiffIamTemplateGenerator.generateTemplate({
|
|
357
|
+
roleName: 'CdkChangesetRole',
|
|
358
|
+
createOidcRole: true,
|
|
359
|
+
oidcRoleName: 'GitHubOIDCRole',
|
|
360
|
+
githubOidc: {
|
|
361
|
+
owner: 'my-org',
|
|
362
|
+
repositories: ['infra-repo'],
|
|
363
|
+
branches: ['main'],
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
fs.writeFileSync('cdk-diff-iam-template.yaml', template);
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
#### With Existing OIDC Provider (Skip Creation)
|
|
371
|
+
|
|
372
|
+
If your account already has a GitHub OIDC provider but you want the template to create the roles:
|
|
373
|
+
|
|
374
|
+
```go
|
|
375
|
+
new CdkDiffIamTemplate({
|
|
376
|
+
project,
|
|
377
|
+
roleName: 'CdkChangesetRole',
|
|
378
|
+
createOidcRole: true,
|
|
379
|
+
skipOidcProviderCreation: true, // Account already has OIDC provider
|
|
380
|
+
githubOidc: {
|
|
381
|
+
owner: 'my-org',
|
|
382
|
+
repositories: ['*'], // All repos in org
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Deploy Task
|
|
388
|
+
|
|
389
|
+
A Projen task is added for easy deployment:
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
npx projen deploy-cdkdiff-iam-template -- --parameter-overrides GitHubOIDCRoleArn=... # plus any extra AWS CLI args
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### CdkDiffIamTemplate Props
|
|
396
|
+
|
|
397
|
+
| Property | Type | Description |
|
|
398
|
+
|----------|------|-------------|
|
|
399
|
+
| `roleName` | `string` | Name for the changeset IAM role (required) |
|
|
400
|
+
| `oidcRoleArn` | `string?` | ARN of existing GitHub OIDC role. Required when `createOidcRole` is false. |
|
|
401
|
+
| `oidcRegion` | `string?` | Region for OIDC trust condition. Required when `createOidcRole` is false. |
|
|
402
|
+
| `createOidcRole` | `boolean?` | Create OIDC role within template (default: false) |
|
|
403
|
+
| `oidcRoleName` | `string?` | Name of OIDC role to create (default: 'GitHubOIDCRole') |
|
|
404
|
+
| `githubOidc` | `GitHubOidcConfig?` | GitHub OIDC config. Required when `createOidcRole` is true. |
|
|
405
|
+
| `skipOidcProviderCreation` | `boolean?` | Skip OIDC provider if it exists (default: false) |
|
|
406
|
+
| `outputPath` | `string?` | Template output path (default: 'cdk-diff-workflow-iam-template.yaml') |
|
|
407
|
+
|
|
408
|
+
### What the Template Creates
|
|
409
|
+
|
|
410
|
+
**External OIDC Role mode:**
|
|
411
|
+
|
|
412
|
+
* Parameter `GitHubOIDCRoleArn` — ARN of your existing GitHub OIDC role
|
|
413
|
+
* IAM role `CdkChangesetRole` with minimal permissions for change set operations
|
|
414
|
+
* Outputs: `CdkChangesetRoleArn`, `CdkChangesetRoleName`
|
|
415
|
+
|
|
416
|
+
**Self-Contained mode (`createOidcRole: true`):**
|
|
417
|
+
|
|
418
|
+
* GitHub OIDC Provider (unless `skipOidcProviderCreation: true`)
|
|
419
|
+
* IAM role `GitHubOIDCRole` with trust policy for GitHub Actions
|
|
420
|
+
* IAM role `CdkChangesetRole` with minimal permissions (trusts the OIDC role)
|
|
421
|
+
* Outputs: `GitHubOIDCProviderArn`, `GitHubOIDCRoleArn`, `GitHubOIDCRoleName`, `CdkChangesetRoleArn`, `CdkChangesetRoleName`
|
|
422
|
+
|
|
423
|
+
**Changeset Role Permissions:**
|
|
424
|
+
|
|
425
|
+
* CloudFormation Change Set operations
|
|
426
|
+
* Access to CDK bootstrap S3 buckets and SSM parameters
|
|
427
|
+
* `iam:PassRole` to `cloudformation.amazonaws.com`
|
|
428
|
+
|
|
429
|
+
Use the created changeset role ARN as `changesetRoleToAssumeArn` in `CdkDiffStackWorkflow`.
|
|
430
|
+
|
|
431
|
+
---
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
## Usage: CdkDriftDetectionWorkflow
|
|
435
|
+
|
|
436
|
+
`CdkDriftDetectionWorkflow` creates a single workflow file (default `drift-detection.yml`) that can run on a schedule and via manual dispatch. It generates a helper script at `.github/workflows/scripts/detect-drift.ts` (by default) that uses AWS SDK v3 to run drift detection, write optional machine‑readable JSON, and print an HTML report for the Step Summary.
|
|
437
|
+
|
|
438
|
+
Example `.projenrc.ts`:
|
|
439
|
+
|
|
440
|
+
```go
|
|
441
|
+
import { awscdk } from 'projen';
|
|
442
|
+
import { CdkDriftDetectionWorkflow } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
443
|
+
|
|
444
|
+
const project = new awscdk.AwsCdkConstructLibrary({ github: true, /* ... */ });
|
|
445
|
+
|
|
446
|
+
new CdkDriftDetectionWorkflow({
|
|
447
|
+
project,
|
|
448
|
+
workflowName: 'Drift Detection', // optional; file name derived as 'drift-detection.yml'
|
|
449
|
+
schedule: '0 1 * * *', // optional cron
|
|
450
|
+
createIssues: true, // default true; create/update issue when drift detected on schedule
|
|
451
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
452
|
+
oidcRegion: 'us-east-1',
|
|
453
|
+
// Optional: Node version (default '24.x')
|
|
454
|
+
// nodeVersion: '24.x',
|
|
455
|
+
// Optional: Where to place the helper script (default '.github/workflows/scripts/detect-drift.ts')
|
|
456
|
+
// scriptOutputPath: '.github/workflows/scripts/detect-drift.ts',
|
|
457
|
+
stacks: [
|
|
458
|
+
{
|
|
459
|
+
stackName: 'MyAppStack-Prod',
|
|
460
|
+
driftDetectionRoleToAssumeArn: 'arn:aws:iam::123456789012:role/cdk-drift-role',
|
|
461
|
+
driftDetectionRoleToAssumeRegion: 'us-east-1',
|
|
462
|
+
// failOnDrift: true, // optional (default true)
|
|
463
|
+
},
|
|
464
|
+
],
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
project.synth();
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### CdkDriftDetectionWorkflow props
|
|
471
|
+
|
|
472
|
+
* `project` (required) — Your Projen project instance.
|
|
473
|
+
* `stacks` (required) — Array of stacks to check.
|
|
474
|
+
* `oidcRoleArn` (required) — Default OIDC role ARN used before chaining into per‑stack drift roles.
|
|
475
|
+
* `oidcRegion` (required) — Default OIDC region.
|
|
476
|
+
* `workflowName` (optional, default `'drift-detection'`) — Human‑friendly workflow name; the file name is derived in kebab‑case.
|
|
477
|
+
* `schedule` (optional) — Cron expression for automatic runs.
|
|
478
|
+
* `createIssues` (optional, default `true`) — When true, scheduled runs will create/update a GitHub issue if drift is detected.
|
|
479
|
+
* `nodeVersion` (optional, default `'24.x'`) — Node.js version for the runner.
|
|
480
|
+
* `scriptOutputPath` (optional, default `'.github/workflows/scripts/detect-drift.ts'`) — Where to write the helper script.
|
|
481
|
+
|
|
482
|
+
### Per‑stack fields
|
|
483
|
+
|
|
484
|
+
* `stackName` (required) — The full CloudFormation stack name.
|
|
485
|
+
* `driftDetectionRoleToAssumeArn` (required) — Role to assume (after OIDC) for making drift API calls.
|
|
486
|
+
* `driftDetectionRoleToAssumeRegion` (required) — Region for that role and API calls.
|
|
487
|
+
* `failOnDrift` (optional, default `true`) — Intended to fail the detection step on drift. The provided script exits with non‑zero when drift is found; the job continues to allow artifact upload and issue creation.
|
|
488
|
+
|
|
489
|
+
### What gets generated
|
|
490
|
+
|
|
491
|
+
* `.github/workflows/<kebab(workflowName)>.yml` — A workflow with one job per stack plus a final summary job.
|
|
492
|
+
* `.github/workflows/scripts/detect-drift.ts` — Helper script that:
|
|
493
|
+
|
|
494
|
+
* Starts drift detection and polls until completion
|
|
495
|
+
* Lists non‑`IN_SYNC` resources and builds an HTML report
|
|
496
|
+
* Writes optional JSON to `DRIFT_DETECTION_OUTPUT` when set
|
|
497
|
+
* Prints to stdout and appends to the GitHub Step Summary when available
|
|
498
|
+
|
|
499
|
+
### Artifacts and summary
|
|
500
|
+
|
|
501
|
+
* Each stack job uploads `drift-results-<stack>.json` (if produced).
|
|
502
|
+
* A final `Drift Detection Summary` job downloads all artifacts and prints a consolidated summary.
|
|
503
|
+
|
|
504
|
+
### Manual dispatch
|
|
505
|
+
|
|
506
|
+
* The workflow exposes an input named `stack` with choices including each configured stack and an `all` option.
|
|
507
|
+
* Choose a specific stack to run drift detection for that stack only, or select `all` (or leave the input empty) to run all stacks.
|
|
508
|
+
|
|
509
|
+
Note: The default workflow does not post PR comments for drift. It can create/update an Issue on scheduled runs when `createIssues` is `true`.
|
|
510
|
+
|
|
511
|
+
### Post-notification steps (e.g., Slack)
|
|
512
|
+
|
|
513
|
+
You can add your own GitHub Action steps to run after the drift detection step for each stack using `postGitHubSteps`.
|
|
514
|
+
Provide your own Slack payload/markdown (this library no longer generates a payload step for you).
|
|
515
|
+
|
|
516
|
+
Option A: slackapi/slack-github-action (Incoming Webhook, official syntax)
|
|
517
|
+
|
|
518
|
+
```go
|
|
519
|
+
new CdkDriftDetectionWorkflow({
|
|
520
|
+
project,
|
|
521
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
522
|
+
oidcRegion: 'us-east-1',
|
|
523
|
+
stacks: [/* ... */],
|
|
524
|
+
postGitHubSteps: ({ stack }) => {
|
|
525
|
+
// Build a descriptive name per stack
|
|
526
|
+
const name = `Notify Slack (${stack} post-drift)`;
|
|
527
|
+
const step = {
|
|
528
|
+
name,
|
|
529
|
+
uses: 'slackapi/slack-github-action@v2.1.1',
|
|
530
|
+
// by default, post steps run only when drift is detected; you can override `if`
|
|
531
|
+
if: "always() && steps.drift.outcome == 'failure'",
|
|
532
|
+
// Use official inputs: webhook + webhook-type, and a YAML payload with blocks
|
|
533
|
+
with: {
|
|
534
|
+
webhook: '${{ secrets.CDK_NOTIFICATIONS_SLACK_WEBHOOK }}',
|
|
535
|
+
'webhook-type': 'incoming-webhook',
|
|
536
|
+
payload: [
|
|
537
|
+
'text: "** ${{ env.STACK_NAME }} ** has drifted!"',
|
|
538
|
+
'blocks:',
|
|
539
|
+
' - type: "section"',
|
|
540
|
+
' text:',
|
|
541
|
+
' type: "mrkdwn"',
|
|
542
|
+
' text: "*Stack:* ${{ env.STACK_NAME }} (region ${{ env.AWS_REGION }}) has drifted:exclamation:"',
|
|
543
|
+
' - type: "section"',
|
|
544
|
+
' fields:',
|
|
545
|
+
' - type: "mrkdwn"',
|
|
546
|
+
' text: "*Stack ARN*\\n${{ steps.drift.outputs.stack-arn }}"',
|
|
547
|
+
' - type: "mrkdwn"',
|
|
548
|
+
' text: "*Issue*\\n<${{ github.server_url }}/${{ github.repository }}/issues/${{ steps.issue.outputs.result }}|#${{ steps.issue.outputs.result }}>"',
|
|
549
|
+
].join('\n'),
|
|
550
|
+
},
|
|
551
|
+
};
|
|
552
|
+
return [step];
|
|
553
|
+
},
|
|
554
|
+
});
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
Note: The Issue link requires `createIssues: true` (default) so that the `Create Issue on Drift` step runs before this Slack step and exposes `steps.issue.outputs.result`. This library orders the steps accordingly.
|
|
558
|
+
|
|
559
|
+
Details:
|
|
560
|
+
|
|
561
|
+
* `postGitHubSteps` can be:
|
|
562
|
+
|
|
563
|
+
* an array of step objects, or
|
|
564
|
+
* a factory function `({ stack }) => step | step[]`.
|
|
565
|
+
* Each step you provide is inserted after the results are uploaded.
|
|
566
|
+
* Default condition: if you do not set `if` on your step, it will default to `always() && steps.drift.outcome == 'failure'`.
|
|
567
|
+
* Available context/env you can use:
|
|
568
|
+
|
|
569
|
+
* `${{ env.STACK_NAME }}`, `${{ env.DRIFT_DETECTION_OUTPUT }}`
|
|
570
|
+
* `${{ steps.drift.outcome }}` — success/failure of the detect step
|
|
571
|
+
* `${{ steps.drift.outputs.stack-arn }}` — Stack ARN resolved at runtime
|
|
572
|
+
* `${{ steps.issue.outputs.result }}` — Issue number if the workflow created/found one (empty when not applicable)
|
|
573
|
+
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
## Usage: CdkDriftIamTemplate
|
|
577
|
+
|
|
578
|
+
Emit an example IAM template you can deploy in your account for the Drift Detection workflow.
|
|
579
|
+
|
|
580
|
+
### With Projen
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
import { awscdk } from 'projen';
|
|
584
|
+
import { CdkDriftIamTemplate } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
585
|
+
|
|
586
|
+
const project = new awscdk.AwsCdkConstructLibrary({
|
|
587
|
+
// ...
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
new CdkDriftIamTemplate({
|
|
591
|
+
project,
|
|
592
|
+
roleName: 'cdk-drift-role',
|
|
593
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
594
|
+
oidcRegion: 'us-east-1',
|
|
595
|
+
// Optional: custom output path (default: 'cdk-drift-workflow-iam-template.yaml')
|
|
596
|
+
// outputPath: 'infra/cdk-drift-iam.yaml',
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
project.synth();
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
A Projen task is also added:
|
|
603
|
+
|
|
604
|
+
```bash
|
|
605
|
+
npx projen deploy-cdkdrift-iam-template -- --parameter-overrides GitHubOIDCRoleArn=... # plus any extra AWS CLI args
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
### Without Projen (Standalone Generator)
|
|
609
|
+
|
|
610
|
+
```go
|
|
611
|
+
import { CdkDriftIamTemplateGenerator } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
612
|
+
import * as fs from 'fs';
|
|
613
|
+
|
|
614
|
+
const template = CdkDriftIamTemplateGenerator.generateTemplate({
|
|
615
|
+
roleName: 'cdk-drift-role',
|
|
616
|
+
oidcRoleArn: 'arn:aws:iam::123456789012:role/github-oidc-role',
|
|
617
|
+
oidcRegion: 'us-east-1',
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
fs.writeFileSync('cdk-drift-iam-template.yaml', template);
|
|
621
|
+
|
|
622
|
+
// Get the deploy command
|
|
623
|
+
const deployCmd = CdkDriftIamTemplateGenerator.generateDeployCommand('cdk-drift-iam-template.yaml');
|
|
624
|
+
console.log('Deploy with:', deployCmd);
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
### What the template defines
|
|
628
|
+
|
|
629
|
+
* Parameter `GitHubOIDCRoleArn` with a default from `oidcRoleArn` — the ARN of your existing GitHub OIDC role allowed to assume this drift role.
|
|
630
|
+
* IAM role `CdkDriftRole` with minimal permissions for CloudFormation drift detection operations.
|
|
631
|
+
* Outputs exporting the role name and ARN.
|
|
632
|
+
|
|
633
|
+
---
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
## Usage: CdkDiffIamTemplateStackSet (Org-Wide Deployment)
|
|
637
|
+
|
|
638
|
+
`CdkDiffIamTemplateStackSet` creates a CloudFormation StackSet template for deploying GitHub OIDC provider, OIDC role, and CDK diff/drift IAM roles across an entire AWS Organization. This is the recommended approach for organizations that want to enable CDK diff/drift workflows across multiple accounts.
|
|
639
|
+
|
|
640
|
+
### Architecture
|
|
641
|
+
|
|
642
|
+
Each account in your organization gets:
|
|
643
|
+
|
|
644
|
+
* **GitHub OIDC Provider** — Authenticates GitHub Actions workflows
|
|
645
|
+
* **GitHubOIDCRole** — Trusts the OIDC provider with repo/branch restrictions
|
|
646
|
+
* **CdkChangesetRole** — For PR change set previews (trusts GitHubOIDCRole)
|
|
647
|
+
* **CdkDriftRole** — For drift detection (trusts GitHubOIDCRole)
|
|
648
|
+
|
|
649
|
+
This is a self-contained deployment with **no role chaining required**.
|
|
650
|
+
|
|
651
|
+
### With Projen
|
|
652
|
+
|
|
653
|
+
```go
|
|
654
|
+
import { awscdk } from 'projen';
|
|
655
|
+
import { CdkDiffIamTemplateStackSet } from '@jjrawlins/cdk-diff-pr-github-action';
|
|
656
|
+
|
|
657
|
+
const project = new awscdk.AwsCdkConstructLibrary({ /* ... */ });
|
|
658
|
+
|
|
659
|
+
new CdkDiffIamTemplateStackSet({
|
|
660
|
+
project,
|
|
661
|
+
githubOidc: {
|
|
662
|
+
owner: 'my-org', // GitHub org or username
|
|
663
|
+
repositories: ['infra-repo', 'app-repo'], // Repos allowed to assume roles
|
|
664
|
+
branches: ['main', 'release/*'], // Branch patterns (default: ['*'])
|
|
665
|
+
},
|
|
666
|
+
targetOrganizationalUnitIds: ['ou-xxxx-xxxxxxxx'], // Target OUs
|
|
667
|
+
regions: ['us-east-1', 'eu-west-1'], // Target regions
|
|
668
|
+
// Optional settings:
|
|
669
|
+
// oidcRoleName: 'GitHubOIDCRole', // default
|
|
670
|
+
// changesetRoleName: 'CdkChangesetRole', // default
|
|
671
|
+
// driftRoleName: 'CdkDriftRole', // default
|
|
672
|
+
// roleSelection: StackSetRoleSelection.BOTH, // BOTH, CHANGESET_ONLY, or DRIFT_ONLY
|
|
673
|
+
// delegatedAdmin: true, // Use --call-as DELEGATED_ADMIN (default: true)
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
project.synth();
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
This creates:
|
|
680
|
+
|
|
681
|
+
* `cdk-diff-workflow-stackset-template.yaml` — CloudFormation template
|
|
682
|
+
* Projen tasks for StackSet management
|
|
683
|
+
|
|
684
|
+
**Projen tasks:**
|
|
685
|
+
|
|
686
|
+
```bash
|
|
687
|
+
npx projen stackset-create # Create the StackSet
|
|
688
|
+
npx projen stackset-update # Update the StackSet template
|
|
689
|
+
npx projen stackset-deploy-instances # Deploy to target OUs/regions
|
|
690
|
+
npx projen stackset-delete-instances # Remove stack instances
|
|
691
|
+
npx projen stackset-delete # Delete the StackSet
|
|
692
|
+
npx projen stackset-describe # Show StackSet status
|
|
693
|
+
npx projen stackset-list-instances # List all instances
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
### Without Projen (Standalone Generator)
|
|
697
|
+
|
|
698
|
+
For non-Projen projects, use `CdkDiffIamTemplateStackSetGenerator` directly:
|
|
699
|
+
|
|
700
|
+
```go
|
|
701
|
+
import {
|
|
702
|
+
CdkDiffIamTemplateStackSetGenerator
|
|
703
|
+
} from '@jjrawlins/cdk-diff-pr-github-action';
|
|
704
|
+
import * as fs from 'fs';
|
|
705
|
+
|
|
706
|
+
// Generate the CloudFormation template
|
|
707
|
+
const template = CdkDiffIamTemplateStackSetGenerator.generateTemplate({
|
|
708
|
+
githubOidc: {
|
|
709
|
+
owner: 'my-org',
|
|
710
|
+
repositories: ['infra-repo'],
|
|
711
|
+
branches: ['main'],
|
|
712
|
+
},
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
// Write to file
|
|
716
|
+
fs.writeFileSync('stackset-template.yaml', template);
|
|
717
|
+
|
|
718
|
+
// Get AWS CLI commands for StackSet operations
|
|
719
|
+
const commands = CdkDiffIamTemplateStackSetGenerator.generateCommands({
|
|
720
|
+
stackSetName: 'cdk-diff-workflow-iam-stackset',
|
|
721
|
+
templatePath: 'stackset-template.yaml',
|
|
722
|
+
targetOrganizationalUnitIds: ['ou-xxxx-xxxxxxxx'],
|
|
723
|
+
regions: ['us-east-1'],
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
console.log('Create StackSet:', commands['stackset-create']);
|
|
727
|
+
console.log('Deploy instances:', commands['stackset-deploy-instances']);
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
### GitHub Actions Workflow (Simplified)
|
|
731
|
+
|
|
732
|
+
With per-account OIDC, your workflow is simplified — no role chaining needed:
|
|
733
|
+
|
|
734
|
+
```yaml
|
|
735
|
+
jobs:
|
|
736
|
+
diff:
|
|
737
|
+
runs-on: ubuntu-latest
|
|
738
|
+
permissions:
|
|
739
|
+
id-token: write
|
|
740
|
+
contents: read
|
|
741
|
+
steps:
|
|
742
|
+
- uses: actions/checkout@v4
|
|
743
|
+
|
|
744
|
+
- uses: aws-actions/configure-aws-credentials@v4
|
|
745
|
+
with:
|
|
746
|
+
role-to-assume: arn:aws:iam::${{ env.ACCOUNT_ID }}:role/GitHubOIDCRole
|
|
747
|
+
aws-region: us-east-1
|
|
748
|
+
|
|
749
|
+
- name: Assume Changeset Role
|
|
750
|
+
run: |
|
|
751
|
+
CREDS=$(aws sts assume-role \
|
|
752
|
+
--role-arn arn:aws:iam::${{ env.ACCOUNT_ID }}:role/CdkChangesetRole \
|
|
753
|
+
--role-session-name changeset-session)
|
|
754
|
+
# Export credentials...
|
|
755
|
+
```
|
|
756
|
+
|
|
757
|
+
### GitHubOidcConfig options
|
|
758
|
+
|
|
759
|
+
| Property | Description |
|
|
760
|
+
|----------|-------------|
|
|
761
|
+
| `owner` | GitHub organization or username (required) |
|
|
762
|
+
| `repositories` | Array of repo names, or `['*']` for all repos (required) |
|
|
763
|
+
| `branches` | Array of branch patterns (default: `['*']`) |
|
|
764
|
+
| `additionalClaims` | Extra OIDC claims like `['pull_request', 'environment:production']` |
|
|
765
|
+
|
|
766
|
+
---
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
## Testing
|
|
770
|
+
|
|
771
|
+
This repository includes Jest tests that snapshot the synthesized outputs from Projen and assert that:
|
|
772
|
+
|
|
773
|
+
* Diff workflows are created per stack and contain all expected steps.
|
|
774
|
+
* Drift detection workflow produces one job per stack and a summary job.
|
|
775
|
+
* Only one helper script file is generated per workflow type.
|
|
776
|
+
* Per‑stack OIDC overrides (where supported) are respected.
|
|
777
|
+
* Helpful validation errors are thrown for missing OIDC settings.
|
|
778
|
+
* The IAM template files contain the expected resources and outputs.
|
|
779
|
+
|
|
780
|
+
Run tests with:
|
|
781
|
+
|
|
782
|
+
```bash
|
|
783
|
+
yarn test
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
## Notes
|
|
787
|
+
|
|
788
|
+
* This package assumes your repository is configured with GitHub Actions and that you have a GitHub OIDC role configured in AWS.
|
|
789
|
+
* The generated scripts use the AWS SDK v3 for CloudFormation and, where applicable, the GitHub REST API.
|