Model/deployment/terraform/lambda/modelling_e2e/main.tf
Khalim Conn-Kowlessar ecb31bf114 Inject Solar throttle gap derived from queue maximum_concurrency
Terraform computes SOLAR_MIN_REQUEST_INTERVAL_SECONDS = N / (0.8 * 600 / 60)
from var.maximum_concurrency (=4.0s at N=32) so the fleet width has one source
of truth and the per-container Solar pacing tracks it automatically.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 16:05:57 +00:00

64 lines
2.3 KiB
HCL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

data "terraform_remote_state" "shared" {
backend = "s3"
config = {
bucket = "assessment-model-terraform-state"
key = "env:/${var.stage}/terraform.tfstate"
region = "eu-west-2"
}
}
data "aws_secretsmanager_secret_version" "db_credentials" {
secret_id = "${var.stage}/assessment_model/db_credentials"
}
locals {
db_credentials = jsondecode(data.aws_secretsmanager_secret_version.db_credentials.secret_string)
# Per-container minimum gap (seconds) between Google Solar API calls. The Solar
# API caps the whole GCP project at a hard 600 QPM shared across Building
# Insights + Data Layers. Each of the up-to-`maximum_concurrency` containers
# loops its ~50-property batch sequentially, so pacing each one to
# 0.8 (safety headroom) × 600 QPM ÷ 60 ÷ N keeps the fleet sum under the
# ceiling — eliminating the sustained 429 storm that per-process backoff alone
# can't ride out. Derived from `maximum_concurrency` so N has one source of
# truth. At N=32 → 4.0s. Consumed by the handler's throttle (see
# infrastructure/solar/google_solar_api_client.py).
solar_min_request_interval_seconds = var.maximum_concurrency / (0.8 * 600 / 60)
}
module "lambda" {
source = "../../modules/lambda_with_sqs"
name = var.lambda_name
stage = var.stage
image_uri = local.image_uri
reserved_concurrent_executions = var.reserved_concurrent_executions
batch_size = var.batch_size
maximum_concurrency = var.maximum_concurrency
timeout = 900
memory_size = 3008
environment = {
STAGE = var.stage
LOG_LEVEL = "info"
POSTGRES_USERNAME = local.db_credentials.db_assessment_model_username
POSTGRES_PASSWORD = local.db_credentials.db_assessment_model_password
POSTGRES_HOST = var.db_host
POSTGRES_DATABASE = var.db_name
POSTGRES_PORT = var.db_port
OPEN_EPC_API_TOKEN = var.open_epc_api_token
GOOGLE_SOLAR_API_KEY = var.google_solar_api_key
DATA_BUCKET = "retrofit-data-${var.stage}"
SOLAR_MIN_REQUEST_INTERVAL_SECONDS = tostring(local.solar_min_request_interval_seconds)
}
}
resource "aws_iam_role_policy_attachment" "modelling_e2e_s3_read" {
role = module.lambda.role_name
policy_arn = data.terraform_remote_state.shared.outputs.modelling_e2e_s3_read_arn
}