Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Missing field LogConfiguration.LogDriver error when enable_cloudwatch_logging is false #91

Merged
merged 1 commit into from
Jun 3, 2023

Conversation

alisson276
Copy link
Contributor

@alisson276 alisson276 commented May 31, 2023

Description

If we pass enable_cloudwatch_logging = false on a service container_definitions section, it will generate this error below:

│ Error: creating ECS Task Definition (my-service): InvalidParameter: 1 validation error(s) found.
│ - missing required field, RegisterTaskDefinitionInput.ContainerDefinitions[0].LogConfiguration.LogDriver.
│ 
│ 
│   with module.ecs.module.service["my-container"].aws_ecs_task_definition.this[0],
│   on ../../modules/aws-compute-ecs/modules/service/main.tf line 608, in resource "aws_ecs_task_definition" "this":
│  608: resource "aws_ecs_task_definition" "this" {

Motivation and Context

This behavior happens because in the planning phase the logConfiguration is computed as {}, which is an invalid configuration when posting on AWS API.
My plan of a just imported resource is:

  # module.ecs.module.service["my-service"].aws_ecs_task_definition.this[0] must be replaced
+/- resource "aws_ecs_task_definition" "this" {
      ~ arn                      = "arn:aws:ecs:us-east-1:1234567891011:task-definition/my-service-definition:20" -> (known after apply)
      ~ arn_without_revision     = "arn:aws:ecs:us-east-1:1234567891011:task-definition/my-service-definition" -> (known after apply)
      ~ container_definitions    = jsonencode(
          ~ [
              ~ {
                  + logConfiguration  = {}
                  - mountPoints       = []
                    name              = "worker"
                  - portMappings      = []
                  - volumesFrom       = []
                    # (7 unchanged attributes hidden)
                },
            ] # forces replacement
        )

And this is computed this way because of a merge():

log_configuration = merge(
    { for k, v in {
      logDriver = "awslogs",
      options = {
        awslogs-region        = data.aws_region.current.name,
        awslogs-group         = try(aws_cloudwatch_log_group.this[0].name, ""),
        awslogs-stream-prefix = "ecs"
      },
    } : k => v if var.enable_cloudwatch_logging },
    var.log_configuration
  )

In the console, if all parameters to merge are null or empty, the result is an empty object:

> merge({}, null)
{}
> merge(null, null)
{}
> merge({}, {a = "A"})
{
  "a" = "A"
}
> length(merge({}, {a = "A"}))
1

Breaking Changes

No breaking change

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@alisson276 alisson276 changed the title Missing field LogConfiguration.LogDriver error when enable_cloudwatch_logging is false fix: Missing field LogConfiguration.LogDriver error when enable_cloudwatch_logging is false May 31, 2023
Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix!

@bryantbiggs bryantbiggs merged commit 8ca6fd4 into terraform-aws-modules:master Jun 3, 2023
antonbabenko pushed a commit that referenced this pull request Jun 3, 2023
### [5.0.2](v5.0.1...v5.0.2) (2023-06-03)

### Bug Fixes

* Missing field LogConfiguration.LogDriver error when enable_cloudwatch_logging is false ([#91](#91)) ([8ca6fd4](8ca6fd4))
@antonbabenko
Copy link
Member

This PR is included in version 5.0.2 🎉

@github-actions
Copy link

github-actions bot commented Jul 4, 2023

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants