Skip to content

Commit f12ac95

Browse files
fix: Creation of an Instance with a Capacity Reservation ID (#278)
* fix bug for capacity reservation id assignment * addition of test case * revert of outputs in complete test case * fix formatting and tfdocs * fix formatting and tfdocs * run of pre-commit hooks * fix README * fix README * fix example to be deployable * fix example to be deployable * Update examples/complete/main.tf * Update examples/complete/main.tf Co-authored-by: Samuel CHNIBER <schniber@amazon.fr> Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com>
1 parent c0f8453 commit f12ac95

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

examples/complete/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Note that this example may create resources which can cost money. Run `terraform
3232

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

4849
| Name | Type |
4950
|------|------|
51+
| [aws_ec2_capacity_reservation.targeted](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_capacity_reservation) | resource |
5052
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
5153
| [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |
5254
| [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource |

examples/complete/main.tf

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ resource "aws_network_interface" "this" {
7373
subnet_id = element(module.vpc.private_subnets, 0)
7474
}
7575

76-
################################################################################
77-
# EC2 Module
78-
################################################################################
76+
resource "aws_ec2_capacity_reservation" "targeted" {
77+
instance_type = "t3.micro"
78+
instance_platform = "Linux/UNIX"
79+
availability_zone = "${local.region}a"
80+
instance_count = 1
81+
instance_match_criteria = "targeted"
82+
}
83+
84+
# ################################################################################
85+
# # EC2 Module
86+
# ################################################################################
7987

8088
module "ec2_disabled" {
8189
source = "../../"
@@ -328,3 +336,27 @@ module "ec2_spot_instance" {
328336

329337
tags = local.tags
330338
}
339+
340+
################################################################################
341+
# EC2 Module - Capacity Reservation
342+
################################################################################
343+
344+
module "ec2_capacity_reservation" {
345+
source = "../../"
346+
347+
name = "${local.name}-capacity-reservation"
348+
349+
ami = data.aws_ami.amazon_linux.id
350+
instance_type = "t3.micro"
351+
subnet_id = element(module.vpc.private_subnets, 0)
352+
vpc_security_group_ids = [module.security_group.security_group_id]
353+
associate_public_ip_address = true
354+
355+
capacity_reservation_specification = {
356+
capacity_reservation_target = {
357+
capacity_reservation_id = aws_ec2_capacity_reservation.targeted.id
358+
}
359+
}
360+
361+
tags = local.tags
362+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ resource "aws_instance" "this" {
4242
dynamic "capacity_reservation_target" {
4343
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
4444
content {
45-
capacity_reservation_id = lookup(capacity_reservation_target.value, "capacity_reservation_id", null)
45+
capacity_reservation_id = lookup(capacity_reservation_target, "capacity_reservation_id", null)
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)