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

feat: Add `availability_zone` output #347

Closed deny-7 closed 1 year ago

deny-7 commented 1 year ago

Description

Added availability_zone output. The availability_zone of the created instance is useful information to use outside of the module.

Motivation and Context

When creating multiple EC2 instances in multiple AZs, in most cases, we would like to have the EBS volume in the same AZ as the created EC2. As a trick we can use data.aws_instance with loop or a map of AZs and random_suffle, but it does not look nice. In my opinion having availability_zone output would get out of tricks and make the code cleaner and easier to read.

This PR resolves #341 and #264

Breaking Changes

None

How Has This Been Tested?

terraform plan --target module.ec2 --target aws_volume_attachment.this --target aws_ebs_volume.this

  # aws_ebs_volume.this will be created
  + resource "aws_ebs_volume" "this" {
      + arn               = (known after apply)
      + availability_zone = "eu-west-1c"
      + encrypted         = (known after apply)
      + final_snapshot    = false
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 1
      + snapshot_id       = (known after apply)
      + tags              = {
          + "Example"    = "ex-volume-attachment"
          + "Name"       = "ex-volume-attachment"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tags_all          = {
          + "Example"    = "ex-volume-attachment"
          + "Name"       = "ex-volume-attachment"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_volume_attachment.this will be created
  + resource "aws_volume_attachment" "this" {
      + device_name = "/dev/sdh"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

  # module.ec2.aws_instance.this[0] will be created
  + resource "aws_instance" "this" {
      + ami                                  = "ami-0c43c11b9446e99da"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = true
      + availability_zone                    = "eu-west-1c"
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_stop                     = (known after apply)
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + host_id                              = (known after apply)
      + host_resource_group_arn              = (known after apply)
      + iam_instance_profile                 = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_lifecycle                   = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "c5.large"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
      + placement_group                      = (known after apply)
      + placement_partition_number           = (known after apply)
      + primary_network_interface_id         = (known after apply)
      + private_dns                          = (known after apply)
      + private_ip                           = (known after apply)
      + public_dns                           = (known after apply)
      + public_ip                            = (known after apply)
      + secondary_private_ips                = (known after apply)
      + security_groups                      = (known after apply)
      + source_dest_check                    = true
      + spot_instance_request_id             = (known after apply)
      + subnet_id                            = "subnet-****"
      + tags                                 = {
          + "Example"    = "ex-volume-attachment"
          + "Name"       = "ex-volume-attachment"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tags_all                             = {
          + "Example"    = "ex-volume-attachment"
          + "Name"       = "ex-volume-attachment"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tenancy                              = (known after apply)
      + user_data                            = (known after apply)
      + user_data_base64                     = (known after apply)
      + user_data_replace_on_change          = false
      + volume_tags                          = {
          + "Name" = "ex-volume-attachment"
        }
      + vpc_security_group_ids               = (known after apply)

      + credit_specification {}

      + enclave_options {
          + enabled = (known after apply)
        }

      + metadata_options {
          + http_endpoint               = "enabled"
          + http_put_response_hop_limit = 1
          + http_tokens                 = "optional"
          + instance_metadata_tags      = (known after apply)
        }

      + timeouts {}
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + ec2_arn                                = (known after apply)
  + ec2_availability_zone                  = "eu-west-1c"
  + ec2_capacity_reservation_specification = (known after apply)
  + ec2_id                                 = (known after apply)
  + ec2_instance_state                     = (known after apply)
  + ec2_primary_network_interface_id       = (known after apply)
  + ec2_private_dns                        = (known after apply)
  + ec2_public_dns                         = (known after apply)
  + ec2_public_ip                          = (known after apply)
  + ec2_tags_all                           = {
      + Example    = "ex-volume-attachment"
      + Name       = "ex-volume-attachment"
      + Repository = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
    }
github-actions[bot] commented 1 year 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

antonbabenko commented 1 year ago

This PR is included in version 5.4.0 :tada:

github-actions[bot] commented 1 year 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.