diff --git a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.test.ts b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.test.ts index 5fe55d59aa..e88370daa5 100644 --- a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.test.ts +++ b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.test.ts @@ -12,6 +12,10 @@ describe('EC2 scale-set provider configuration', () => { expect(parseEc2ScaleSetProviderConfig({ ...config, runnerNamePrefix: 'r'.repeat(45) })).toMatchObject({ runnerNamePrefix: 'r'.repeat(45), }); + expect(parseEc2ScaleSetProviderConfig(config).jitConfigTTLSeconds).toBeUndefined(); + expect(parseEc2ScaleSetProviderConfig({ ...config, jitConfigTTLSeconds: 3600 })).toMatchObject({ + jitConfigTTLSeconds: 3600, + }); }); it.each([ @@ -22,6 +26,10 @@ describe('EC2 scale-set provider configuration', () => { [{ ...config, ssmParameterTags: [{ Key: 'aws:owner', Value: 'untrusted' }] }], [{ ...config, runnerNamePrefix: 'r'.repeat(46) }], [{ ...config, bootTimeoutMinutes: 10 }], + [{ ...config, jitConfigTTLSeconds: 0 }], + [{ ...config, jitConfigTTLSeconds: -1 }], + [{ ...config, jitConfigTTLSeconds: Number.NaN }], + [{ ...config, jitConfigTTLSeconds: '3600' }], ])('rejects invalid or unsupported values instead of forwarding them to AWS', (invalid) => { expect(() => parseEc2ScaleSetProviderConfig(invalid)).toThrow(); }); diff --git a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.ts b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.ts index 91ae93d555..2f14426765 100644 --- a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.ts +++ b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/configuration.ts @@ -18,6 +18,7 @@ export interface Ec2ScaleSetProviderConfig { environment: string; runnerNamePrefix: string; jitConfigParameterPath: string; + jitConfigTTLSeconds?: number; subnets: string[]; launchTemplateName: string; ec2instanceCriteria: RunnerInputParameters['ec2instanceCriteria']; @@ -72,6 +73,14 @@ function optionalBoolean(value: unknown, name: string): boolean | undefined { return value; } +function optionalPositiveNumber(value: unknown, name: string): number | undefined { + if (value === undefined) return undefined; + if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) { + throw new Ec2ScaleSetValidationError(`Invalid EC2 scale-set configuration field '${name}'`); + } + return value; +} + function requireStringArray( value: unknown, name: string, @@ -213,6 +222,7 @@ export function parseEc2ScaleSetProviderConfig(value: unknown): Ec2ScaleSetProvi 'environment', 'runnerNamePrefix', 'jitConfigParameterPath', + 'jitConfigTTLSeconds', 'subnets', 'launchTemplateName', 'ec2instanceCriteria', @@ -271,6 +281,7 @@ export function parseEc2ScaleSetProviderConfig(value: unknown): Ec2ScaleSetProvi environment: requireString(value.environment, 'environment', /^[A-Za-z0-9][A-Za-z0-9._-]*$/, 128), runnerNamePrefix: requirePossiblyEmptyString(value.runnerNamePrefix, 'runnerNamePrefix', /^[A-Za-z0-9._-]*$/, 45), jitConfigParameterPath, + jitConfigTTLSeconds: optionalPositiveNumber(value.jitConfigTTLSeconds, 'jitConfigTTLSeconds'), subnets: requireStringArray(value.subnets, 'subnets', /^subnet-[0-9a-f]+$/, 32), launchTemplateName: requireString(value.launchTemplateName, 'launchTemplateName', /^[A-Za-z0-9()./_-]+$/, 128), ec2instanceCriteria: { diff --git a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.test.ts b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.test.ts index d72c8a50e8..0f7c8746c8 100644 --- a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.test.ts +++ b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.test.ts @@ -71,6 +71,45 @@ describe('EC2 scale-set scale up', () => { }); }); + it('publishes the JIT configuration on the Standard tier without a policy when no TTL is configured', async () => { + const instanceId = 'i-1234567890abcdef0'; + ec2Mock.on(DescribeInstancesCommand).resolves({}); + ec2Mock.on(CreateFleetCommand).resolves({ Instances: [{ InstanceIds: [instanceId] }] }); + + await createTestProvider().reconcile(createRequest()); + + expect(ssmMock).toHaveReceivedCommandWith(PutParameterCommand, { Tier: 'Standard', Policies: undefined }); + }); + + it('publishes the JIT configuration on the Advanced tier with an expiration policy when a TTL is configured', async () => { + const instanceId = 'i-1234567890abcdef0'; + const now = new Date('2026-01-01T00:00:00.000Z'); + vi.useFakeTimers({ now, toFake: ['Date'] }); + try { + ec2Mock.on(DescribeInstancesCommand).resolves({}); + ec2Mock.on(CreateFleetCommand).resolves({ Instances: [{ InstanceIds: [instanceId] }] }); + + const result = await createTestProvider({ + configuration: { ...config, jitConfigTTLSeconds: 3600 }, + }).reconcile(createRequest()); + + expect(result.actions.launched).toBe(1); + expect(ssmMock).toHaveReceivedCommandWith(PutParameterCommand, { + Name: `${config.jitConfigParameterPath}/${instanceId}`, + Tier: 'Advanced', + Policies: JSON.stringify([ + { + Type: 'Expiration', + Version: '1.0', + Attributes: { Timestamp: '2026-01-01T01:00:00.000Z' }, + }, + ]), + }); + } finally { + vi.useRealTimers(); + } + }); + it('resolves an AMI parameter through the provider-owned SSM client', async () => { const instanceId = 'i-1234567890abcdef0'; const amiIdSsmParameterName = '/github-action-runners/unit-test/ami'; diff --git a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.ts b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.ts index ef6cfc3a88..3f9e94699f 100644 --- a/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.ts +++ b/lambdas/libs/compute-providers/aws/ec2/src/scale-set/scale-up.ts @@ -79,6 +79,11 @@ async function publishJitConfiguration( throw new Ec2ScaleSetValidationError('JIT configuration must be between 1 and 8192 bytes'); } + // Parameter policies (e.g. Expiration) are only supported on the Advanced + // tier, so a TTL forces the tier regardless of the value size. Expiration is + // enforced asynchronously by SSM and only cleans up parameters left behind by + // instances that never read their JIT configuration. + const ttlSeconds = input.configuration.jitConfigTTLSeconds; await ssmClient.send( new PutParameterCommand({ Name: jitParameterName(input.configuration, instanceId), @@ -86,7 +91,17 @@ async function publishJitConfiguration( Type: 'SecureString', KeyId: input.configuration.ssmKmsKeyId, Overwrite: false, - Tier: valueSize >= SSM_STANDARD_TIER_THRESHOLD ? 'Advanced' : 'Standard', + Tier: ttlSeconds !== undefined || valueSize >= SSM_STANDARD_TIER_THRESHOLD ? 'Advanced' : 'Standard', + Policies: + ttlSeconds === undefined + ? undefined + : JSON.stringify([ + { + Type: 'Expiration', + Version: '1.0', + Attributes: { Timestamp: new Date(Date.now() + ttlSeconds * 1000).toISOString() }, + }, + ]), Tags: jitParameterTags(input, instanceId), }), { abortSignal: signal }, diff --git a/modules/compute-providers/aws/ec2/scale-set.tf b/modules/compute-providers/aws/ec2/scale-set.tf index f6246eeb86..beaf0ca9c9 100644 --- a/modules/compute-providers/aws/ec2/scale-set.tf +++ b/modules/compute-providers/aws/ec2/scale-set.tf @@ -37,6 +37,9 @@ locals { local.ami_id_ssm_external ? { amiIdSsmParameterName = local.ami_id_ssm_parameter_name } : {}, + var.storage_provider.aws.ssm.ttl_seconds.tokens == null ? {} : { + jitConfigTTLSeconds = var.storage_provider.aws.ssm.ttl_seconds.tokens + }, ) scale_set_owned_instance_conditions = [ diff --git a/modules/compute-providers/aws/ec2/variables.tf b/modules/compute-providers/aws/ec2/variables.tf index e1fd17428c..4cda0e7e8c 100644 --- a/modules/compute-providers/aws/ec2/variables.tf +++ b/modules/compute-providers/aws/ec2/variables.tf @@ -364,6 +364,7 @@ variable "storage_provider" { - `aws.ssm.paths.root`: Root Parameter Store path for the runner configuration. - `aws.ssm.paths.tokens`: Path segment used for registration tokens and just-in-time configuration. - `aws.ssm.paths.config`: Path segment used for persistent runner and provider configuration. + - `aws.ssm.ttl_seconds.tokens`: Optional TTL in seconds for scale-set just-in-time configuration parameters. When set, the parameters use the Advanced tier with an SSM expiration policy. - `aws.ssm.tags`: Shared SSM tags that override module-level `tags`. - `aws.ssm.parameters.tags`: Parameter-specific tags that override module-level and shared SSM tags. EOT @@ -375,6 +376,9 @@ variable "storage_provider" { tokens = string config = string }) + ttl_seconds = optional(object({ + tokens = optional(number, null) + }), {}) tags = optional(map(string), {}) parameters = optional(object({ tags = optional(map(string), {}) diff --git a/modules/multi-runner/tests/config-translation.tftest.hcl b/modules/multi-runner/tests/config-translation.tftest.hcl index c693e64bff..8347233b9e 100644 --- a/modules/multi-runner/tests/config-translation.tftest.hcl +++ b/modules/multi-runner/tests/config-translation.tftest.hcl @@ -542,6 +542,11 @@ run "non_empty_v2_map_is_authoritative" { error_message = "A non-empty v2 runner map must be authoritative and must not merge stable lanes or flat defaults." } + assert { + condition = module.runner_configs["experimental"].orchestration_provider.webhook.scale_up.lambda.environment[0].variables["SSM_TOKEN_TTL_SECONDS"] == "7200" + error_message = "A v2 lane token TTL must reach the scale-up Lambda environment." + } + assert { condition = jsonencode(local.normalized_config) == jsonencode(local.v2_config) error_message = "A non-empty v2 runner map must select the v2 object without leaking stable flat inputs." diff --git a/modules/orchestration-providers/webhook/pool/pool.tf b/modules/orchestration-providers/webhook/pool/pool.tf index 43e3653e8f..6d2e0ca7e9 100644 --- a/modules/orchestration-providers/webhook/pool/pool.tf +++ b/modules/orchestration-providers/webhook/pool/pool.tf @@ -29,7 +29,7 @@ locals { INCLUDE_BUSY_RUNNERS = var.config.include_busy_runners } - ssm_environment_variables = { + ssm_environment_variables = merge({ PARAMETER_GITHUB_APP_ID_NAME = var.config.github_app_parameters.id.name PARAMETER_GITHUB_APP_KEY_BASE64_NAME = var.config.github_app_parameters.key_base64.name PARAMETER_GITHUB_APPS_MANIFEST_NAME = var.config.github_app_parameters.additional_apps_manifest != null ? var.config.github_app_parameters.additional_apps_manifest.name : "" @@ -38,7 +38,9 @@ locals { SSM_PARAMETER_STORE_TAGS = var.storage_provider.aws.ssm.parameter_store_tags SSM_PARAMETER_STORE_MAX_CONCURRENT_INVOCATIONS = var.storage_provider.aws.ssm.parameter_store_max_concurrent_invocations SSM_PARAMETER_STORE_MAX_WRITES_PER_SECOND = var.storage_provider.aws.ssm.parameter_store_max_writes_per_second - } + }, var.storage_provider.aws.ssm.token_ttl_seconds != null ? { + SSM_TOKEN_TTL_SECONDS = tostring(var.storage_provider.aws.ssm.token_ttl_seconds) + } : {}) } resource "aws_lambda_function" "pool" { diff --git a/modules/orchestration-providers/webhook/pool/variables.tf b/modules/orchestration-providers/webhook/pool/variables.tf index eb10c1e586..2b776b0be1 100644 --- a/modules/orchestration-providers/webhook/pool/variables.tf +++ b/modules/orchestration-providers/webhook/pool/variables.tf @@ -145,6 +145,7 @@ variable "storage_provider" { parameter_store_max_concurrent_invocations = optional(number, 1) parameter_store_max_writes_per_second = optional(number, 40) + token_ttl_seconds = optional(number, null) }) }) environment_variables = optional(map(string), {}) diff --git a/modules/orchestration-providers/webhook/scale-runners/scale-up.tf b/modules/orchestration-providers/webhook/scale-runners/scale-up.tf index cc26020663..379415cf9b 100644 --- a/modules/orchestration-providers/webhook/scale-runners/scale-up.tf +++ b/modules/orchestration-providers/webhook/scale-runners/scale-up.tf @@ -49,7 +49,9 @@ resource "aws_lambda_function" "scale_up" { SSM_PARAMETER_STORE_TAGS = var.storage_provider.aws.ssm.parameter_store_tags SSM_PARAMETER_STORE_MAX_CONCURRENT_INVOCATIONS = var.storage_provider.aws.ssm.parameter_store_max_concurrent_invocations SSM_PARAMETER_STORE_MAX_WRITES_PER_SECOND = var.storage_provider.aws.ssm.parameter_store_max_writes_per_second - }, var.storage_provider.scale_up.environment_variables) + }, var.storage_provider.aws.ssm.token_ttl_seconds != null ? { + SSM_TOKEN_TTL_SECONDS = tostring(var.storage_provider.aws.ssm.token_ttl_seconds) + } : {}, var.storage_provider.scale_up.environment_variables) } dynamic "vpc_config" { diff --git a/modules/orchestration-providers/webhook/scale-runners/variables.tf b/modules/orchestration-providers/webhook/scale-runners/variables.tf index 0e35c41160..95ab401bbe 100644 --- a/modules/orchestration-providers/webhook/scale-runners/variables.tf +++ b/modules/orchestration-providers/webhook/scale-runners/variables.tf @@ -49,6 +49,7 @@ variable "config" { - `storage_provider.aws.ssm.parameter_store_tags`: JSON-encoded tags applied to parameters created at runtime. - `storage_provider.aws.ssm.parameter_store_max_concurrent_invocations`: Expected number of concurrent scale-up and pool Lambda invocations writing to Parameter Store, used to pace writes. - `storage_provider.aws.ssm.parameter_store_max_writes_per_second`: Parameter Store write-rate limit, in writes per second, that runtime writes are paced against. + - `storage_provider.aws.ssm.token_ttl_seconds`: Optional TTL in seconds for registration-token and JIT-config parameters. Null leaves SSM expiration policies disabled. - `observability.logs`: Shared logging level, retention, encryption, and log-class configuration. - `observability.tracing`: Lambda X-Ray and tracing-helper configuration. - `observability.metrics`: Metrics enablement, namespace, and GitHub rate-limit metric configuration. @@ -247,6 +248,7 @@ variable "storage_provider" { parameter_store_max_concurrent_invocations = optional(number, 1) parameter_store_max_writes_per_second = optional(number, 40) + token_ttl_seconds = optional(number, null) }) }) scale_up = optional(object({ diff --git a/modules/orchestration-providers/webhook/variables.tf b/modules/orchestration-providers/webhook/variables.tf index 57b583730e..c557f5f912 100644 --- a/modules/orchestration-providers/webhook/variables.tf +++ b/modules/orchestration-providers/webhook/variables.tf @@ -221,6 +221,7 @@ variable "storage_provider" { - `storage_provider.aws.ssm.parameter_store_tags`: JSON-encoded tags applied to runtime parameters. - `storage_provider.aws.ssm.parameter_store_max_concurrent_invocations`: Expected number of concurrent scale-up and pool Lambda invocations writing to Parameter Store, used to pace writes. - `storage_provider.aws.ssm.parameter_store_max_writes_per_second`: Parameter Store write-rate limit, in writes per second, that runtime writes are paced against. + - `storage_provider.aws.ssm.token_ttl_seconds`: Optional TTL in seconds for registration-token and JIT-config parameters. Null leaves SSM expiration policies disabled. - `scale_up`, `scale_down`, `pool`, and `job_retry`: Provider-owned environment variables and IAM policy fragments. EOT type = object({ @@ -235,6 +236,7 @@ variable "storage_provider" { parameter_store_max_concurrent_invocations = optional(number, 1) parameter_store_max_writes_per_second = optional(number, 40) + token_ttl_seconds = optional(number, null) }) }) scale_up = optional(object({ diff --git a/modules/runner-config/orchestration-provider.tf b/modules/runner-config/orchestration-provider.tf index b049c70196..7eea1f22ad 100644 --- a/modules/runner-config/orchestration-provider.tf +++ b/modules/runner-config/orchestration-provider.tf @@ -55,6 +55,7 @@ module "orchestration_webhook" { parameter_store_tags = local.parameter_store_tags parameter_store_max_concurrent_invocations = var.storage_provider.aws.ssm.parameters.max_concurrent_invocations parameter_store_max_writes_per_second = var.storage_provider.aws.ssm.parameters.max_writes_per_second + token_ttl_seconds = var.storage_provider.aws.ssm.ttl_seconds.tokens } } } diff --git a/modules/runner-config/tests/pool.tftest.hcl b/modules/runner-config/tests/pool.tftest.hcl index 8537f3f553..56710a8aad 100644 --- a/modules/runner-config/tests/pool.tftest.hcl +++ b/modules/runner-config/tests/pool.tftest.hcl @@ -692,3 +692,63 @@ run "job_retry_uses_common_runner_configuration_identity" { error_message = "Job retry must apply its configured Lambda reserved concurrency." } } + +run "token_ttl_defaults_to_disabled" { + command = plan + + assert { + condition = ( + !contains(keys(module.orchestration_webhook[0].scale_up.lambda.environment[0].variables), "SSM_TOKEN_TTL_SECONDS") + && !contains(keys(module.orchestration_webhook[0].pool.lambda.environment[0].variables), "SSM_TOKEN_TTL_SECONDS") + ) + error_message = "Omitted token TTL must leave expiration disabled." + } +} + +run "token_ttl_reaches_scale_up_and_pool" { + command = plan + + variables { + storage_provider = { + aws = { + ssm = { + paths = { + root = "/github-runner" + tokens = "tokens" + config = "config" + } + ttl_seconds = { tokens = 300 } + } + } + } + } + + assert { + condition = ( + module.orchestration_webhook[0].scale_up.lambda.environment[0].variables["SSM_TOKEN_TTL_SECONDS"] == "300" + && module.orchestration_webhook[0].pool.lambda.environment[0].variables["SSM_TOKEN_TTL_SECONDS"] == "300" + ) + error_message = "The storage-provider token TTL must reach both scale-up and pool Lambdas." + } +} + +run "rejects_non_positive_token_ttl" { + command = plan + + variables { + storage_provider = { + aws = { + ssm = { + paths = { + root = "/github-runner" + tokens = "tokens" + config = "config" + } + ttl_seconds = { tokens = 0 } + } + } + } + } + + expect_failures = [var.storage_provider] +} diff --git a/modules/runner-config/variables.tf b/modules/runner-config/variables.tf index f08d977d37..f32a64651d 100644 --- a/modules/runner-config/variables.tf +++ b/modules/runner-config/variables.tf @@ -147,6 +147,7 @@ variable "storage_provider" { - `storage_provider.aws.ssm.paths.tokens`: Path segment under `paths.root` used for registration tokens and just-in-time configuration. - `storage_provider.aws.ssm.paths.config`: Path segment under `paths.root` used for persistent runner configuration. - `storage_provider.aws.ssm.kms_key_id`: Optional customer-managed KMS key ARN used by control-plane IAM policies to decrypt shared GitHub App parameters. The ARN may be unknown until apply; null omits the provider-owned KMS statements. It does not select encryption for runtime-created runner parameters. + - `storage_provider.aws.ssm.ttl_seconds.tokens`: Optional TTL in seconds for the Parameter Store parameters holding the runner registration token or just-in-time configuration. When set, the webhook scale-up and pool Lambdas and the scale-set controller create these parameters with an SSM expiration policy. Requires the Advanced parameter tier, which incurs additional costs. Must be a positive number and should comfortably exceed the runner boot time. Null leaves native expiration disabled. - `storage_provider.aws.ssm.tags`: Shared tags for SSM-related resources. These override module-level `tags` and are inherited by parameter and housekeeper resources. - `storage_provider.aws.ssm.parameters.max_concurrent_invocations`: Expected number of concurrent scale-up and pool Lambda invocations writing to Parameter Store. Each invocation paces its writes to a share of the account-wide write-rate limit. The default is `1`. - `storage_provider.aws.ssm.parameters.max_writes_per_second`: Parameter Store write rate to pace against, in writes per second. AWS documents a `PutParameter` quota of 3 TPS per account and Region (10 TPS with higher throughput enabled). The default of 40 keeps the previous pacing; lower it if `PutParameter` calls are throttled. @@ -174,7 +175,10 @@ variable "storage_provider" { config = string }) kms_key_id = optional(string, null) - tags = optional(map(string), {}) + ttl_seconds = optional(object({ + tokens = optional(number, null) + }), {}) + tags = optional(map(string), {}) parameters = optional(object({ tags = optional(map(string), {}) max_concurrent_invocations = optional(number, 1) @@ -205,6 +209,10 @@ variable "storage_provider" { }) }) + validation { + condition = var.storage_provider.aws.ssm.ttl_seconds.tokens == null ? true : var.storage_provider.aws.ssm.ttl_seconds.tokens > 0 + error_message = "`storage_provider.aws.ssm.ttl_seconds.tokens` must be a positive number." + } } variable "observability" {