Commit 7f0a0ae6 authored by Samuel CHNIBER's avatar Samuel CHNIBER Committed by GitHub

fix: Assignment of the Capacity Reservation id to an instance (#282)

Co-authored-by: default avatarSamuel CHNIBER <schniber@amazon.fr>
parent b3d6f511
......@@ -32,15 +32,16 @@ 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 |
| <a name="module_ec2_multiple"></a> [ec2\_multiple](#module\_ec2\_multiple) | ../../ | n/a |
| <a name="module_ec2_network_interface"></a> [ec2\_network\_interface](#module\_ec2\_network\_interface) | ../../ | n/a |
| <a name="module_ec2_open_capacity_reservation"></a> [ec2\_open\_capacity\_reservation](#module\_ec2\_open\_capacity\_reservation) | ../../ | n/a |
| <a name="module_ec2_spot_instance"></a> [ec2\_spot\_instance](#module\_ec2\_spot\_instance) | ../../ | n/a |
| <a name="module_ec2_t2_unlimited"></a> [ec2\_t2\_unlimited](#module\_ec2\_t2\_unlimited) | ../../ | n/a |
| <a name="module_ec2_t3_unlimited"></a> [ec2\_t3\_unlimited](#module\_ec2\_t3\_unlimited) | ../../ | n/a |
| <a name="module_ec2_targeted_capacity_reservation"></a> [ec2\_targeted\_capacity\_reservation](#module\_ec2\_targeted\_capacity\_reservation) | ../../ | n/a |
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
| <a name="module_vpc"></a> [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 |
......
......@@ -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 = {
......
......@@ -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)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment