Detect terraform drift in deploy / integration envs#1889
Conversation
a703cef to
3cef1ee
Compare
47a1d9d to
40a4de0
Compare
3cef1ee to
28eabd4
Compare
d3ea388 to
6cc088e
Compare
28eabd4 to
0f21e8c
Compare
6cc088e to
7f4b384
Compare
457dc35 to
a2f494a
Compare
093f0a1 to
301ef15
Compare
e4e689d to
f5f6c78
Compare
|
NB: the TODO in infra/deployments/integration/account/drift-detection.tf should be removed to revert back to |
There was a problem hiding this comment.
Pull request overview
This PR implements automated drift detection for Terraform deployments in the deploy and integration environments. The solution uses CodeBuild to periodically check if deployed infrastructure matches the current state of the main branch by comparing git SHAs stored in SSM parameters.
Key Changes:
- Added a new
drift-detectionTerraform module that creates CodeBuild projects, EventBridge schedules, and IAM roles for automated drift checking - Implemented a bash script that clones the repository, builds module dependency trees, and compares deployed SHAs against current code
- Configured drift detection in both integration/account and deploy/tools roots with weekly Monday 9 AM UTC schedules
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| infra/modules/drift-detection/variables.tf | Defines input variables for deployment name, schedule, git repository URL, and branch |
| infra/modules/drift-detection/outputs.tf | Exports CodeBuild project details, EventBridge rule, and CloudWatch log group names |
| infra/modules/drift-detection/main.tf | Creates CodeBuild project with buildspec that installs hcl2json and runs drift detection |
| infra/modules/drift-detection/iam.tf | Sets up IAM roles and policies for CodeBuild and EventBridge with appropriate SSM read permissions |
| infra/modules/drift-detection/eventbridge.tf | Configures EventBridge scheduled rule to trigger CodeBuild drift checks |
| infra/modules/drift-detection/drift-detection.sh | Bash script that performs drift detection by comparing git SHAs and analyzing module dependencies |
| infra/deployments/integration/inputs.tf | Adds drift_detection_schedule variable for integration environment |
| infra/deployments/integration/account/drift-detection.tf | Instantiates drift detection module for integration (currently pointing to test branch) |
| infra/deployments/deploy/tools/drift-detection.tf | Instantiates drift detection module for deploy environment |
| infra/deployments/deploy/inputs.tf | Adds drift_detection_schedule variable for deploy environment |
Comments suppressed due to low confidence (1)
infra/modules/drift-detection/main.tf:27
- The script downloads the latest version of hcl2json from GitHub without verifying checksums or signatures. This could be a security risk if the GitHub API is compromised or returns unexpected data. Consider pinning to a specific version and verifying checksums, or using a more secure installation method.
"HCL2JSON_VERSION=\"$(curl -s https://api.github.com/repos/tmccombs/hcl2json/releases/latest | jq -r '.tag_name')\"",
"curl https://github.com/tmccombs/hcl2json/releases/download/$${HCL2JSON_VERSION}/hcl2json_linux_amd64 -L -o hcl2json",
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
a4e9397 to
4ea9f3b
Compare
sarahseewhy
left a comment
There was a problem hiding this comment.
This is really cool and will save us the toil task of doing manual checks. Thank you 👏
Let's chat as a team about alert etc (which you mention in the PR description).
And thanks for the note about changing the branch name back to main before merging. Just so we don't forget to change the branch name, I'm going to mark this as "Request changes" although there's nothing else I think needs changing.
4ea9f3b to
39b8409
Compare
6b88683 to
a73e37f
Compare
a73e37f to
22a1646
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
sarahseewhy
left a comment
There was a problem hiding this comment.
I think this looks fine. Copilot has some granular suggestions (I do agree with the typo one about list vs last, can't believe I missed that). How would you like to proceed?
22a1646 to
cc62125
Compare
cc62125 to
fd67e6a
Compare
This module sets up a CodeBuild project and EventBridge rule to periodically check for drift in Terraform roots by comparing the current Git commit SHA against the last applied SHAs stored in SSM Parameter Store. It reports any discrepancies found.
Sometimes it's useful to make PRs against branches other than main
fd67e6a to
a1ddd07
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
What problem does this pull request solve?
Trello card: https://trello.com/c/QV2ruMG8/777-automate-checks-for-whether-deploy-and-integration-are-up-to-date-with-main
This PR adds drift detection for deploy and integration deployments on top of the SSM parameter recording from the previous PR.
Currently, this just runs a codebuild project weekly. need to discuss alerting / general logic.
Things to consider when reviewing
Prerequisites
Deploy all integration infrastructure to establish baseline:
This will:
hcl2jsoninstalledTest 1: Verify No Drift (Baseline)
With all integration roots freshly applied, trigger drift detection:
Expected: All roots show
✅ UP TO DATE: No changes detectedTest 2: Create Drift and Verify Detection
Push a commit with changes but don't apply it:
Trigger drift detection:
Expected output:
integration/accountshows:⚠️ DRIFT DETECTEDinfra/deployments/integration/account/README.mdintegration/review) show:✅ UP TO DATETest 3: Verify Module Dependency Graph
Test that drift is correctly isolated to specific module dependencies:
Expected: Only
integration/reviewshould show drift (notintegration/account), verifying the dependency tree is built correctly per-root.Test 4: Verify hcl2json Installation
Check that hcl2json is successfully installed in the install phase:
Expected: Should see hcl2json version downloaded and
hcl2json --versionoutput.Clean Up
Key Behaviors to Verify
hcl2jsonsuccessfully installed in CodeBuild install phasejqReminders
If you've made changes to the deployer role (files in
modules/deployer-access):make <environment> forms/account applyon the relevant environments (dev,staging,user-research, and/orprod)apply-forms-terraform-<environment>pipelines have run successfully