Skip to content

Commit a053478

Browse files
authored
feat: Expose autoscaler VPC config to the root module (#160)
* feat: Expose autoscaler VPC config to the root module * Add docs
1 parent 791982a commit a053478

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

.spacelift/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: 2
2-
module_version: 4.2.0
2+
module_version: 4.3.0
33

44
tests:
55
- name: AMD64-based workerpool

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ This module is also available [on the OpenTofu registry](https://search.opentofu
237237
| `spacelift_api_credentials` | Spacelift API credentials used to authenticate the autoscaler and lifecycle manager with Spacelift. See definition for full details. | `object`<br/>(See definition) | `null` | [variables.tf:304-318](./variables.tf#L304-L318) |
238238
| `instance_refresh` | If this block is configured, start an Instance Refresh when this Auto Scaling Group is updated | `any` | `{}` | [variables.tf:217-221](./variables.tf#L217-L221) |
239239
| `instance_market_options` | The market (purchasing) option for the instance | `any` | `{}` | [variables.tf:223-227](./variables.tf#L223-L227) |
240+
| `autoscaling_vpc_sg_ids` | Security groups that should be assigned to autoscaling lambda | `null` | `[]` | [variables.tf:223-227](./variables.tf#L272-L276) |
241+
| `autoscaling_vpc_subnets` | Subnets that should be assigned to autoscaling lambda | `null` | `[]` | [variables.tf:223-227](./variables.tf#L278-L82) |
240242

241243
### Self-hosted Configuration
242244

autoscaler/autoscaler.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ resource "aws_lambda_function" "autoscaler" {
4747
timeout = var.autoscaling_configuration.timeout != null ? var.autoscaling_configuration.timeout : 30
4848

4949
dynamic "vpc_config" {
50-
for_each = var.subnet_ids != null && var.security_group_ids != null ? ["USE_VPC_CONFIG"] : []
50+
for_each = var.spacelift_vpc_subnet_ids != null && var.spacelift_vpc_security_group_ids != null ? ["USE_VPC_CONFIG"] : []
5151
content {
52-
security_group_ids = var.security_group_ids
53-
subnet_ids = var.subnet_ids
52+
security_group_ids = var.spacelift_vpc_security_group_ids
53+
subnet_ids = var.spacelift_vpc_subnet_ids
5454
ipv6_allowed_for_dual_stack = var.ipv6_allowed_for_dual_stack
5555
}
5656
}

autoscaler/iam.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ data "aws_iam_policy_document" "autoscaler" {
4545
resources = ["*"]
4646
}
4747

48+
# Allow the Lambda to take actions on NetworkInterfaces
49+
statement {
50+
effect = "Allow"
51+
actions = [
52+
"ec2:DescribeNetworkInterfaces",
53+
"ec2:CreateNetworkInterface",
54+
"ec2:DeleteNetworkInterface",
55+
"ec2:DescribeInstances",
56+
"ec2:AttachNetworkInterface"
57+
]
58+
resources = ["*"]
59+
}
60+
4861
# Allow the Lambda to read the secret from SSM Parameter Store.
4962
statement {
5063
effect = "Allow"

autoscaler/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ variable "api_key_ssm_parameter_arn" {
7474
type = string
7575
}
7676

77-
variable "subnet_ids" {
77+
variable "spacelift_vpc_subnet_ids" {
7878
description = "List of subnet IDs when the lambda function should run in a VPC."
7979
type = list(string)
8080
default = null
8181
}
8282

83-
variable "security_group_ids" {
83+
variable "spacelift_vpc_security_group_ids" {
8484
description = "List of security group IDs when the lambda function should run in a VPC."
8585
type = list(string)
8686
default = null

examples/autoscaler/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ module "this" {
4949
vpc_subnets = data.aws_subnets.this.ids
5050
worker_pool_id = var.worker_pool_id
5151

52+
# Autoscaler VPC configuration
53+
autoscaling_vpc_sg_ids = [data.aws_security_group.this.id]
54+
autoscaling_vpc_subnets = data.aws_subnets.this.ids
55+
5256
autoscaling_configuration = {
5357
max_create = 5
5458
max_terminate = 5

main.tf

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ module "autoscaler" {
2323
count = local.autoscaling_enabled ? 1 : 0
2424
source = "./autoscaler"
2525

26-
additional_tags = var.additional_tags
27-
api_key_ssm_parameter_arn = local.ssm_arn
28-
api_key_ssm_parameter_name = local.ssm_name
29-
auto_scaling_group_arn = module.asg.autoscaling_group_arn
30-
autoscaling_configuration = var.autoscaling_configuration
31-
aws_partition_dns_suffix = data.aws_partition.current.dns_suffix
32-
aws_region = data.aws_region.this.name
33-
base_name = local.base_name
34-
cloudwatch_log_group_retention = var.cloudwatch_log_group_retention
35-
spacelift_api_credentials = var.spacelift_api_credentials
36-
iam_permissions_boundary = var.iam_permissions_boundary
37-
worker_pool_id = var.worker_pool_id
26+
additional_tags = var.additional_tags
27+
api_key_ssm_parameter_arn = local.ssm_arn
28+
api_key_ssm_parameter_name = local.ssm_name
29+
auto_scaling_group_arn = module.asg.autoscaling_group_arn
30+
autoscaling_configuration = var.autoscaling_configuration
31+
aws_partition_dns_suffix = data.aws_partition.current.dns_suffix
32+
aws_region = data.aws_region.this.name
33+
base_name = local.base_name
34+
cloudwatch_log_group_retention = var.cloudwatch_log_group_retention
35+
spacelift_api_credentials = var.spacelift_api_credentials
36+
iam_permissions_boundary = var.iam_permissions_boundary
37+
worker_pool_id = var.worker_pool_id
38+
spacelift_vpc_subnet_ids = var.autoscaling_vpc_subnets
39+
spacelift_vpc_security_group_ids = var.autoscaling_vpc_sg_ids
3840
}
3941

4042
module "lifecycle_manager" {

variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ variable "autoscaling_configuration" {
269269
default = null
270270
}
271271

272+
variable "autoscaling_vpc_subnets" {
273+
description = "List of VPC subnets to use for the autoscaler Lambda function."
274+
type = list(string)
275+
default = null
276+
}
277+
278+
variable "autoscaling_vpc_sg_ids" {
279+
description = "values of the security group to use for the autoscaler Lambda function."
280+
type = list(string)
281+
default = null
282+
}
283+
272284
variable "selfhosted_configuration" {
273285
description = <<EOF
274286
Configuration for selfhosted launcher. Configuration options are:
@@ -321,4 +333,4 @@ variable "cloudwatch_log_group_retention" {
321333
description = "Retention period for the autoscaler and lifecycle manager cloudwatch log group."
322334
type = number
323335
default = 7
324-
}
336+
}

0 commit comments

Comments
 (0)