Skip to content
2 changes: 2 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Note that this example may create resources which can cost money. Run `terraform

| Name | Source | Version |
|------|--------|---------|
| <a name="module_ec2_capacity_reservation"></a> [ec2\_capacity\_reservation](#module\_ec2\_capacity\_reservation) | ../../ | n/a |
| <a name="module_ec2_complete"></a> [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a |
| <a name="module_ec2_disabled"></a> [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a |
| <a name="module_ec2_metadata_options"></a> [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a |
Expand All @@ -47,6 +48,7 @@ Note that this example may create resources which can cost money. Run `terraform

| Name | Type |
|------|------|
| [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 |
| [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource |
Expand Down
38 changes: 35 additions & 3 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ resource "aws_network_interface" "this" {
subnet_id = element(module.vpc.private_subnets, 0)
}

################################################################################
# EC2 Module
################################################################################
resource "aws_ec2_capacity_reservation" "targeted" {
instance_type = "t3.micro"
instance_platform = "Linux/UNIX"
availability_zone = "${local.region}a"
instance_count = 1
instance_match_criteria = "targeted"
}

# ################################################################################
# # EC2 Module
# ################################################################################

module "ec2_disabled" {
source = "../../"
Expand Down Expand Up @@ -328,3 +336,27 @@ module "ec2_spot_instance" {

tags = local.tags
}

################################################################################
# EC2 Module - Capacity Reservation
################################################################################

module "ec2_capacity_reservation" {
source = "../../"

name = "${local.name}-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

capacity_reservation_specification = {
capacity_reservation_target = {
capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id
}
}

tags = local.tags
}
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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.value, "capacity_reservation_id", null)
capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
}
}
}
Expand Down