fix: correct CloudWatch Log Group dependency ordering#45
Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit intoApr 9, 2026
Merged
Conversation
The log group's `name` attribute referenced `aws_lambda_function.forwarder.function_name`, creating an implicit dependency that forced the Lambda to be created BEFORE the log group. This caused `ResourceAlreadyExistsException` failures when: - Migrating accounts between deployment patterns (e.g. per-account configs to herds of accounts), where orphaned log groups from previous deployments collide with the new Terraform state. - Destroy-and-recreate scenarios where the Lambda is replaced and external triggers invoke it before Terraform recreates the log group. Two changes fix this by inverting the dependency direction: 1. Log group `name` now uses `var.function_name` directly instead of referencing the Lambda resource. This breaks the wrong-direction implicit dependency. The resolved string is identical since `aws_lambda_function.forwarder.function_name` was already set to `var.function_name`. 2. A `logging_config` block on the Lambda explicitly points it at the Terraform-managed log group. This creates the correct dependency (Lambda depends on log group) and tells the Lambda runtime to use the specified log group rather than auto-creating one. For existing deployments, this is a non-breaking in-place Lambda update (adding the `logging_config` attribute). The log group shows no diff since the name resolves to the same value. `log_format = "Text"` preserves the existing default Lambda logging behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
LorisFriedel
marked this pull request as ready for review
April 9, 2026 08:01
RaphaelAllier
approved these changes
Apr 9, 2026
gh-worker-dd-mergequeue-cf854d
Bot
deleted the
loris.friedel/fix-loggroup-dependency-ordering
branch
April 9, 2026 12:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
aws_cloudwatch_log_group.forwarder_log_groupresource constructs itsnamefromaws_lambda_function.forwarder.function_name, creating an implicit Terraform dependency that forces the Lambda to be created before its log group. This causesResourceAlreadyExistsExceptionfailures because:/aws/lambda/<function_name>if it doesn't exist. Since Terraform creates the Lambda first (due to the wrong dependency direction), any invocation before Terraform reaches the log group resource causes a conflict.terraform importor state migration), the log group already exists and Terraform'sCreateLogGroupcall fails.Root Cause
The dependency is backwards. The log group should exist before the Lambda, not after.
Fix
Two changes that invert the dependency direction:
1. Break the wrong dependency
The resolved string is identical (
var.function_nameis whataws_lambda_function.forwarder.function_namewas set to), but the Terraform dependency edge is removed.2. Create the correct dependency
This tells both Terraform and the Lambda runtime to use the Terraform-managed log group. Terraform creates the log group first, then the Lambda pointing at it. The Lambda runtime won't auto-create a log group.
Alternative considered:
depends_onAn alternative is to add
depends_on = [aws_cloudwatch_log_group.forwarder_log_group]to the Lambda resource (combined with change 1.). This also fixes the Terraform dependency ordering.I preferred
logging_configfor two reasons:AWS-level protection.
depends_ononly fixes the Terraform graph — Lambda can still auto-create the log group if invoked in edge cases (e.g., log group deleted out-of-band, or external triggers firing during a destroy-recreate cycle).logging_configtells the Lambda runtime itself to use the specified log group, preventing auto-creation at the AWS API level.Idiomatic Terraform. The reference to
aws_cloudwatch_log_group.forwarder_log_group.nameinsidelogging_configcreates an implicit dependency through the resource graph, which is the standard Terraform pattern. Explicitdepends_onis recommended only as a last resort since it forces Terraform to defer reading the depended-on resource until apply time.That said,
depends_onis a simpler change with a smaller surface area. Iflogging_configcauses any unforeseen issue during rollout, switching todepends_onis a safe fallback.Impact on existing deployments
logging_config(not a replacement)log_format = "Text"preserves existing defaultlogging_configrequires >= 5.32.0; module already requires >= 6.21Testing
All 54 existing
terraform testcases pass.