Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
11 kB 384 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385# The stansite labeler: a public service at stansite.substandard.blog.## Disabled by default. Nothing here is created until labeler_enabled is true# and the operator has a did:web identity, a PDS for its repository, and a# signing key in Secrets Manager. The key never passes through Terraform.## The service serves its own DID document at /.well-known/did.json, which is# why the identity must be a did:web on this host. The app.bsky.labeler.service# record is published separately, after the document answers.
variable "labeler_enabled" { description = "Create the stansite labeler service" type = bool default = false}
variable "labeler_image_tag" { description = "ECR image tag for the labeler; labeler/deploy.sh writes the commit sha here" type = string default = "latest"
validation { condition = can(regex("^([0-9a-f]{12}|latest)$", var.labeler_image_tag)) error_message = "Use a 12-character commit sha, or latest before the first image is pushed." }}
variable "labeler_did" { description = "The labeler account's did:web identity" type = string default = "did:web:stansite.substandard.blog"
validation { condition = startswith(var.labeler_did, "did:web:") error_message = "The service serves its own DID document, so the identity must be did:web." }}
variable "labeler_pds" { description = "PDS that hosts the labeler account's repository" type = string default = ""
validation { condition = !var.labeler_enabled || var.labeler_pds != "" error_message = "Set labeler_pds when labeler_enabled is true." }}
variable "labeler_signing_key_secret_arn" { description = "Secrets Manager ARN holding the labeler's secp256k1 private key, hex" type = string default = ""
validation { condition = !var.labeler_enabled || var.labeler_signing_key_secret_arn != "" error_message = "Set labeler_signing_key_secret_arn when labeler_enabled is true." }}
variable "labeler_data_url" { description = "Full account dataset the labeler reads" type = string default = "https://substandard.blog/panoptiblog/accounts.json"}
locals { labeler_host = "stansite.${local.domain}"}
resource "aws_ecr_repository" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "substandard-stansite-labeler" image_tag_mutability = "MUTABLE"
image_scanning_configuration { scan_on_push = true }}
resource "aws_ecr_lifecycle_policy" "labeler" { count = var.labeler_enabled ? 1 : 0 repository = aws_ecr_repository.labeler[0].name
policy = jsonencode({ rules = [{ rulePriority = 1 description = "Keep the last 30 labeler images" selection = { tagStatus = "any" countType = "imageCountMoreThan" countNumber = 30 } action = { type = "expire" } }] })}
resource "aws_cloudwatch_log_group" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "/ecs/stansite-labeler" retention_in_days = 30}
resource "aws_iam_role" "labeler_execution" { count = var.labeler_enabled ? 1 : 0 name = "substandard-stansite-labeler-execution" assume_role_policy = data.aws_iam_policy_document.ecs_tasks_assume.json}
resource "aws_iam_role_policy_attachment" "labeler_execution" { count = var.labeler_enabled ? 1 : 0 role = aws_iam_role.labeler_execution[0].name policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"}
resource "aws_iam_role_policy" "labeler_execution_secret" { count = var.labeler_enabled ? 1 : 0 role = aws_iam_role.labeler_execution[0].id
policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["secretsmanager:GetSecretValue"] Resource = [var.labeler_signing_key_secret_arn] }] })}
resource "aws_ecs_cluster" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "substandard-stansite-labeler"}
resource "aws_security_group" "labeler_alb" { count = var.labeler_enabled ? 1 : 0 name_prefix = "substandard-stansite-labeler-alb-" description = "Public HTTPS ingress for the stansite labeler" vpc_id = data.aws_vpc.default.id
ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }
ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }
egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] }}
resource "aws_security_group" "labeler_service" { count = var.labeler_enabled ? 1 : 0 name_prefix = "substandard-stansite-labeler-service-" description = "The labeler task, reachable only from its load balancer" vpc_id = data.aws_vpc.default.id
ingress { from_port = 3000 to_port = 3000 protocol = "tcp" security_groups = [aws_security_group.labeler_alb[0].id] }
egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] }}
resource "aws_ecs_task_definition" "labeler" { count = var.labeler_enabled ? 1 : 0 family = "substandard-stansite-labeler" requires_compatibilities = ["FARGATE"] network_mode = "awsvpc" cpu = "512" memory = "1024" execution_role_arn = aws_iam_role.labeler_execution[0].arn
container_definitions = jsonencode([{ name = "labeler" image = "${aws_ecr_repository.labeler[0].repository_url}:${var.labeler_image_tag}" essential = true
portMappings = [{ containerPort = 3000 hostPort = 3000 protocol = "tcp" }]
environment = [ { name = "LABELER_DID", value = var.labeler_did }, { name = "LABELER_PDS", value = var.labeler_pds }, { name = "LABELER_ENDPOINT", value = "https://${local.labeler_host}" }, { name = "PANOPTIBLOG_DATA_URL", value = var.labeler_data_url }, { name = "PORT", value = "3000" }, ]
secrets = [{ name = "LABELER_SIGNING_KEY" valueFrom = var.labeler_signing_key_secret_arn }]
logConfiguration = { logDriver = "awslogs" options = { "awslogs-group" = aws_cloudwatch_log_group.labeler[0].name "awslogs-region" = "us-east-1" "awslogs-stream-prefix" = "labeler" } } }])}
resource "aws_lb" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "stansite-labeler" internal = false load_balancer_type = "application" security_groups = [aws_security_group.labeler_alb[0].id] subnets = data.aws_subnets.default.ids}
resource "aws_lb_target_group" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "stansite-labeler-tg" port = 3000 protocol = "HTTP" target_type = "ip" vpc_id = data.aws_vpc.default.id
health_check { enabled = true path = "/health" matcher = "200" interval = 30 timeout = 5 healthy_threshold = 2 unhealthy_threshold = 3 }}
resource "aws_acm_certificate" "labeler" { count = var.labeler_enabled ? 1 : 0 domain_name = local.labeler_host validation_method = "DNS"
lifecycle { create_before_destroy = true }}
resource "aws_route53_record" "labeler_cert_validation" { for_each = var.labeler_enabled ? { for dvo in aws_acm_certificate.labeler[0].domain_validation_options : dvo.domain_name => { name = dvo.resource_record_name type = dvo.resource_record_type record = dvo.resource_record_value } } : {}
zone_id = aws_route53_zone.substandard.zone_id name = each.value.name type = each.value.type records = [each.value.record] ttl = 300}
resource "aws_acm_certificate_validation" "labeler" { count = var.labeler_enabled ? 1 : 0 certificate_arn = aws_acm_certificate.labeler[0].arn validation_record_fqdns = [for record in aws_route53_record.labeler_cert_validation : record.fqdn]}
resource "aws_lb_listener" "labeler_https" { count = var.labeler_enabled ? 1 : 0 load_balancer_arn = aws_lb.labeler[0].arn port = 443 protocol = "HTTPS" ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" certificate_arn = aws_acm_certificate_validation.labeler[0].certificate_arn
default_action { type = "forward" target_group_arn = aws_lb_target_group.labeler[0].arn }}
resource "aws_lb_listener" "labeler_http" { count = var.labeler_enabled ? 1 : 0 load_balancer_arn = aws_lb.labeler[0].arn port = 80 protocol = "HTTP"
default_action { type = "redirect"
redirect { port = "443" protocol = "HTTPS" status_code = "HTTP_301" } }}
resource "aws_route53_record" "labeler_a" { count = var.labeler_enabled ? 1 : 0 zone_id = aws_route53_zone.substandard.zone_id name = local.labeler_host type = "A"
alias { name = aws_lb.labeler[0].dns_name zone_id = aws_lb.labeler[0].zone_id evaluate_target_health = false }}
resource "aws_route53_record" "labeler_aaaa" { count = var.labeler_enabled ? 1 : 0 zone_id = aws_route53_zone.substandard.zone_id name = local.labeler_host type = "AAAA"
alias { name = aws_lb.labeler[0].dns_name zone_id = aws_lb.labeler[0].zone_id evaluate_target_health = false }}
resource "aws_ecs_service" "labeler" { count = var.labeler_enabled ? 1 : 0 name = "stansite-labeler" cluster = aws_ecs_cluster.labeler[0].id task_definition = aws_ecs_task_definition.labeler[0].arn desired_count = 1 launch_type = "FARGATE"
network_configuration { subnets = data.aws_subnets.default.ids security_groups = [aws_security_group.labeler_service[0].id] assign_public_ip = true }
load_balancer { target_group_arn = aws_lb_target_group.labeler[0].arn container_name = "labeler" container_port = 3000 }
depends_on = [aws_lb_listener.labeler_https]}
output "labeler_repository_url" { description = "ECR repository the labeler image is pushed to; null while the labeler is disabled" value = try(aws_ecr_repository.labeler[0].repository_url, null)}
output "labeler_url" { value = "https://${local.labeler_host}"}
output "labeler_cluster" { value = try(aws_ecs_cluster.labeler[0].name, null)}
output "labeler_service" { value = try(aws_ecs_service.labeler[0].name, null)}