The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as budgets from "@distilled.cloud/aws/budgets"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | |
| 7 | const { test } = Test.make({ providers: AWS.providers() }); |
| 8 | |
| 9 | const budgetName = "alchemy-test-budget-action"; |
| 10 | const execRoleName = "alchemy-test-budgets-action-exec"; |
| 11 | const targetRoleName = "alchemy-test-budgets-action-target"; |
| 12 | |
| 13 | const getAction = (accountId: string, actionId: string) => |
| 14 | budgets |
| 15 | .describeBudgetAction({ |
| 16 | AccountId: accountId, |
| 17 | BudgetName: budgetName, |
| 18 | ActionId: actionId, |
| 19 | }) |
| 20 | .pipe( |
| 21 | Effect.map((r) => r.Action), |
| 22 | Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)), |
| 23 | ); |
| 24 | |
| 25 | const deployAction = (thresholdValue: number) => |
| 26 | Effect.gen(function* () { |
| 27 | const execRole = yield* AWS.IAM.Role("BudgetActionExecRole", { |
| 28 | roleName: execRoleName, |
| 29 | assumeRolePolicyDocument: { |
| 30 | Version: "2012-10-17", |
| 31 | Statement: [ |
| 32 | { |
| 33 | Effect: "Allow", |
| 34 | Principal: { Service: "budgets.amazonaws.com" }, |
| 35 | Action: ["sts:AssumeRole"], |
| 36 | }, |
| 37 | ], |
| 38 | }, |
| 39 | managedPolicyArns: ["arn:aws:iam::aws:policy/AWSBudgetsActionsWithAWSResourceControlAccess"], |
| 40 | }); |
| 41 | |
| 42 | const targetRole = yield* AWS.IAM.Role("BudgetActionTargetRole", { |
| 43 | roleName: targetRoleName, |
| 44 | assumeRolePolicyDocument: { |
| 45 | Version: "2012-10-17", |
| 46 | Statement: [ |
| 47 | { |
| 48 | Effect: "Allow", |
| 49 | Principal: { Service: "ec2.amazonaws.com" }, |
| 50 | Action: ["sts:AssumeRole"], |
| 51 | }, |
| 52 | ], |
| 53 | }, |
| 54 | }); |
| 55 | |
| 56 | const budget = yield* AWS.Budgets.Budget("ActionBudget", { |
| 57 | budgetName, |
| 58 | budgetType: "COST", |
| 59 | timeUnit: "MONTHLY", |
| 60 | // Large limit so the 100% threshold is never crossed by real account |
| 61 | // spend — the action must stay in STANDBY. |
| 62 | budgetLimit: { amount: "1000000", unit: "USD" }, |
| 63 | }); |
| 64 | |
| 65 | const action = yield* AWS.Budgets.BudgetAction("FreezeAction", { |
| 66 | budgetName: budget.budgetName, |
| 67 | notificationType: "ACTUAL", |
| 68 | actionType: "APPLY_IAM_POLICY", |
| 69 | actionThreshold: { |
| 70 | actionThresholdValue: thresholdValue, |
| 71 | actionThresholdType: "PERCENTAGE", |
| 72 | }, |
| 73 | definition: { |
| 74 | iamActionDefinition: { |
| 75 | policyArn: "arn:aws:iam::aws:policy/AWSDenyAll", |
| 76 | roles: [targetRole.roleName], |
| 77 | }, |
| 78 | }, |
| 79 | executionRoleArn: execRole.roleArn, |
| 80 | approvalModel: "MANUAL", |
| 81 | subscribers: [{ subscriptionType: "EMAIL", address: "budget-test@example.com" }], |
| 82 | tags: { Team: "alchemy-test" }, |
| 83 | }); |
| 84 | |
| 85 | return { |
| 86 | accountId: action.accountId, |
| 87 | actionId: action.actionId, |
| 88 | actionArn: action.actionArn, |
| 89 | execRoleArn: execRole.roleArn, |
| 90 | }; |
| 91 | }); |
| 92 | |
| 93 | test.provider( |
| 94 | "lifecycle: create IAM budget action, update threshold, destroy", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | yield* stack.destroy(); |
| 98 | |
| 99 | const deployed = yield* stack.deploy(deployAction(100)); |
| 100 | const { accountId, actionId } = deployed; |
| 101 | |
| 102 | expect(deployed.actionArn).toBe( |
| 103 | `arn:aws:budgets::${accountId}:budget/${budgetName}/action/${actionId}`, |
| 104 | ); |
| 105 | |
| 106 | // Out-of-band verification via distilled. |
| 107 | const created = yield* getAction(accountId, actionId); |
| 108 | expect(created?.ActionType).toBe("APPLY_IAM_POLICY"); |
| 109 | expect(created?.Status).toBe("STANDBY"); |
| 110 | expect(created?.ApprovalModel).toBe("MANUAL"); |
| 111 | expect(created?.ExecutionRoleArn).toBe(deployed.execRoleArn); |
| 112 | expect(created?.ActionThreshold.ActionThresholdValue).toBe(100); |
| 113 | expect(created?.Definition.IamActionDefinition?.PolicyArn).toBe( |
| 114 | "arn:aws:iam::aws:policy/AWSDenyAll", |
| 115 | ); |
| 116 | expect(created?.Definition.IamActionDefinition?.Roles).toEqual([targetRoleName]); |
| 117 | |
| 118 | // Tags — user tag plus the internal alchemy brand. |
| 119 | const tags = yield* budgets |
| 120 | .listTagsForResource({ ResourceARN: deployed.actionArn }) |
| 121 | .pipe( |
| 122 | Effect.map((r) => |
| 123 | Object.fromEntries((r.ResourceTags ?? []).map((t) => [t.Key, t.Value])), |
| 124 | ), |
| 125 | ); |
| 126 | expect(tags.Team).toBe("alchemy-test"); |
| 127 | expect(Object.keys(tags).some((k) => k.startsWith("alchemy:"))).toBe(true); |
| 128 | |
| 129 | // Update — lower the threshold; the action id must be stable. |
| 130 | const updated = yield* stack.deploy(deployAction(90)); |
| 131 | expect(updated.actionId).toBe(actionId); |
| 132 | |
| 133 | const afterUpdate = yield* getAction(accountId, actionId); |
| 134 | expect(afterUpdate?.ActionThreshold.ActionThresholdValue).toBe(90); |
| 135 | |
| 136 | // Destroy — the action (and budget) are gone. |
| 137 | yield* stack.destroy(); |
| 138 | const afterDestroy = yield* budgets |
| 139 | .describeBudgetAction({ |
| 140 | AccountId: accountId, |
| 141 | BudgetName: budgetName, |
| 142 | ActionId: actionId, |
| 143 | }) |
| 144 | .pipe( |
| 145 | Effect.map(() => "found"), |
| 146 | Effect.catchTag("NotFoundException", () => Effect.succeed("not-found")), |
| 147 | ); |
| 148 | expect(afterDestroy).toBe("not-found"); |
| 149 | }), |
| 150 | { |
| 151 | tags: ["provider:aws", "provider:aws:budgets", "provider:aws:iam", "live"], |
| 152 | timeout: 240_000, |
| 153 | }, |
| 154 | ); |