Skip to content

Commit 63278db

Browse files
committed
Fix: Require AMI ID with preinstalled dependecies.
1 parent 97d2ad0 commit 63278db

File tree

9 files changed

+120
-131
lines changed

9 files changed

+120
-131
lines changed

stackguardian_private_runner/aws/ami.tf

Lines changed: 0 additions & 40 deletions
This file was deleted.

stackguardian_private_runner/aws/autoscaling.tf

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
locals {
2-
subnet_id = var.private_subnet_id != null ? var.private_subnet_id : var.public_subnet_id
3-
user_data_script = var.ami_id != "" ? "register_runner.sh" : "user_data_all.sh"
2+
subnet_id = var.private_subnet_id != null ? var.private_subnet_id : var.public_subnet_id
3+
4+
# SSH key logic: custom public key > named key > no key
5+
use_custom_key = var.ssh_public_key != ""
6+
use_named_key = var.ssh_key_name != "" && var.ssh_public_key == ""
7+
ssh_key_name = local.use_custom_key ? aws_key_pair.this[0].key_name : (local.use_named_key ? var.ssh_key_name : "")
48
}
59

610
data "stackguardian_runner_group_token" "this" {
711
runner_group_id = stackguardian_runner_group.this.resource_name
812
}
913

14+
# Create SSH key pair when custom public key is provided
15+
resource "aws_key_pair" "this" {
16+
count = local.use_custom_key ? 1 : 0
17+
key_name = "${var.name_prefix}-private-runner-custom-key"
18+
public_key = var.ssh_public_key
19+
20+
tags = {
21+
Name = "${var.name_prefix}-private-runner-custom-key"
22+
}
23+
}
24+
1025
# Launch Template for Auto Scaling Group
1126
resource "aws_launch_template" "this" {
1227
name_prefix = "${var.name_prefix}-private-runner-"
13-
image_id = local.runner_ami_id
28+
image_id = var.ami_id
1429
instance_type = var.instance_type
15-
key_name = var.ssh_key_name
30+
key_name = local.ssh_key_name != "" ? local.ssh_key_name : null
1631

1732
vpc_security_group_ids = [aws_security_group.this.id]
1833

34+
network_interfaces {
35+
associate_public_ip_address = var.associate_public_ip
36+
}
37+
1938
iam_instance_profile {
2039
name = aws_iam_instance_profile.this.name
2140
}
@@ -29,23 +48,17 @@ resource "aws_launch_template" "this" {
2948
block_device_mappings {
3049
device_name = "/dev/sda1"
3150
ebs {
32-
volume_size = 100
33-
volume_type = "gp3"
34-
delete_on_termination = true
51+
volume_size = var.volume_size
52+
volume_type = var.volume_type
53+
delete_on_termination = var.delete_volume_on_termination
3554
}
3655
}
3756

38-
user_data = base64encode("${templatefile("${path.module}/templates/${local.user_data_script}", {
39-
os_family = var.os_family
57+
user_data = base64encode("${templatefile("${path.module}/templates/register_runner.sh", {
4058
sg_org_name = var.sg_org_name
4159
sg_api_uri = var.sg_api_uri
4260
sg_runner_group_name = stackguardian_runner_group.this.resource_name
4361
sg_runner_group_token = data.stackguardian_runner_group_token.this.runner_group_token
44-
# sg_runner_group_token = (
45-
# data.stackguardian_runner_group_token.this.runner_group_token ! = null
46-
# ? data.stackguardian_runner_group_token.this.runner_group_token
47-
# : var.sg_runner_token
48-
# )
4962
})}")
5063

5164
tag_specifications {

stackguardian_private_runner/aws/network.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,13 @@ resource "aws_security_group" "this" {
5050
description = "Block inboud and Allow All outbound for Private Runner."
5151
vpc_id = var.vpc_id
5252

53-
# Allow all ingress
54-
# ingress {
55-
# from_port = 0
56-
# to_port = 0
57-
# protocol = "-1"
58-
# cidr_blocks = ["0.0.0.0/0"]
59-
# }
60-
6153
# Allow SSH
6254
ingress {
6355
description = "SSH"
6456
from_port = 22
6557
to_port = 22
6658
protocol = "tcp"
67-
cidr_blocks = ["178.77.15.22/32"]
59+
cidr_blocks = var.allow_ssh_cidr_blocks
6860
}
6961

7062
egress {

stackguardian_private_runner/aws/provider.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ terraform {
44
source = "StackGuardian/stackguardian"
55
version = "1.3.3"
66
}
7-
# Local Provider
8-
# stackguardian = {
9-
# source = "terraform/provider/stackguardian"
10-
# version = "0.0.0-dev"
11-
# }
127
aws = {
138
source = "hashicorp/aws"
149
}
@@ -25,7 +20,6 @@ terraform {
2520
source = "hashicorp/random"
2621
}
2722
}
28-
required_version = ">= 1.3"
2923
}
3024

3125
provider "aws" {

stackguardian_private_runner/aws/runner_group.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
locals {
2+
runner_group_name = (
3+
var.override_runner_group_name != ""
4+
? var.override_runner_group_name
5+
: "${var.name_prefix}-runner-group-${data.aws_caller_identity.current.account_id}"
6+
)
7+
}
8+
19
resource "stackguardian_runner_group" "this" {
2-
resource_name = "${var.name_prefix}-runner-group-${data.aws_caller_identity.current.account_id}"
10+
resource_name = locals.runner_group_name
311
description = "Private Runner Group for AWS S3 storage backend"
412

513
max_number_of_runners = var.asg_max_size

stackguardian_private_runner/aws/storage_backend.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ resource "random_string" "storage_backend_prefix" {
55
}
66

77
resource "aws_s3_bucket" "this" {
8-
bucket = "${random_string.storage_backend_prefix.result}-private-runner-storage-backend"
9-
# force_destroy = true
8+
bucket = "${random_string.storage_backend_prefix.result}-private-runner-storage-backend"
9+
force_destroy = var.force_destroy_storage_backend
1010
}
1111

1212
# Optional: Block public access

stackguardian_private_runner/aws/templates/register_runner.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/usr/bin/env sh
2+
23
set -e
34

45
## Register Private Runner

stackguardian_private_runner/aws/templates/user_data_all.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

stackguardian_private_runner/aws/variables.tf

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,33 @@ variable "name_prefix" {
3030
default = "sg"
3131
}
3232

33-
/*------------------------+
34-
| EC2 Required Variables |
35-
+------------------------*/
33+
variable "override_runner_group_name" {
34+
description = <<EOT
35+
Optional: Override the default runner group name.
36+
If not provided, the module will use the default group name: {name_prefix}-runner-group-{account_id}
37+
This is useful if you want to use a specific runner group name for your organization.
38+
Default is an empty string, meaning the default group name will be used.
39+
EOT
40+
type = string
41+
default = ""
42+
}
43+
44+
/*---------------------------+
45+
| Backend Storage Variables |
46+
+---------------------------*/
47+
variable "force_destroy_storage_backend" {
48+
description = <<EOT
49+
Whether to force destroy the storage backend (S3 bucket) when the module is destroyed.
50+
This will delete all data in the bucket, so use with caution.
51+
Default is false, meaning the bucket will not be deleted if it contains objects.
52+
EOT
53+
type = bool
54+
default = false
55+
}
56+
57+
/*-----------------------+
58+
| EC2 Network Variables |
59+
+-----------------------*/
3660
variable "vpc_id" {
3761
description = "Existing VPC ID for the Private Runner instance"
3862
type = string
@@ -49,36 +73,69 @@ variable "public_subnet_id" {
4973
type = string
5074
}
5175

52-
variable "ssh_key_name" {
53-
description = "The SSH Key Name for the Private Runner instance"
76+
variable "associate_public_ip" {
77+
description = "Whether to assign a public IP to the Private Runner instance"
78+
type = bool
79+
default = true
80+
}
81+
82+
/*-----------------------+
83+
| EC2 Storage Variables |
84+
+-----------------------*/
85+
variable "volume_type" {
86+
description = "Type of the EBS volume for the Private Runner instance"
5487
type = string
88+
default = "gp3"
5589
}
5690

57-
variable "instance_type" {
58-
description = "The EC2 instance type for Private Runner (min 4 vCPU, 8GB RAM recommended)"
91+
variable "volume_size" {
92+
description = "Size of the EBS volume in GB for the Private Runner instance"
93+
type = number
94+
default = 100
95+
}
96+
97+
variable "delete_volume_on_termination" {
98+
description = "Whether to delete the EBS volume on instance termination"
99+
type = bool
100+
default = false
101+
}
102+
103+
/*------------------------------+
104+
| EC2 SSH Connection Variables |
105+
+------------------------------*/
106+
variable "ssh_key_name" {
107+
description = "The existing SSH key name from AWS. If not provided and ssh_public_key is empty, no SSH key will be configured."
59108
type = string
60-
default = "t3.medium"
109+
default = ""
61110
}
62111

63-
variable "os_family" {
64-
description = "The OS family for Private Runner instance: 'amazon', 'ubuntu', or 'rhel'"
112+
variable "ssh_public_key" {
113+
description = "Custom SSH public key content to add to the instance. If provided, this takes precedence over ssh_key_name."
65114
type = string
66-
default = "ubuntu"
115+
default = ""
116+
}
67117

68-
validation {
69-
condition = contains(["amazon", "ubuntu", "rhel"], var.os_family)
70-
error_message = "The os_family must be one of 'amazon', 'ubuntu', or 'rhel'."
71-
}
118+
variable "allow_ssh_cidr_blocks" {
119+
description = "CIDR blocks allowed to SSH into the Private Runner instance. If empty, no SSH access is allowed."
120+
type = list(string)
121+
default = []
72122
}
73123

74-
variable "os_version" {
75-
description = "Specific OS version (e.g., '20.04' for Ubuntu, '8.5' for RHEL)"
124+
/*------------------------+
125+
| EC2 Instance Variables |
126+
+------------------------*/
127+
variable "instance_type" {
128+
description = "The EC2 instance type for Private Runner (min 4 vCPU, 8GB RAM recommended)"
76129
type = string
77-
default = "22.04"
130+
default = "t3.xlarge"
78131
}
79132

80133
variable "ami_id" {
81-
description = "The AMI ID for the Private Runner instance with pre-installed dependencies. If not provided, it will be fetched based on the OS family and version and dependencies will be installed in user-data."
134+
description = <<EOT
135+
The AMI ID for the Private Runner instance with pre-installed dependencies.
136+
Required dependencies: docker, cron, jq, sg-runner (main.sh)
137+
Recommended: Use StackGuardian Template with Packer to build custom AMI.
138+
EOT
82139
type = string
83140
default = ""
84141
}
@@ -95,7 +152,7 @@ variable "asg_min_size" {
95152
variable "asg_max_size" {
96153
description = "Maximum number of instances in the Auto Scaling Group"
97154
type = number
98-
default = 2
155+
default = 5
99156
}
100157

101158
variable "asg_desired_capacity" {
@@ -123,13 +180,13 @@ variable "image" {
123180
variable "scale_out_cooldown_duration" {
124181
description = "Scale out cooldown duration in minutes"
125182
type = string
126-
default = "2"
183+
default = "3"
127184
}
128185

129186
variable "scale_in_cooldown_duration" {
130187
description = "Scale in cooldown duration in minutes"
131188
type = string
132-
default = "2"
189+
default = "5"
133190
}
134191

135192
variable "scale_out_threshold" {

0 commit comments

Comments
 (0)