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.76k stars 9.11k forks source link

[feature] Add support for License Manager - Host Resource Group #25525

Open sc250024 opened 2 years ago

sc250024 commented 2 years ago

Community Note

Description

As best I can tell, while there is currently support for Resource Groups (via the aws_resourcegroups_group resource), there is no support for the (annoyingly very similarly named) host resource group which is located within the AWS License Manager scope.

This is a screenshot of that resource:

license-manager-host-resource-group

I discovered the lack of this resource while trying to configure an autoscaling group comprised of metal type instances, and realized that there is a fairly lengthy process to go through on the licensing interface in order to configure this. I mistakenly thought that passing in the ARN of a aws_resourcegroups_group into the aws_launch_template was what was needed, but this is not the case.

The initial desire for this ticket was to do something like the following article: https://devdosvid.blog/2021/10/24/auto-scaling-group-for-your-macos-ec2-instances-fleet/. I noticed that this needed resource did not yet exist, so I figured I'd create an issue to create it.

Following the current resource naming conventions, this new resource would be aws_licensemanager_host_resource_group.

New or Affected Resource(s)

Potential Terraform Configuration

Based on the current way to configure this resource via the console...

create-host-resource-group

...the Terraform resource could probably be configured like so:

resource "aws_licensemanager_host_resource_group" "example" {
  description = "Test host resource group used for dedicated metal hosts."
  name        = "test-host-resource-group"
  tags        = { "some-key" : "some-value" }

  license_configurations = [
    aws_licensemanager_license_configuration.license1.arn,
    aws_licensemanager_license_configuration.license2.arn,
  ]

  dedicated_host_management_settings {
    allocate_hosts_automatically = true
    recover_hosts_automatically  = false
    release_hosts_automatically  = true

    instance_familes = ["m5", "m5d"]
  }
}

resource "aws_launch_template" "foo" {
  name = "foo"

  ...

  placement {
    host_resource_group_arn = aws_licensemanager_host_resource_group.example.arn
    tenancy                 = "host"
  }
}
sc250024 commented 2 years ago

To anyone who reads a bit further, I checked into this, and from what I can tell, I think that the AWS API doesn't allow you to create this resource (host resource groups) directly. What's even more confusing is that AWS treats host resource groups and regular resource groups as sort of the same.

Here's an image showing this:

resource-group-discrepancy

Basically, host resource groups are treated as regular resource groups, but not the other way around.

This means that there are two places inside of the console to create resources that are almost the same. They both use the same ARN format. But if you try to pass in a regular resource group (on the right) to a launch template, it will fail.

This seems like an error in the way AWS is handling this type of resource.

Kitsune-Fox commented 1 year ago

Hi @sc250024,

I know this is like a year too late but I was looking at doing the same thing and the AWS provider does actually allow for this. It's created just like a RG but instead of using a resource group query, you use a service configuration.

So for instance, to set up the DRG for AWS Licence Manager, you use the configuration parameters you can find here: https://docs.aws.amazon.com/ARG/latest/userguide/about-slg.html#about-slg-types-resourcegroups-ec2-hostmanagement

There's a AWS blog that links to a repo with examples. https://aws.amazon.com/blogs/compute/implementing-autoscaling-for-ec2-mac-instances https://github.com/aws-samples/amazon-autoscaling-mac1metal-ec2-with-terraform

The TF would look like this:

resource "aws_licensemanager_license_configuration"  "license_config"{  
  name                     = "MyRequiredLicense"
  description              = "Pass through configuration for Host Resource Group"
  license_count            = 32
  license_count_hard_limit = false
  license_counting_type    = "Core"
}

resource "aws_resourcegroups_group" "aws_resourcegroups_licence_group" {
  name = "LicenceManagerResourceGroup"
  configuration {
    type = "AWS::EC2::HostManagement"
    parameters {
      name   = "allowed-host-based-license-configurations"
      values = [aws_licensemanager_license_configuration.license_config.arn]
    }
    parameters {
      name   = "auto-allocate-host"
      values = [true]
    }
    parameters {
      name   = "auto-release-host"
      values = [true]
    }
    parameters {
      name   = "auto-host-recovery"
      values = [true]
    }
    parameters {
      name   = "allowed-host-families"
      values = ["mac2"]
    }
  }
  configuration {
    type = "AWS::ResourceGroups::Generic"
    parameters {
      name   = "allowed-resource-types"
      values = ["AWS::EC2::Host"]
    }
    parameters {
      name   = "deletion-protection"
      values = ["UNLESS_EMPTY"]
    }
  }
}

Hope this helps someone.

Edit: It looks like you don't need to use the license config, so instead of this:

    parameters {
      name   = "allowed-host-based-license-configurations"
      values = [aws_licensemanager_license_configuration.license_config.arn]
    }

you can instead just use:

    parameters {
      name   = "any-host-based-license-configuration"
      values = [true]
    }