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

Error when engine-log + slow-log without a specific log group name defined #16

Open
fmidaglia-gocloud opened this issue Sep 29, 2024 · 1 comment

Comments

@fmidaglia-gocloud
Copy link

Description

An error is found when enabling slow-log and engine-log and in both cases the name of the log-group is not specified so that the module completes it with the default

example to reproduce the error:

provider "aws" {
  region = local.region
}

data "aws_availability_zones" "available" {}

locals {
  region = "eu-west-1"
  name   = "ex-${basename(path.cwd)}"

  vpc_cidr = "10.0.0.0/16"
  azs      = slice(data.aws_availability_zones.available.names, 0, 3)

  tags = {
    Name       = local.name
    Example    = local.name
    Repository = "https://github.com/terraform-aws-modules/terraform-aws-elasticache"
  }
}

################################################################################
# ElastiCache Module
################################################################################
module "elasticache" {
  source = "../../"

  cluster_id               = local.name
  create_cluster           = true
  create_replication_group = false

  engine_version = "7.1"
  node_type      = "cache.t4g.small"

  maintenance_window = "sun:05:00-sun:09:00"
  apply_immediately  = true

  # Security Group
  vpc_id = module.vpc.vpc_id
  security_group_rules = {
    ingress_vpc = {
      # Default type is `ingress`
      # Default port is based on the default engine port
      description = "VPC traffic"
      cidr_ipv4   = module.vpc.vpc_cidr_block
    }
  }

  # Subnet Group
  subnet_group_name        = local.name
  subnet_group_description = "${title(local.name)} subnet group"
  subnet_ids               = module.vpc.private_subnets

  # Parameter Group
  create_parameter_group      = true
  parameter_group_name        = local.name
  parameter_group_family      = "redis7"
  parameter_group_description = "${title(local.name)} parameter group"
  parameters = [
    {
      name  = "latency-tracking"
      value = "yes"
    }
  ]

  # Log Configuration
  log_delivery_configuration = {
    engine-log = {
      # cloudwatch_log_group_name = "elasticache-engine-log"
      destination_type          = "cloudwatch-logs"
      log_format                = "json"
    }
    slow-log = {
      # cloudwatch_log_group_name = "elasticache-slow-log"
      destination_type          = "cloudwatch-logs"
      log_format                = "json"
    }
  }

  tags = local.tags
}

################################################################################
# Supporting Resources
################################################################################

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"

  name = local.name
  cidr = local.vpc_cidr

  azs             = local.azs
  public_subnets  = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
  private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)]

  tags = local.tags
}
  • [✅] ✋ I have searched the open/closed issues and my issue is not listed.

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

  • Module version [Required]: 1.2.3

  • Terraform version: v1.9.6

  • Provider version(s): aws v5.69.0

Reproduction Code [Required]

Steps to reproduce the behavior:

Expected behavior

Two log groups with different name created:

  • /aws/elasticache/dmc-prd-example-00-engine
  • /aws/elasticache/dmc-prd-example-00-slow

Actual behavior

Two log groups with the same name are traing to be created

  • /aws/elasticache/dmc-prd-example-00
  • /aws/elasticache/dmc-prd-example-00

Terminal Output Screenshot(s)

image

Additional context

Possible solution main.tf line 212 replace resource "aws_cloudwatch_log_group" "this" with:

resource "aws_cloudwatch_log_group" "this" {
  for_each = { for k, v in var.log_delivery_configuration : k => v if local.create_cloudwatch_log_group && try(v.create_cloudwatch_log_group, true) && try(v.destination_type, "") == "cloudwatch-logs" }

  name = format("/aws/elasticache/%s",
    coalesce(
      try(each.value.cloudwatch_log_group_name, null),
      format("%s-%s",coalesce(var.cluster_id, var.replication_group_id, ""),
        each.key == "slow-log" ? "slow" : "engine"
      )
    )
  )

  retention_in_days = try(each.value.cloudwatch_log_group_retention_in_days, 14)
  kms_key_id        = try(each.value.cloudwatch_log_group_kms_key_id, null)
  skip_destroy      = try(each.value.cloudwatch_log_group_skip_destroy, null)
  log_group_class   = try(each.value.cloudwatch_log_group_class, null)

  tags = merge(local.tags, try(each.value.tags, {}))
}
@tanjinP
Copy link

tanjinP commented Oct 25, 2024

Here is a potential workaround, just create the group manually outside the module:

module "my_redis" {
...
log_delivery_configuration = {
    slow-log = {
      destination_type = "cloudwatch-logs"
      log_format       = "json"
      destination      = "/aws/elasticache/some_location/slowlog"
    }
    engine-log = {
      destination_type = "cloudwatch-logs"
      log_format       = "json"
     
      # must be done due to a bug in provider
      create_cloudwatch_log_group = false
      destination                 = aws_cloudwatch_log_group.engine.name
    }
  }
  tags = {
    Tier = var.env
    Env  = var.env
    Name = var.name
  }
}

resource "aws_cloudwatch_log_group" "engine" {
  name              = "/aws/elasticache/some_location/enginelog"
  retention_in_days = 14  # default from module
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants