terraform-aws-ec2-instance icon indicating copy to clipboard operation
terraform-aws-ec2-instance copied to clipboard

Allow to specify the ID of a Capacity Reservation in which to run an instance - capacity_reservation_id

Open joelsdc opened this issue 3 years ago • 0 comments

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

Add option for setting capacity_reservation_id param in the aws_instance resource.

Expected Behavior

Launch an EC2 instance using a pre-existing Capacity Reservation

Describe Ideal Solution

# AWS Capacity Reservation
resource "aws_ec2_capacity_reservation" "win_g4dn_xlarge_usw2a" {
  instance_type     = "g4dn.xlarge"
  availability_zone = "us-west2-a"
  instance_platform = "Windows"
  instance_count    = 1
}

# AWS Instance
module "instance_from_capacity_reservation" {
  source = "cloudposse/ec2-instance/aws"
  version = "x.x.x"
  ...
  ...
  capacity_reservation_id = aws_ec2_capacity_reservation.win_g4dn_xlarge_usw2a.id
  ...
  ...
}

Additional Context

I'm not sure if the solution would be something as simple as adding:

  • variables.tf:
variable "capacity_reservation_id" {
  type        = string
  default     = null
  description = "The ID of the Capacity Reservation in which to run the instance"
}
  • main.tf:
resource "aws_instance" "default" {
  ...
  ...
  capacity_reservation_specification {
    capacity_reservation_target {
      capacity_reservation_id = var.capacity_reservation_id != null ? var.capacity_reservation_id : null
    }
  }
  ...
  ...
}

joelsdc avatar Mar 04 '22 21:03 joelsdc