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]: Error: creating EC2 Spot Fleet Request: InvalidSpotFleetRequestConfig: The specified Subnet: subnet-xyz cannot be used with the specified Availability Zone: ca-central-1a. #30751

Open trent-abc opened 1 year ago

trent-abc commented 1 year ago

Terraform Core Version

v1.4.3-dev

AWS Provider Version

v4.62.0

Affected Resource(s)

aws_spot_fleet_request

Expected Behavior

Success

Actual Behavior

Failed provisioning

Relevant Error/Panic Output Snippet

╷
│ Error: creating EC2 Spot Fleet Request: InvalidSpotFleetRequestConfig: The specified Subnet: subnet-XYZ cannot be used with the specified Availability Zone: ca-central-1a.
│   status code: 400, request id: ABC-DEF-GHI-JKL
│
│   with aws_spot_fleet_request.jenkins_agent_fleet_requirements_based,
│   on fleet.tf line 165, in resource "aws_spot_fleet_request" "jenkins_agent_fleet_requirements_based":
│  165: resource "aws_spot_fleet_request" "jenkins_agent_fleet_requirements_based" {
│
╵

Terraform Configuration Files

resource "aws_launch_template" "jenkins_agent" {
…
  # instance_type = "t2.large"
  instance_requirements {
    allowed_instance_types = [
        "c5.xlarge",
        "c6i.2xlarge",
        "t2.large",
        "t3.large",
    ]
    memory_mib {
        max = 32768
        min = 8192
      }
    vcpu_count {
        max = 16
        min = 4
    }
  }
  network_interfaces {
    subnet_id = aws_subnet.ca-central-1a-private.id
  }
  placement {
    availability_zone = "ca-central-1a"
  }
…
}
resource "aws_spot_fleet_request" "jenkins_agent_fleet" {
  iam_fleet_role  = "arn:aws:iam::XXXXXXXXXX:role/aws-service-role/spotfleet.amazonaws.com/AWSServiceRoleForEC2SpotFleet"
  target_capacity = 0
  launch_template_config {
    launch_template_specification {
      id = aws_launch_template.jenkins_agent.id
      version = aws_launch_template.jenkins_agent.latest_version
    }
  }
}

Steps to Reproduce

terraform init terraform apply

Debug Output

Unnecessary

Panic Output

No response

Important Factoids

Ok, inside the aws_launch_template block I have parameter

instance_requirements

If this is set we get the error as described above. If instead I comment that block out and use:

instance_type = "t2.large"

The provisioning is successful.

Notice target_capacity = 0 in the aws_spot_fleet_request block, this is important, and I want a capacity of 0 because Jenkins is managing the fleet, provisioning additional nodes when it needs them. I tried to provision via AWS UI and import it later into the terraform config, the launch template was successful. but when I try to set 0 instances in the aws_spot_fleet_request I can-not launch it from the UI--the button is greyed out. I'm at a loss here, why does it work when I specify instance_type but I'm getting an availability zone error when I specify instance requirements?

This should be easy enough to reproduce, but just to be clear I'm trying to use ca-central-1 and my instances are provisioned on a private subnet.

References

https://github.com/hashicorp/terraform/issues/33040

Would you like to implement a fix?

No

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

algo7 commented 1 year ago

same issue but with aws_ec2_fleet resource with Terraform AWS Provider v5.5.0

v0devil commented 8 months ago

Same issue with aws version = ">=5.41.0"

prakashul commented 3 months ago

hi @trent-abc

Below snippet worked for me, with aws provider version 5.62.0:

resource "aws_spot_fleet_request" "spot_fleet_test" {
  iam_fleet_role                      = "<ROLE_ARN>"
  allocation_strategy                 = var.allocation_strategy #i used capacityOptimized
  target_capacity                     = 1
  terminate_instances_with_expiration = true
  wait_for_fulfillment                = true
  fleet_type                          = "request"
  target_capacity_unit_type           = "units"

  launch_template_config {
    launch_template_specification {
      id      = aws_launch_template.test_template.id
      version = aws_launch_template.test_template.latest_version
    }
    overrides {
      subnet_id         = "sub-xxxxxxxxxxxx"
      weighted_capacity = 0

      instance_requirements {
        memory_mib {
          max = 131072 # 128 Gb
          min = 8192
        }

        vcpu_count {
          max = 32
          min = 4
        }
      }
    }
  }
  timeouts {
    create = "20m"
  }
}

# Example Launch Template
resource "aws_launch_template" "test_template" {
  name     = "test-template"
  image_id = "ami-xxxxxxxxxxxx"
  key_name = var.key_name
  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
    instance_metadata_tags      = "enabled"
  }
  network_interfaces {
    associate_public_ip_address = false
    subnet_id                   = "sub-xxxxxxxxxxxx"
    security_groups             = ["sg-xxxxxxxxxxxx"]
  }
  tag_specifications {
    resource_type = "instance"
  }
}