hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.86k stars 9.21k forks source link

[Bug]: Provider produced inconsistent result after apply #39146

Open bsantacruz-code opened 2 months ago

bsantacruz-code commented 2 months ago

Terraform Core Version

1.9.5 on darwin_arm64

AWS Provider Version

5.65.0

Affected Resource(s)

aws_elasticache_serverless_cache

Expected Behavior

Create a Redis Serverless database

Actual Behavior

Creates the Redis Serverless database but when you use again terraform apply it's replace the redis database

Relevant Error/Panic Output Snippet

Error: Provider produced inconsistent result after apply

When applying changes to aws_elasticache_serverless_cache.redis, provider "provider[\"registry.terraform.io/hashicorp/aws\"]" produced an unexpected
new value: .kms_key_id: was cty.StringVal("25086aeb-503e-46c1-aed4-cc4b67f3ca60"), but now
cty.StringVal("arn:aws:kms:us-east-1:299186857980:key/25086aeb-503e-46c1-aed4-cc4b67f3ca60").

This is a bug in the provider, which should be reported in the provider's own issue tracker.

Terraform Configuration Files

provider "aws" {
  region  = var.region
  profile = var.cli_profile

  default_tags {
    tags = {
      Project     = var.project
      Environment = var.environment
    }
  }
}

terraform {
  required_version = ">= 1.5"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.30"
    }
  }
}

resource "aws_security_group" "redis_sg" {
  name        = "${var.client}-redis-sg"
  description = "Security group for Redis access"
  vpc_id      = aws_vpc.vpc.id

  ingress {
    from_port   = 6379
    to_port     = 6379
    protocol    = "tcp"
    cidr_blocks = [var.vpc_cidr]
  }

  ingress {
    from_port = 6379
    to_port = 6379
    protocol = "tcp"
    security_groups = [aws_security_group.eks_sg.id]
  }

  ingress {
    from_port   = 6379
    to_port     = 6379
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.client}-redis-sg"
  }
}

resource "aws_kms_key" "redis_key" {
  description = "KMS key for encrypting redis ${var.client}-${var.environment}-redis"
  key_usage   = "ENCRYPT_DECRYPT" # Symmetric key for encryption and decryption
  is_enabled  = true

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
      },
      "Action": "kms:*",
      "Resource": "*"
    },
     {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/bsantacruz"
      },
      "Action": "kms:*",
      "Resource": "*"
    }
  ]
}
EOF

  tags = {
    Name = "${var.client}-${var.environment}-redis-kms-key"
  }
}

# Allow the necessary IAM roles and services to use the key
resource "aws_kms_alias" "redis_key_alias" {
  name          = "alias/${var.client}-${var.environment}-redis-kms-key"
  target_key_id = aws_kms_key.redis_key.id
}

resource "aws_elasticache_serverless_cache" "redis" {
  engine = "redis"
  name   = "${var.client}-${var.environment}-redis"
  cache_usage_limits {
    data_storage {
      maximum = 10
      unit    = "GB"
    }
    ecpu_per_second {
      maximum = 5000
    }
  }
  description              = "${var.client} Redis Server"
  kms_key_id               = aws_kms_key.redis_key.id
  major_engine_version     = "7"
  snapshot_retention_limit = 1
  security_group_ids       = [aws_security_group.redis_sg.id]
  subnet_ids               = aws_subnet.private.*.id
  depends_on = [aws_kms_key.redis_key]

  lifecycle {
    ignore_changes = [kms_key_id]
  }
}

Steps to Reproduce

terraform init terraform fmt & terraform validate terraform plan terraform apply

Debug Output

No response

Panic Output

│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to aws_elasticache_serverless_cache.redis, provider "provider[\"registry.terraform.io/hashicorp/aws\"]" produced an unexpected
│ new value: .kms_key_id: was cty.StringVal("25086aeb-503e-46c1-aed4-cc4b67f3ca60"), but now
│ cty.StringVal("arn:aws:kms:us-east-1:299186857980:key/25086aeb-503e-46c1-aed4-cc4b67f3ca60").
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 2 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 2 months ago

Similar #35285

@bsantacruz-code 👋 Thank you for taking the time to raise this! I noticed a comment on a similar issue that has a workaround suggestion, in case you want to try that while this issue awaits prioritization.