Files
acdl/terraform/platform/main.tf
T
Jon Chery 07c0349131 feat(P24): platform Lambda + DynamoDB contract ingestion + cross-account IAM
Phase 24 — platform-lambda-and-contract-ingestion.

- core/lambda/contract_ingestor.py: AWS Lambda handler invoked via Function
  URL (IAM auth). Parses JSON body, validates required fields, writes the
  contract to DynamoDB table acdl-contracts (PK consumerRepo, SK
  contractId#submittedAt, status submitted, ISO-8601 submittedAt). report_error
  action is a stub returning "error_report_prepared"; GitHub issue creation is
  wired in Phase 25. Returns 400 on missing fields / unknown action, 500 on
  error. Table name + GitHub-token secret ID come from env (set by Terraform).
- core/lambda/__init__.py: empty package marker.
- terraform/platform/main.tf: DynamoDB acdl-contracts (PITR, SSE via CMK),
  KMS customer-managed key with alias/acdl-platform, Secrets Manager secret
  acdl/github-token, IAM execution role (DynamoDB write + Secrets Manager read +
  KMS decrypt + CloudWatch logs), Lambda acdl-contract-ingestor (Python 3.12,
  handler contract_ingestor.lambda_handler), Function URL with AWS_IAM auth.
  State key platform/terraform.tfstate (distinct from spike/microservice).
- terraform/platform/README.md: documents what it deploys, the state key, how
  to apply, and the cross-account invocation model.
- terraform/platform/consumer_invoke_policy.json: ABAC-scoped policy template
  applied to consumer deploy roles during onboarding; grants
  lambda:InvokeFunctionUrl conditioned on aws:PrincipalTag/acdl:owner ==
  consumerRepo.
- tests/test_contract_ingestor.py: 11 tests (moto-backed DynamoDB mock) covering
  submit_contract put_item shape, report_error stub, missing-field 400, unknown
  action 400, the lambda_handler wrapper with a Function-URL-style event, dict
  body, default action, and internal-error 500.
- docs/environments/index.md: new section documenting the cross-account
  contract-ingestion grant (one-way consumer→platform, D-051) and that
  onboarding now also grants the consumer deploy role InvokeFunctionUrl.
- scripts/run_ci.sh, pipelines/ci.yaml, .gitea/workflows/ci.yml,
  .github/workflows/ci.yml: add core/lambda/contract_ingestor.py to the lint
  py_compile list. The two workflow YAMLs remain byte-identical.

Verification: scripts/run_ci.sh passes all 3 stages (lint/test/check-only);
python3 -m pytest tests/ -v passes all 213 tests (11 new + 202 existing).

---ci---
project: acdl
phase: 24
milestone: v1.7
status: execute
---/ci---
2026-07-22 20:04:10 +00:00

164 lines
4.7 KiB
Terraform

# ACDL platform infrastructure — contract ingestion Lambda + DynamoDB (D-051)
#
# Deploys:
# - DynamoDB table acdl-contracts (PK consumerRepo, SK contractId#submittedAt, SSE via CMK, PITR)
# - KMS customer-managed key for DynamoDB + SSM (shared CMK)
# - Lambda function acdl-contract-ingestor (Python 3.12, handler contract_ingestor.lambda_handler)
# - Lambda Function URL (IAM auth — consumers invoke via SigV4)
# - Secrets Manager secret acdl/github-token (stores the Lambda's GitHub PAT for issue creation)
# - IAM execution role for the Lambda (DynamoDB write + Secrets Manager read + KMS decrypt)
#
# State: terraform/platform/terraform.tfstate (separate from spike/ and microservice/)
terraform {
required_version = ">= 1.9, < 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "acdl-tfstate-581513795199-us-east-1"
key = "platform/terraform.tfstate"
region = "us-east-1"
}
}
provider "aws" {
region = "us-east-1"
}
# KMS customer-managed key for DynamoDB SSE + SSM Parameter Store encryption
resource "aws_kms_key" "acdl_platform" {
description = "ACDL platform KMS key (DynamoDB SSE + SSM + Secrets Manager)"
enable_key_rotation = true
deletion_window_in_days = 30
}
resource "aws_kms_alias" "acdl_platform" {
name = "alias/acdl-platform"
target_key_id = aws_kms_key.acdl_platform.key_id
}
# DynamoDB table for contract ingestion
resource "aws_dynamodb_table" "acdl_contracts" {
name = "acdl-contracts"
billing_mode = "PAY_PER_REQUEST"
hash_key = "consumerRepo"
range_key = "contractId#submittedAt"
attribute {
name = "consumerRepo"
type = "S"
}
attribute {
name = "contractId#submittedAt"
type = "S"
}
point_in_time_recovery {
enabled = true
}
server_side_encryption {
enabled = true
kms_key_arn = aws_kms_key.acdl_platform.arn
}
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# Secrets Manager secret for the Lambda's GitHub token (issue creation)
resource "aws_secretsmanager_secret" "github_token" {
name = "acdl/github-token"
description = "GitHub PAT for the platform Lambda to create issues on the platform repo (D-055)."
kms_key_id = aws_kms_key.acdl_platform.arn
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# IAM execution role for the Lambda
resource "aws_iam_role" "lambda_exec" {
name = "acdl-contract-ingestor-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
}]
})
}
resource "aws_iam_role_policy" "lambda_permissions" {
name = "acdl-contract-ingestor-policy"
role = aws_iam_role.lambda_exec.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = ["dynamodb:PutItem", "dynamodb:GetItem", "dynamodb:Query", "dynamodb:UpdateItem"]
Resource = aws_dynamodb_table.acdl_contracts.arn
},
{
Effect = "Allow"
Action = ["secretsmanager:GetSecretValue"]
Resource = aws_secretsmanager_secret.github_token.arn
},
{
Effect = "Allow"
Action = ["kms:Decrypt"]
Resource = aws_kms_key.acdl_platform.arn
},
{
Effect = "Allow"
Action = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
# Lambda function
resource "aws_lambda_function" "contract_ingestor" {
function_name = "acdl-contract-ingestor"
handler = "contract_ingestor.lambda_handler"
runtime = "python3.12"
role = aws_iam_role.lambda_exec.arn
filename = "contract_ingestor.zip"
source_code_hash = filebase64sha256("contract_ingestor.zip")
environment {
variables = {
CONTRACTS_TABLE = aws_dynamodb_table.acdl_contracts.name
GITHUB_TOKEN_SECRET_ID = aws_secretsmanager_secret.github_token.name
PLATFORM_REPO = "acdl/acdl"
}
}
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# Lambda Function URL (IAM auth — consumers invoke via SigV4)
resource "aws_lambda_function_url" "contract_ingestor" {
function_name = aws_lambda_function.contract_ingestor.function_name
authorization_type = "AWS_IAM"
}