Skip to content

Commit a661719

Browse files
committed
feat: add more options
1 parent 700ca8b commit a661719

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

main.tf

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
locals {
2+
aws_partition = data.aws_partition.current.partition
3+
24
vpc_dns_resolver = module.this.enabled ? cidrhost(data.aws_vpc.lookup[0].cidr_block, 2) : "10.0.0.2"
35
proxies = { for k, v in var.proxies : k => merge(v, { name = k }) }
46
proxies_port_range = [local.proxies.default.listener_port, local.proxies.default.listener_port]
@@ -27,6 +29,8 @@ locals {
2729
}
2830
}
2931

32+
data "aws_partition" "current" {}
33+
3034
# ================================================================== service ===
3135

3236
module "proxy" {
@@ -50,39 +54,27 @@ module "proxy" {
5054
}
5155

5256
iam_instance_profile_name = module.this.enabled ? resource.aws_iam_instance_profile.this[0].id : null
53-
key_name = ""
57+
key_name = var.key_name
5458
metadata_http_tokens_required = true
5559

5660
autoscaling_policies_enabled = false
5761
desired_capacity = local.capacity.desired
58-
min_size = var.capacity.min
59-
max_size = var.capacity.max
62+
min_size = local.capacity.min
63+
max_size = local.capacity.max
6064
max_instance_lifetime = "604800"
6165
wait_for_capacity_timeout = "300s"
6266
tag_specifications_resource_types = ["instance", "volume", "spot-instances-request"]
6367

6468
mixed_instances_policy = {
6569
instances_distribution = {
66-
on_demand_base_capacity = 0
67-
on_demand_percentage_above_base_capacity = 0
70+
on_demand_base_capacity = var.spot.enabled ? 0 : 100
71+
on_demand_percentage_above_base_capacity = var.spot.enabled ? 0 : 100
6872
on_demand_allocation_strategy = "prioritized"
69-
spot_allocation_strategy = "capacity-optimized"
73+
spot_allocation_strategy = var.spot.allocation_strategy
7074
spot_instance_pools = 0
7175
spot_max_price = ""
7276
}
73-
override = [{
74-
instance_type = "t3.nano"
75-
weighted_capacity = 1
76-
}, {
77-
instance_type = "t3a.nano"
78-
weighted_capacity = 1
79-
}, {
80-
instance_type = "t3.micro"
81-
weighted_capacity = 1
82-
}, {
83-
instance_type = "t3a.micro"
84-
weighted_capacity = 1
85-
}]
77+
override = [for x in var.instance_types : { instance_type = x.type, weighted_capacity = x.weight }]
8678
}
8779

8880
associate_public_ip_address = false
@@ -266,7 +258,7 @@ resource "aws_iam_role_policy_attachment" "ssm_managed_instance_core" {
266258
count = module.this.enabled ? 1 : 0
267259

268260
role = resource.aws_iam_role.this[0].name
269-
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
261+
policy_arn = "arn:${local.aws_partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
270262
}
271263

272264
resource "aws_iam_policy" "this" {
@@ -307,8 +299,8 @@ data "aws_iam_policy_document" "this" {
307299
"s3:GetBucketLocation",
308300
]
309301
resources = [
310-
"arn:aws:s3:::${var.ssm_sessions.logs_bucket_name}",
311-
"arn:aws:s3:::${var.ssm_sessions.logs_bucket_name}/*"
302+
"arn:${local.aws_partition}:s3:::${var.ssm_sessions.logs_bucket_name}",
303+
"arn:${local.aws_partition}:s3:::${var.ssm_sessions.logs_bucket_name}/*"
312304
]
313305
}
314306
}

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ variable "capacity" {
2727
default = {}
2828
}
2929

30+
variable "instance_types" {
31+
type = list(object({
32+
type = string
33+
weight = optional(number, 1)
34+
}))
35+
description = "List of instance types and their weighted capacity to be used."
36+
default = [{ type = "t3.nano" }, { type = "t3a.nano" }, { type = "t3.micro" }, { type = "t3a.micro" }]
37+
}
38+
39+
variable "spot" {
40+
type = object({
41+
enabled = optional(bool, true)
42+
allocation_strategy = optional(string, "capacity-optimized")
43+
})
44+
description = "Configuration of spot instances"
45+
default = {}
46+
}
47+
3048
variable "logs_bucket_name" {
3149
type = string
3250
description = "S3 bucket for storing logs."
@@ -44,6 +62,12 @@ variable "ssm_sessions" {
4462

4563
# --------------------------------------------------------------- networking ---
4664

65+
variable "key_name" {
66+
type = string
67+
description = "Name of existing SSH key to be assigned to instances."
68+
default = ""
69+
}
70+
4771
variable "public_accessible" {
4872
type = bool
4973
description = "Toggle whether the NLB is publicly accessible."

0 commit comments

Comments
 (0)