diff --git a/examples/complete/README.md b/examples/complete/README.md
index 008f8307..327f765f 100644
--- a/examples/complete/README.md
+++ b/examples/complete/README.md
@@ -32,15 +32,16 @@ Note that this example may create resources which can cost money. Run `terraform
| Name | Source | Version |
|------|--------|---------|
-| [ec2\_capacity\_reservation](#module\_ec2\_capacity\_reservation) | ../../ | n/a |
| [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a |
| [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a |
| [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a |
| [ec2\_multiple](#module\_ec2\_multiple) | ../../ | n/a |
| [ec2\_network\_interface](#module\_ec2\_network\_interface) | ../../ | n/a |
+| [ec2\_open\_capacity\_reservation](#module\_ec2\_open\_capacity\_reservation) | ../../ | n/a |
| [ec2\_spot\_instance](#module\_ec2\_spot\_instance) | ../../ | n/a |
| [ec2\_t2\_unlimited](#module\_ec2\_t2\_unlimited) | ../../ | n/a |
| [ec2\_t3\_unlimited](#module\_ec2\_t3\_unlimited) | ../../ | n/a |
+| [ec2\_targeted\_capacity\_reservation](#module\_ec2\_targeted\_capacity\_reservation) | ../../ | n/a |
| [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
| [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
@@ -48,6 +49,7 @@ Note that this example may create resources which can cost money. Run `terraform
| Name | Type |
|------|------|
+| [aws_ec2_capacity_reservation.open](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource |
| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource |
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
| [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |
diff --git a/examples/complete/main.tf b/examples/complete/main.tf
index c2cd951a..338b45c1 100644
--- a/examples/complete/main.tf
+++ b/examples/complete/main.tf
@@ -73,6 +73,14 @@ resource "aws_network_interface" "this" {
subnet_id = element(module.vpc.private_subnets, 0)
}
+resource "aws_ec2_capacity_reservation" "open" {
+ instance_type = "t3.micro"
+ instance_platform = "Linux/UNIX"
+ availability_zone = "${local.region}a"
+ instance_count = 1
+ instance_match_criteria = "open"
+}
+
resource "aws_ec2_capacity_reservation" "targeted" {
instance_type = "t3.micro"
instance_platform = "Linux/UNIX"
@@ -81,9 +89,9 @@ resource "aws_ec2_capacity_reservation" "targeted" {
instance_match_criteria = "targeted"
}
-# ################################################################################
-# # EC2 Module
-# ################################################################################
+# # ################################################################################
+# # # EC2 Module
+# # ################################################################################
module "ec2_disabled" {
source = "../../"
@@ -341,16 +349,36 @@ module "ec2_spot_instance" {
# EC2 Module - Capacity Reservation
################################################################################
-module "ec2_capacity_reservation" {
+module "ec2_open_capacity_reservation" {
source = "../../"
- name = "${local.name}-capacity-reservation"
+ name = "${local.name}-open-capacity-reservation"
ami = data.aws_ami.amazon_linux.id
instance_type = "t3.micro"
subnet_id = element(module.vpc.private_subnets, 0)
vpc_security_group_ids = [module.security_group.security_group_id]
- associate_public_ip_address = true
+ associate_public_ip_address = false
+
+ capacity_reservation_specification = {
+ capacity_reservation_target = {
+ capacity_reservation_id = aws_ec2_capacity_reservation.open.id
+ }
+ }
+
+ tags = local.tags
+}
+
+module "ec2_targeted_capacity_reservation" {
+ source = "../../"
+
+ name = "${local.name}-targeted-capacity-reservation"
+
+ ami = data.aws_ami.amazon_linux.id
+ instance_type = "t3.micro"
+ subnet_id = element(module.vpc.private_subnets, 0)
+ vpc_security_group_ids = [module.security_group.security_group_id]
+ associate_public_ip_address = false
capacity_reservation_specification = {
capacity_reservation_target = {
diff --git a/main.tf b/main.tf
index 5d0ec624..5f244b8e 100644
--- a/main.tf
+++ b/main.tf
@@ -42,7 +42,7 @@ resource "aws_instance" "this" {
dynamic "capacity_reservation_target" {
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
content {
- capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
+ capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null)
}
}
}
@@ -182,14 +182,15 @@ resource "aws_spot_instance_request" "this" {
# End spot request specific attributes
dynamic "capacity_reservation_specification" {
- for_each = var.capacity_reservation_specification != null ? [var.capacity_reservation_specification] : []
+ for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []
content {
- capacity_reservation_preference = lookup(capacity_reservation_specification.value, "capacity_reservation_preference", null)
+ capacity_reservation_preference = try(capacity_reservation_specification.value.capacity_reservation_preference, null)
dynamic "capacity_reservation_target" {
- for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
+ for_each = try([capacity_reservation_specification.value.capacity_reservation_target], [])
content {
- capacity_reservation_id = lookup(capacity_reservation_target.value, "capacity_reservation_id", null)
+ capacity_reservation_id = try(capacity_reservation_target.value.capacity_reservation_id, null)
+ capacity_reservation_resource_group_arn = try(capacity_reservation_target.value.capacity_reservation_resource_group_arn, null)
}
}
}