terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
758 stars 1.88k forks source link

fix: Add cpu_credits support for all burstable instance types #294

Closed tofupup closed 2 years ago

tofupup commented 2 years ago

Description

Add a burstable_types data source, and modify the local.is_t_instance_type check to check against this data source, instead of a manually updated list of instance types. This allows the cpu_credits setting to be set for all instance types that support the setting.

Motivation and Context

Current instance type t4g supports burstable CPU credit settings, and it seems likely AWS will add more instance types like this in the future (t5*, etc). Using a data source for this, while it adds a small startup penalty while the data source is queried, will automatically these future changes, as well as the existing instance types.

Fixes #293

Breaking Changes

This should not break backwards compatibility, as existing instance types that support the cpu_credits are supported by the data source.

How Has This Been Tested?

Existing t3 test:

❯ terraform plan -target=module.ec2_t3_unlimited | grep -A2 credit_specification
      + credit_specification {
          + cpu_credits = "unlimited"
        }
❯ terraform apply -target=module.ec2_t3_unlimited
ec2_t3_unlimited_id = "i-01525b74f750093e6"
❯ aws --region eu-west-1 ec2 describe-instance-credit-specifications --instance-ids i-01525b74f750093e6
{
    "InstanceCreditSpecifications": [
        {
            "InstanceId": "i-01525b74f750093e6",
            "CpuCredits": "unlimited"
        }
    ]
}

New t4g test:

❯ terraform plan -target=module.ec2_t4g_standard | grep -A2 credit_specification
      + credit_specification {
          + cpu_credits = "standard"
        }
❯ terraform apply -target=module.ec2_t4g_standard
ec2_t4g_standard_id = "i-092e7613313e6c631"
❯ aws --region eu-west-1 ec2 describe-instance-credit-specifications --instance-ids i-092e7613313e6c631
{
    "InstanceCreditSpecifications": [
        {
            "InstanceId": "i-092e7613313e6c631",
            "CpuCredits": "standard"
        }
    ]
}

Create instances, 1 x t3 standard, 1 x t3 unlimited, 1 x t4g standard, 1 x t4g unlimited, 1 x a1 (settings as standard but this instance doesn't support bursting)

Code ```terraform data "aws_ssm_parameter" "ubuntu-jammy-arm64" { name = "/aws/service/canonical/ubuntu/server/22.04/stable/current/arm64/hvm/ebs-gp2/ami-id" } data "aws_ssm_parameter" "ubuntu-jammy-amd64" { name = "/aws/service/canonical/ubuntu/server/22.04/stable/current/amd64/hvm/ebs-gp2/ami-id" } module "t3_standard_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "4.1.4" name = "t3-credit-standard" ami = data.aws_ssm_parameter.ubuntu-jammy-amd64.value instance_type = "t3.micro" monitoring = false cpu_credits = "standard" tags = { Credit = "standard" InstanceType = "t3.micro" } } module "t3_unlimited_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "4.1.4" name = "t3-credit-unlimited" ami = data.aws_ssm_parameter.ubuntu-jammy-amd64.value instance_type = "t3.micro" monitoring = false cpu_credits = "unlimited" tags = { Credit = "unlimited" InstanceType = "t3.micro" } } module "t4g_standard_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "4.1.4" name = "t4g-credit-standard" ami = data.aws_ssm_parameter.ubuntu-jammy-arm64.value instance_type = "t4g.small" monitoring = false cpu_credits = "standard" tags = { Credit = "standard" InstanceType = "t4g.small" } } module "t4g_unlimited_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "4.1.4" name = "t4g-credit-unlimited" ami = data.aws_ssm_parameter.ubuntu-jammy-arm64.value instance_type = "t4g.small" monitoring = false cpu_credits = "unlimited" tags = { Credit = "standard" InstanceType = "t4g.small" } } module "a1_standard_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "4.1.4" name = "a1-credit-standard" ami = data.aws_ssm_parameter.ubuntu-jammy-arm64.value instance_type = "a1.medium" monitoring = false cpu_credits = "standard" tags = { Credit = "standard" InstanceType = "a1.medium" } } output "t3_standard_id" { value = module.t3_standard_instance.id } output "t3_unlimited_id" { value = module.t3_unlimited_instance.id } output "t4g_standard_id" { value = module.t4g_standard_instance.id } output "t4g_unlimited_id" { value = module.t4g_unlimited_instance.id } output "a1_standard_id" { value = module.a1_standard_instance.id } ```
Run using current v4.1.4 module, shows t4g with unset credit_specification like a1 type, created instances using default burst specification (unlimited for t4g). t3 is set correctly ```bash ❯ terraform plan | egrep -A3 'credit_spec|tags_all' + tags_all = { + "Credit" = "standard" + "InstanceType" = "a1.medium" + "Name" = "a1-credit-standard" -- + credit_specification {} + ebs_block_device { + delete_on_termination = (known after apply) -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t3.micro" + "Name" = "t3-credit-standard" -- + credit_specification { + cpu_credits = "standard" } -- + tags_all = { + "Credit" = "unlimited" + "InstanceType" = "t3.micro" + "Name" = "t3-credit-unlimited" -- + credit_specification { + cpu_credits = "unlimited" } -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t4g.small" + "Name" = "t4g-credit-standard" -- + credit_specification {} + ebs_block_device { + delete_on_termination = (known after apply) -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t4g.small" + "Name" = "t4g-credit-unlimited" -- + credit_specification {} + ebs_block_device { + delete_on_termination = (known after apply) ❯ terraform apply a1_standard_id = "i-04497a01dd37628fd" t3_standard_id = "i-0b92ea1a73bf20e43" t3_unlimited_id = "i-0ae6e2f3afb84316c" t4g_standard_id = "i-0c4f1e7256d376190" t4g_unlimited_id = "i-03d861674e1260f90" # (ORDER BELOW: t3 standard, t3 unlimited, t4g standard, t4g unlimited, a1) ❯ aws ec2 describe-instance-credit-specifications --instance-ids i-0b92ea1a73bf20e43 i-0ae6e2f3afb84316c i-0c4f1e7256d376190 i-03d861674e1260f90 i-04497a01dd37628fd { "InstanceCreditSpecifications": [ { "InstanceId": "i-0b92ea1a73bf20e43", "CpuCredits": "standard" }, { "InstanceId": "i-0ae6e2f3afb84316c", "CpuCredits": "unlimited" }, { "InstanceId": "i-0c4f1e7256d376190", "CpuCredits": "unlimited" }, { "InstanceId": "i-03d861674e1260f90", "CpuCredits": "unlimited" }, { "InstanceId": "i-04497a01dd37628fd", "CpuCredits": "standard" } ] } ```
Replace module source in code to point to fork with changes implemented, shows t4g with set credit_specification, created instances using specified burst setting ```bash ❯ grep source main.tf source = "git::https://github.com/tofupup/terraform-aws-ec2-instance.git?ref=5cc414b1979774244087d51dd72a424e5ba1a5ae" ❯ terraform plan | egrep -A3 'credit_spec|tags_all' + tags_all = { + "Credit" = "standard" + "InstanceType" = "a1.medium" + "Name" = "a1-credit-standard" -- + credit_specification {} + ebs_block_device { + delete_on_termination = (known after apply) -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t3.micro" + "Name" = "t3-credit-standard" -- + credit_specification { + cpu_credits = "standard" } -- + tags_all = { + "Credit" = "unlimited" + "InstanceType" = "t3.micro" + "Name" = "t3-credit-unlimited" -- + credit_specification { + cpu_credits = "unlimited" } -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t4g.small" + "Name" = "t4g-credit-standard" -- + credit_specification { + cpu_credits = "standard" } -- + tags_all = { + "Credit" = "standard" + "InstanceType" = "t4g.small" + "Name" = "t4g-credit-unlimited" -- + credit_specification { + cpu_credits = "unlimited" } ❯ terraform apply a1_standard_id = "i-0a67bdc418058132d" t3_standard_id = "i-06097e933ea3e7a65" t3_unlimited_id = "i-028140121fa6f73ca" t4g_standard_id = "i-0db54f86efe99f19e" t4g_unlimited_id = "i-021c6e761b0ef47e1" # (ORDER BELOW: t3 standard, t3 unlimited, t4g standard, t4g unlimited, a1) ❯ aws ec2 describe-instance-credit-specifications --instance-ids i-06097e933ea3e7a65 i-028140121fa6f73ca i-0db54f86efe99f19e i-021c6e761b0ef47e1 i-0a67bdc418058132d { "InstanceCreditSpecifications": [ { "InstanceId": "i-06097e933ea3e7a65", "CpuCredits": "standard" }, { "InstanceId": "i-028140121fa6f73ca", "CpuCredits": "unlimited" }, { "InstanceId": "i-0db54f86efe99f19e", "CpuCredits": "standard" }, { "InstanceId": "i-021c6e761b0ef47e1", "CpuCredits": "unlimited" }, { "InstanceId": "i-0a67bdc418058132d", "CpuCredits": "standard" } ] } ```
github-actions[bot] commented 2 years ago

This PR has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this PR will be closed in 10 days

github-actions[bot] commented 2 years ago

This PR was automatically closed because of stale in 10 days

github-actions[bot] commented 2 years ago

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