terraform-aws-modules / terraform-aws-autoscaling

Terraform module to create AWS Auto Scaling resources πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws
Apache License 2.0
289 stars 553 forks source link

License specifications - Reference to undeclared resource #195

Closed sc250024 closed 2 years ago

sc250024 commented 2 years ago

Description

I cannot use the license_specifications block of this module, and receive an error that says Reference to undeclared resource. It could be that I'm using the wrong format for the input. If I am, then maybe I'll reuse this code to provide an example in the examples directory.

Versions

Reproduction Code [Required]

Steps to reproduce the behavior:

Using a clean copy of this repository as a basis, do the following:

  1. Checkout v5.2.0 of the module (git checkout v5.2.0)
  2. Go into the examples directory, and create a new folder called license
  3. Copy the main.tf and versions.tf files from the collapsible sections below, and place them in the license folder.
  4. Run terraform init, and then terraform plan.
  5. Observe the same error as from the above description.
main.tf ```hcl provider "aws" { region = local.region default_tags { tags = { Project = "terraform-aws-autoscaling" } } # Make it faster by skipping something skip_get_ec2_platforms = true skip_metadata_api_check = true skip_region_validation = true skip_credentials_validation = true skip_requesting_account_id = true } locals { name = "ex-asg-complete" region = "us-east-1" tags = { Owner = "user" Environment = "dev" } user_data = <<-EOT #!/bin/bash echo "Hello Terraform!" EOT } ################################################################################ # Supporting Resources ################################################################################ module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 3.0" name = local.name cidr = "10.99.0.0/18" azs = ["${local.region}a", "${local.region}b", "${local.region}c"] public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"] private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"] enable_dns_hostnames = true enable_dns_support = true tags = local.tags } data "aws_ami" "amazon_linux" { most_recent = true owners = ["amazon"] filter { name = "name" values = [ "amzn-ami-hvm-*-x86_64-gp2", ] } } ################################################################################ # Launch template only ################################################################################ resource "aws_licensemanager_license_configuration" "test" { license_count = 1 license_count_hard_limit = true license_counting_type = "Socket" name = "test-license" license_rules = [ "#allowedTenancy=EC2-DedicatedHost", "#maximumSockets=2", "#minimumSockets=2", ] } module "launch_template_only" { source = "../../" create = false desired_capacity = 1 image_id = data.aws_ami.amazon_linux.id instance_type = "t3.micro" max_size = 1 min_size = 0 name = "launch-template-only-${local.name}" tags = local.tags vpc_zone_identifier = module.vpc.private_subnets license_specifications = { license_configuration_arn = aws_licensemanager_license_configuration.test.arn } } ```
versions.tf ```hcl terraform { required_version = ">= 0.13.1" required_providers { aws = { source = "hashicorp/aws" version = ">= 3.72" } } } ```

Expected behavior

I am following the pattern of several other inputs of this module. I would expect that license_specifications accepts a mapping like the following:

license_specifications = {
  license_configuration_arn = aws_licensemanager_license_configuration.test.arn
}

I could be wrong though.

Actual behavior

The actual error that appears is the following:

β•·
β”‚ Error: Reference to undeclared resource
β”‚
β”‚   on .terraform/modules/my_asg_module/main.tf line 152, in resource "aws_launch_template" "this":
β”‚  152:       license_configuration_arn = license_specifications.value.license_configuration_arn
β”‚
β”‚ A managed resource "license_specifications" "value" has not been declared in module.my_asg_module.
β•΅

Terminal Output Screenshot(s)

This basically shows the same as the error above.

Screen Shot 2022-06-21 at 14 37 53

Additional context

If this issue leads to a fix version, would it be possible to cut a version v5.3.0 version of this module to support the AWS provider 3.x version in addition to the normal v6.6.0 version please?

sc250024 commented 2 years ago

As a followup, I think I found the issue:

https://github.com/terraform-aws-modules/terraform-aws-autoscaling/blob/v5.2.0/main.tf#L149-L154

Basically, the underlying aws_launch_template resource uses the key license_specification. In the code above, the dynamic block is correctly named license_specification. However, in line https://github.com/terraform-aws-modules/terraform-aws-autoscaling/blob/v5.2.0/main.tf#L152, the license_configuration_arn key points to license_specifications.value.license_configuration_arn (Notice the extra s).

The fix would be:

  dynamic "license_specification" {
    for_each = var.license_specifications != null ? [var.license_specifications] : []
    content {
-      license_configuration_arn = license_specifications.value.license_configuration_arn
+      license_configuration_arn = license_specification.value.license_configuration_arn
    }
  }
antonbabenko commented 2 years ago

This issue has been resolved in version 6.5.1 :tada:

github-actions[bot] commented 1 year ago

I'm going to lock this issue 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 similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.