import * as cdk from "aws-cdk-lib"; import * as budgets from "aws-cdk-lib/aws-budgets"; import { Construct } from "constructs"; export interface GovernanceTogglesProps { /** Monthly cost budget ceiling in USD. */ readonly monthlyLimitUsd: number; /** Email that receives the budget threshold alerts. */ readonly alertEmail: string; } /** * Account-level governance toggles expressible as CloudFormation. Adapted from * seahaven-account-baseline/lib/governance-toggles.ts. * * Provides a monthly AWS Budget with 80% / 100% actual + 100% forecast alerts. * Alerts go to a Sea Haven ops address (NOT the external dev team) so cost * surprises surface to the account owner. */ export class GovernanceToggles extends Construct { constructor(scope: Construct, id: string, props: GovernanceTogglesProps) { super(scope, id); const subscriber = [ { subscriptionType: "EMAIL", address: props.alertEmail, }, ]; new budgets.CfnBudget(this, "MonthlyCostBudget", { budget: { budgetName: "seahaven-extdev-monthly-cost", budgetType: "COST", timeUnit: "MONTHLY", budgetLimit: { amount: props.monthlyLimitUsd, unit: "USD", }, }, notificationsWithSubscribers: [ { notification: { notificationType: "ACTUAL", comparisonOperator: "GREATER_THAN", threshold: 80, thresholdType: "PERCENTAGE", }, subscribers: subscriber, }, { notification: { notificationType: "ACTUAL", comparisonOperator: "GREATER_THAN", threshold: 100, thresholdType: "PERCENTAGE", }, subscribers: subscriber, }, { notification: { notificationType: "FORECASTED", comparisonOperator: "GREATER_THAN", threshold: 100, thresholdType: "PERCENTAGE", }, subscribers: subscriber, }, ], }); cdk.Annotations.of(this).addInfo( "Budget alerts: 80%/100% actual + 100% forecast of $" + props.monthlyLimitUsd + " to " + props.alertEmail ); } }