Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 135145e

Browse files
fix: Correct capacity reservation target (#288)
1 parent dbf16a2 commit 135145e

File tree

3 files changed

+81
-89
lines changed

3 files changed

+81
-89
lines changed

‎.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.71.0
3+
rev: v1.74.1
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each
@@ -24,7 +24,7 @@ repos:
2424
- '--args=--only=terraform_standard_module_structure'
2525
- '--args=--only=terraform_workspace_remote'
2626
- repo: https://github.com/pre-commit/pre-commit-hooks
27-
rev: v4.2.0
27+
rev: v4.3.0
2828
hooks:
2929
- id: check-merge-conflict
3030
- id: end-of-file-fixer

‎examples/complete/main.tf

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,9 @@ locals {
1818
}
1919

2020
################################################################################
21-
# Supporting Resources
21+
# EC2 Module
2222
################################################################################
2323

24-
module "vpc" {
25-
source = "terraform-aws-modules/vpc/aws"
26-
version = "~> 3.0"
27-
28-
name = local.name
29-
cidr = "10.99.0.0/18"
30-
31-
azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
32-
public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
33-
private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
34-
database_subnets = ["10.99.7.0/24", "10.99.8.0/24", "10.99.9.0/24"]
35-
36-
tags = local.tags
37-
}
38-
39-
data "aws_ami" "amazon_linux" {
40-
most_recent = true
41-
owners = ["amazon"]
42-
43-
filter {
44-
name = "name"
45-
values = ["amzn-ami-hvm-*-x86_64-gp2"]
46-
}
47-
}
48-
49-
module "security_group" {
50-
source = "terraform-aws-modules/security-group/aws"
51-
version = "~> 4.0"
52-
53-
name = local.name
54-
description = "Security group for example usage with EC2 instance"
55-
vpc_id = module.vpc.vpc_id
56-
57-
ingress_cidr_blocks = ["0.0.0.0/0"]
58-
ingress_rules = ["http-80-tcp", "all-icmp"]
59-
egress_rules = ["all-all"]
60-
61-
tags = local.tags
62-
}
63-
64-
resource "aws_placement_group" "web" {
65-
name = local.name
66-
strategy = "cluster"
67-
}
68-
69-
resource "aws_kms_key" "this" {
70-
}
71-
72-
resource "aws_network_interface" "this" {
73-
subnet_id = element(module.vpc.private_subnets, 0)
74-
}
75-
76-
resource "aws_ec2_capacity_reservation" "open" {
77-
instance_type = "t3.micro"
78-
instance_platform = "Linux/UNIX"
79-
availability_zone = "${local.region}a"
80-
instance_count = 1
81-
instance_match_criteria = "open"
82-
}
83-
84-
resource "aws_ec2_capacity_reservation" "targeted" {
85-
instance_type = "t3.micro"
86-
instance_platform = "Linux/UNIX"
87-
availability_zone = "${local.region}a"
88-
instance_count = 1
89-
instance_match_criteria = "targeted"
90-
}
91-
92-
# # ################################################################################
93-
# # # EC2 Module
94-
# # ################################################################################
95-
9624
module "ec2_disabled" {
9725
source = "../../"
9826

@@ -123,10 +51,6 @@ module "ec2_complete" {
12351
cpu_core_count = 2 # default 4
12452
cpu_threads_per_core = 1 # default 2
12553

126-
capacity_reservation_specification = {
127-
capacity_reservation_preference = "open"
128-
}
129-
13054
enable_volume_tags = false
13155
root_block_device = [
13256
{
@@ -295,15 +219,13 @@ module "ec2_spot_instance" {
295219
create_spot_instance = true
296220

297221
ami = data.aws_ami.amazon_linux.id
298-
instance_type = "c4.4xlarge"
299222
availability_zone = element(module.vpc.azs, 0)
300223
subnet_id = element(module.vpc.private_subnets, 0)
301224
vpc_security_group_ids = [module.security_group.security_group_id]
302-
placement_group = aws_placement_group.web.id
303225
associate_public_ip_address = true
304226

305227
# Spot request specific attributes
306-
spot_price = "0.60"
228+
spot_price = "0.1"
307229
spot_wait_for_fulfillment = true
308230
spot_type = "persistent"
309231
spot_instance_interruption_behavior = "terminate"
@@ -314,9 +236,6 @@ module "ec2_spot_instance" {
314236
cpu_core_count = 2 # default 4
315237
cpu_threads_per_core = 1 # default 2
316238

317-
capacity_reservation_specification = {
318-
capacity_reservation_preference = "open"
319-
}
320239

321240
enable_volume_tags = false
322241
root_block_device = [
@@ -388,3 +307,75 @@ module "ec2_targeted_capacity_reservation" {
388307

389308
tags = local.tags
390309
}
310+
311+
################################################################################
312+
# Supporting Resources
313+
################################################################################
314+
315+
module "vpc" {
316+
source = "terraform-aws-modules/vpc/aws"
317+
version = "~> 3.0"
318+
319+
name = local.name
320+
cidr = "10.99.0.0/18"
321+
322+
azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
323+
public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
324+
private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
325+
database_subnets = ["10.99.7.0/24", "10.99.8.0/24", "10.99.9.0/24"]
326+
327+
tags = local.tags
328+
}
329+
330+
data "aws_ami" "amazon_linux" {
331+
most_recent = true
332+
owners = ["amazon"]
333+
334+
filter {
335+
name = "name"
336+
values = ["amzn-ami-hvm-*-x86_64-gp2"]
337+
}
338+
}
339+
340+
module "security_group" {
341+
source = "terraform-aws-modules/security-group/aws"
342+
version = "~> 4.0"
343+
344+
name = local.name
345+
description = "Security group for example usage with EC2 instance"
346+
vpc_id = module.vpc.vpc_id
347+
348+
ingress_cidr_blocks = ["0.0.0.0/0"]
349+
ingress_rules = ["http-80-tcp", "all-icmp"]
350+
egress_rules = ["all-all"]
351+
352+
tags = local.tags
353+
}
354+
355+
resource "aws_placement_group" "web" {
356+
name = local.name
357+
strategy = "cluster"
358+
}
359+
360+
resource "aws_kms_key" "this" {
361+
}
362+
363+
resource "aws_network_interface" "this" {
364+
subnet_id = element(module.vpc.private_subnets, 0)
365+
}
366+
367+
resource "aws_ec2_capacity_reservation" "open" {
368+
instance_type = "t3.micro"
369+
instance_platform = "Linux/UNIX"
370+
availability_zone = "${local.region}a"
371+
instance_count = 1
372+
instance_match_criteria = "open"
373+
}
374+
375+
resource "aws_ec2_capacity_reservation" "targeted" {
376+
instance_type = "t3.micro"
377+
instance_platform = "Linux/UNIX"
378+
availability_zone = "${local.region}a"
379+
instance_count = 1
380+
instance_match_criteria = "targeted"
381+
}

‎main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ resource "aws_instance" "this" {
3535
ebs_optimized = var.ebs_optimized
3636

3737
dynamic "capacity_reservation_specification" {
38-
for_each = var.capacity_reservation_specification!=null ? [var.capacity_reservation_specification] : []
38+
for_each = length(var.capacity_reservation_specification) >0 ? [var.capacity_reservation_specification] : []
3939
content {
40-
capacity_reservation_preference = lookup(capacity_reservation_specification.value, "capacity_reservation_preference", null)
40+
capacity_reservation_preference = try(capacity_reservation_specification.value.capacity_reservation_preference, null)
4141

4242
dynamic "capacity_reservation_target" {
43-
for_each = lookup(capacity_reservation_specification.value, "capacity_reservation_target", [])
43+
for_each = try([capacity_reservation_specification.value.capacity_reservation_target], [])
4444
content {
45-
capacity_reservation_id = lookup(capacity_reservation_specification.value.capacity_reservation_target, "capacity_reservation_id", null)
45+
capacity_reservation_id = try(capacity_reservation_target.value.capacity_reservation_id, null)
46+
capacity_reservation_resource_group_arn = try(capacity_reservation_target.value.capacity_reservation_resource_group_arn, null)
4647
}
4748
}
4849
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /