CLD-485: only create cloud trail if nesessary - #14
Conversation
c9dc1dc to
3f6b63a
Compare
3f6b63a to
30fb115
Compare
|
rebased with |
|
next steps:
NB version has increased to v0.4.4 on main recently, so this version will need to be bumped also before merging |
EventBridge receives Route 53 management events from any active trail in the account - it does not require a specific trail. Most AWS accounts already have a default multi-region trail (e.g. management-events) with IncludeGlobalServiceEvents enabled, which is sufficient. The custom trail we were creating was a redundant duplicate costing customers ~$3/month extra (2nd trail + KMS key + S3 storage). Changes: - Add CreateCloudTrail parameter (default: false) with AllowedValues true/false - Add ShouldCreateTrail condition gating TrailKMSKey, TrailS3Bucket, TrailBucketPolicy and Trail behind it - Use !If in ConfigureCustomResource to pass an empty CloudTrailBucketName when the bucket was not created - Use !If with AWS::NoValue in ConfigureFunctionRole to omit S3 policy statements when the bucket does not exist - Guard S3 bucket cleanup in configure/app.py behind a non-empty bucket name check to avoid errors on stack delete when no bucket was created - Document the new parameter in README.md Customers who have no existing trail can set CreateCloudTrail=true to retain the previous behaviour.
Surfaces CloudTrail status, EventBridge rule ARN, SQS queue URL and Lambda ARN in the CloudFormation console. The CloudTrailStatus output explicitly tells customers whether the stack is managing a trail and reminds them to clean up any orphaned NS1CloudSyncTrail after upgrade.
- Add Update handler in configure/app.py to stop, delete and empty the CloudSync-created trail and S3 bucket when upgrading from CreateCloudTrail=true to false, preventing CloudFormation from failing to delete a non-empty bucket - Handle 0.4.3 upgrade path where CreateCloudTrail did not exist in OldResourceProperties, defaulting old value to 'true' (trail was always createdi in 0.4.3 and earlier) - Fall back to s3:ListAllMyBuckets scan for bucket name when upgrading from 0.4.3 where CloudTrailBucketName was not present in OldResourceProperties - Safety guard: only delete trails named NS1CloudSyncTrail or CloudSyncTrail — all other trail names (management-events, org trails etc.) are skipped - Pass CloudTrailName and CreateCloudTrail through ConfigureCustomResource so the Lambda has access to them in OldResourceProperties during updates - Add s3:ListAllMyBuckets, s3:DeleteObject/GetObject/ListBucket, cloudtrail: StopLogging/DeleteTrail permissions to ConfigureFunctionRole - Update CreateCloudTrail and CloudTrailName parameter descriptions with clearer guidance and warnings DNSUpdateFunctionArn
Bucket deletion during a stack update is size-dependent and may cause the Lambda to timeout, leaving the stack in UPDATE_ROLLBACK_FAILED. A real-world test on a 1-month-old bucket (~927MB, ~144k objects) caused a 15-minute DNS sync outage via Lambda timeout and CloudFormation retry. Instead, stop and delete the CloudSync-managed trail only (fast), retain the S3 bucket, and warn the customer via CloudWatch logs to empty and delete it manually once DNS sync is verified working. Also surface the retained bucket name in a new RetainedCloudTrailBucket CloudFormation output so customers can find it without digging through logs.
Add AllowedPattern to CloudTrailName to block known system trail names (management-events, aws-controltower-BaselineCloudTrail, AccountTrail) at CloudFormation validation time, preventing accidental deletion of existing trails. Prepend description with "ONLY USED when CreateCloudTrail is true" so it is the first thing visible in the SAR console parameter UI. Update README to bold the same message in the parameters table. Fix wording on RetainedCloudTrailBucket output description.
30fb115 to
2c7d9db
Compare
|
rebased with |
|
status update in https://nsone.slack.com/archives/D097YJXHP5G/p1785413015439009 |
|
Key Points for IBM docs 1. New parameter:
2. DNS sync is not interrupted 3. Changing
4. S3 log bucket is NOT automatically deleted — manual cleanup required aws s3 rm s3://cloudsync-trail-bucket-<suffix> --recursive
aws s3 rb s3://cloudsync-trail-bucket-<suffix>5. Expect ~10 minutes of CloudFormation cleanup noise — this is normal 6. Check the Outputs tab after deploying 7. Downgrading from v0.4.6 There is no one-click rollback in AWS CloudFormation. Recommended approach — avoid needing to downgrade at all: Full rollback via Full rollback for SAR-installed stacks: Auto-rollback risk:
|
EventBridge receives Route 53 management events from any active trail in the account - it does not require a specific trail. Most AWS accounts already have a default multi-region trail (e.g. management-events) with IncludeGlobalServiceEvents enabled, which is sufficient. The custom trail we were creating was a redundant duplicate costing customers ~$3/month extra (2nd trail + KMS key + S3 storage).
Changes:
Customers who have no existing trail can set CreateCloudTrail=true to retain the previous behaviour.
One Key Point is :
So safest option is to not try and uto delete the bucket, but warn the user to manually delete it after.
I have more detailed and test details documented here: https://ns1inc.atlassian.net/browse/CLD-485?focusedCommentId=1655636