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

ID is not getting assigned to bucket_lifecycle_configuration #96

Open
anemiroffsumer opened this issue May 20, 2024 · 1 comment
Open
Labels
invalid This doesn't seem right

Comments

@anemiroffsumer
Copy link

anemiroffsumer commented May 20, 2024

Describe the Bug

I have a main module that uses your module as a sub module , which is turn is passed to a template. There has been no alteration to your code.

When , I try to generate the plan I get 18 resources created:

│ Error: expected length of rule.0.id to be in the range (1 - 255), got 
│
│   with module.logging.module.cloudtrail-s3.module.cloudtrail_s3_bucket.module.s3_bucket.module.aws_s3_bucket.aws_s3_bucket_lifecycle_configuration.default[0],
│   on .terraform\modules\logging.cloudtrail-s3.cloudtrail_s3_bucket.s3_bucket.aws_s3_bucket\lifecycle.tf line 157, in resource "aws_s3_bucket_lifecycle_configuration" "default":
│  157: resource "aws_s3_bucket_lifecycle_configuration" "default" {

Expected Behavior

All resources are able to be created in the plan.

I also expect when I set the

variable "lifecycle_rule_enabled" {
  description = "Enable lifecycle events on this bucket"
  type        = bool
  default     = false
}

I would expect no lifecycle rules to be created and thus this error wouldn't even occur.

Steps to Reproduce

Module:

Main.tf

module "cloudtrail-s3" {
  source  = "cloudposse/cloudtrail-s3-bucket/aws//examples/complete"
  version = "0.26.4"
  region = "us-east-1" 
}
Click to reveal additional code

providers.tf

terraform {
  required_version = "~> 1.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.16.0"
    }
  }
}

variables.tf

variable "abort_incomplete_multipart_upload_days" {
  description = "Maximum time (in days) that you want to allow multipart uploads to remain in progress"
  type        = number
  default     = 5
}

variable "access_log_bucket_name" {
  description = "Name of the S3 bucket where s3 access log will be sent to"
  type        = string
  default     = ""
}

variable "acl" {
  description = "The canned ACL to apply. We recommend log-delivery-write for compatibility with AWS services"
  type        = string
  default     = "log-delivery-write"
}

variable "additional_tag_map" {
  description = "Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id."
  type        = map(string)
  default     = {}
}

variable "allow_ssl_requests_only" {
  description = "Set to true to require requests to use Secure Socket Layer (HTTPS/SSL). This will explicitly deny access to HTTP requests"
  type        = bool
  default     = true
}

variable "attributes" {
  description = "ID element. Additional attributes (e.g. workers or cluster) to add to id, in the order they appear in the list. New attributes are appended to the end of the list. The elements of the list are joined by the delimiter and treated as a single ID element."
  type        = list(string)
  default     = []
}

variable "block_public_acls" {
  description = "Set to false to disable the blocking of new public access lists on the bucket"
  type        = bool
  default     = true
}

variable "block_public_policy" {
  description = "Set to false to disable the blocking of new public policies on the bucket"
  type        = bool
  default     = true
}

variable "bucket_notifications_enabled" {
  description = "Send notifications for the object created events. Used for 3rd-party log collection from a bucket. This does not affect access log bucket created by this module. To enable bucket notifications on the access log bucket, create it separately using the cloudposse/s3-log-storage/aws"
  type        = bool
  default     = false
}

variable "bucket_notifications_prefix" {
  description = "Prefix filter. Used to manage object notifications"
  type        = string
  default     = ""
}

variable "bucket_notifications_type" {
  description = "Type of the notification configuration. Only SQS is supported."
  type        = string
  default     = "SQS"
}

variable "context" {
  description = "Single object for setting entire context at once. See description of individual variables for details. Leave string and numeric variables as null to use default value. Individual variable settings (non-null) override settings in context object, except for attributes, tags, and additional_tag_map, which are merged."
  type        = any
  default     = {
    additional_tag_map = {},
    attributes         = [],
    delimiter          = null,
    descriptor_formats = {},
    enabled            = true,
    environment        = null,
    id_length_limit    = null,
    label_key_case     = null,
    label_order        = [],
    label_value_case   = null,
    labels_as_tags     = ["unset"],
    name               = null,
    namespace          = null,
    regex_replace_chars = null,
    stage              = null,
    tags               = {},
    tenant             = null
  }
}

variable "create_access_log_bucket" {
  description = "A flag to indicate if a bucket for s3 access logs should be created"
  type        = bool
  default     = false
}

variable "delimiter" {
  description = "Delimiter to be used between ID elements. Defaults to - (hyphen). Set to \"\" to use no delimiter at all."
  type        = string
  default     = null
}

variable "descriptor_formats" {
  description = "Describe additional descriptors to be output in the descriptors output map. Map of maps. Keys are names of descriptors. Values are maps of the form { format = string labels = list(string) } (Type is any so the map values can later be enhanced to provide additional options.) format is a Terraform format string to be passed to the format() function. labels is a list of labels, in order, to pass to format() function. Label values will be normalized before being passed to format() so they will be identical to how they appear in id. Default is {} (descriptors output will be empty)."
  type        = any
  default     = {}
}

variable "enable_glacier_transition" {
  description = "Glacier transition might just increase your bill. Set to false to disable lifecycle transitions to AWS Glacier."
  type        = bool
  default     = false
}

variable "enabled" {
  description = "Set to false to prevent the module from creating any resources"
  type        = bool
  default     = null
}

variable "environment" {
  description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'"
  type        = string
  default     = null
}

variable "expiration_days" {
  description = "Number of days after which to expunge the objects"
  type        = number
  default     = 90
}

variable "force_destroy" {
  description = "(Optional, Default:false ) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable"
  type        = bool
  default     = false
}

variable "glacier_transition_days" {
  description = "Number of days after which to move the data to the glacier storage tier"
  type        = number
  default     = 60
}

variable "id_length_limit" {
  description = "Limit id to this many characters (minimum 6). Set to 0 for unlimited length. Set to null for keep the existing setting, which defaults to 0. Does not affect id_full."
  type        = number
  default     = null
}

variable "ignore_public_acls" {
  description = "Set to false to disable the ignoring of public access lists on the bucket"
  type        = bool
  default     = true
}

variable "kms_master_key_arn" {
  description = "The AWS KMS master key ARN used for the SSE-KMS encryption. This can only be used when you set the value of sse_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse_algorithm is aws:kms"
  type        = string
  default     = ""
}

variable "label_key_case" {
  description = "Controls the letter case of the tags keys (label names) for tags generated by this module. Does not affect keys of tags passed in via the tags input. Possible values: lower, title, upper. Default value: title."
  type        = string
  default     = null
}

variable "label_order" {
  description = "The order in which the labels (ID elements) appear in the id. Defaults to [\"namespace\", \"environment\", \"stage\", \"name\", \"attributes\"]. You can omit any of the 6 labels (\"tenant\" is the 6th), but at least one must be present."
  type        = list(string)
  default     = null
}

variable "label_value_case" {
  description = "Controls the letter case of ID elements (labels) as included in id, set as tag values, and output by this module individually. Does not affect values of tags passed in via the tags input. Possible values: lower, title, upper and none (no transformation). Set this to title and set delimiter to \"\" to yield Pascal Case IDs. Default value: lower."
  type        = string
  default     = null
}

variable "labels_as_tags" {
  description = "Set of labels (ID elements) to include as tags in the tags output. Default is to include all labels. Tags with empty values will not be included in the tags output. Set to [] to suppress all generated tags. Notes: The value of the name tag, if included, will be the id, not the name. Unlike other null-label inputs, the initial setting of labels_as_tags cannot be changed in later chained modules. Attempts to change it will be silently ignored."
  type        = set(string)
  default     = ["default"]
}

variable "lifecycle_prefix" {
  description = "Prefix filter. Used to manage object lifecycle events"
  type        = string
  default     = ""
}

variable "lifecycle_rule_enabled" {
  description = "Enable lifecycle events on this bucket"
  type        = bool
  default     = false
}

variable "lifecycle_tags" {
  description = "Tags filter. Used to manage object lifecycle events"
  type        = map(string)
  default     = {}
}

variable "name" {
  description = "ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. This is the only ID element not also included as a tag. The \"name\" tag is set to the full id string. There is no tag with the value of the name input."
  type        = string
  default     = null
}

variable "namespace" {
  description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
  type        = string
  default     = null
}

variable "noncurrent_version_expiration_days" {
  description = "Specifies when noncurrent object versions expire"
  type        = number
  default     = 90
}

variable "noncurrent_version_transition_days" {
  description = "Specifies when noncurrent object versions transitions"
  type        = number
  default     = 30
}

variable "policy" {
  description = "A valid bucket policy JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a terraform plan. In this case, please make sure you use the verbose/specific version of the policy"
  type        = string
  default     = ""
}

variable "regex_replace_chars" {
  description = "Terraform regular expression (regex) string. Characters matching the regex will be removed from the ID elements. If not set, \"/[^a-zA-Z0-9-]/\" is used to remove all characters other than hyphens, letters and digits."
  type        = string
  default     = null
}

variable "restrict_public_buckets" {
  description = "Set to false to disable the restricting of making the bucket public"
  type        = bool
  default     = true
}

variable "sse_algorithm" {
  description = "The server-side encryption algorithm to use. Valid values are AES256 and aws:kms"
  type        = string
  default     = "AES256"
}

variable "stage" {
  description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
  type        = string
  default     = null
}

variable "standard_transition_days" {
  description = "Number of days to persist in the standard storage tier before moving to the infrequent access tier"
  type        = number
  default     = 30
}

variable "tags" {
  description = "Additional tags (e.g. {'BusinessUnit': 'XYZ'}). Neither the tag keys nor the tag values will be modified by this module."
  type        = map(string)
  default     = {}
}

variable "tenant" {
  description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for"
  type        = string
  default     = null
}

variable "versioning_enabled" {
  description = "A state of versioning. Versioning is a means of keeping multiple variants of an object in the same bucket"
  type        = bool
  default     = false
}

Screenshots

image

Environment

Windows VSCode

terraform {
  required_version = "1.7.0"
}
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = " ~> 5.0"
    }
  }
}

Latest version of your module

Additional Context

I tried both in terraform and opentofu same error.

@anemiroffsumer anemiroffsumer added the bug 🐛 An issue with the system label May 20, 2024
@Nuru Nuru added invalid This doesn't seem right and removed bug 🐛 An issue with the system labels May 22, 2024
@Nuru
Copy link
Sponsor Contributor

Nuru commented May 22, 2024

@anemiroffsumer I am sorry you are having trouble with our module.

It appears you are new to Terraform and do not understand how modules work, or how our module repos are laid out. This code in your main.tf has a few problems:

module "cloudtrail-s3" {
  source  = "cloudposse/cloudtrail-s3-bucket/aws//examples/complete"
  version = "0.26.4"
  region = "us-east-1" 
}

The examples/complete in our repos is not meant to be used this way. It is meant to show you how to use the component by giving you an example main.tf and supporting files. It is not meant to be called directly.

The examples/complete/fixtures.us-east-2.tfvars file provides a minimal example of the inputs you can set, but those variables do not take effect when you call the module this way. You probably already noticed this, which is why you set the region.

Our modules make use of our null-label module to handle generating IDs. This module gives you some flexibility about how you construct IDs, but the short story is that while all the inputs are optional, you have to prove some kind of input. For people who do not want to take full advantage, we recommend just setting name to something. Your module works if you just add name = "test" to your module "cloudtrail-s3" invocation.

Third, the variables.tf you supplied defines inputs into your module. Those inputs are not automatically forwarded to the modules you call. The following works, but should not be considered a pattern to follow.

provider "aws" {
  region  = "us-east-1"
}

module "cloudtrail-s3" {
  source  = "cloudposse/cloudtrail-s3-bucket/aws//examples/complete"
  version = "0.26.4"
  name    = "cloudtrail-s3-bucket"

  lifecycle_rule_enabled = var.lifecycle_rule_enabled
}

variable "lifecycle_rule_enabled" {
  description = "Enable lifecycle events on this bucket"
  type        = bool
  default     = false
}

I hope this answers your questions, and if so, please close this issue. If not, please reach out to us in the #terraform channel of our Slack workspace for more help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants