The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as CodeDeploy from "@alchemy.run/aws/CodeDeploy"; |
| 2 | import * as IAM from "@alchemy.run/aws/IAM"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 13 | |
| 14 | export const FIXTURE_APPLICATION_NAME = "alchemy-test-codedeploy-bindings-app"; |
| 15 | export const FIXTURE_GROUP_NAME = "alchemy-test-codedeploy-bindings-dg"; |
| 16 | |
| 17 | /** A syntactically valid but nonexistent revision to address revision ops. */ |
| 18 | const FIXTURE_REVISION = { |
| 19 | revisionType: "S3", |
| 20 | s3Location: { |
| 21 | bucket: "alchemy-test-codedeploy-bindings-nonexistent", |
| 22 | key: "app.zip", |
| 23 | bundleType: "zip", |
| 24 | }, |
| 25 | } as const; |
| 26 | |
| 27 | export class CodeDeployTestFunction extends Lambda.Function<Lambda.Function>()( |
| 28 | "CodeDeployTestFunction", |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * Every route answers `{ …fields }` on success or `{ errorTag }` when the |
| 33 | * operation fails with a TYPED error — the test asserts the tag is a typed, |
| 34 | * non-authorization tag, which proves both the binding wiring and the IAM |
| 35 | * grant. An untyped error crashes into a 500 instead. |
| 36 | */ |
| 37 | const errorTagged = <A, E extends { _tag: string }, R>( |
| 38 | effect: Effect.Effect<A, E, R>, |
| 39 | ): Effect.Effect<A | { errorTag: string }, never, R> => |
| 40 | effect.pipe( |
| 41 | Effect.map((a): A | { errorTag: string } => a), |
| 42 | Effect.catch((e) => Effect.logError(e).pipe(Effect.as({ errorTag: e._tag }))), |
| 43 | ); |
| 44 | |
| 45 | export default CodeDeployTestFunction.make( |
| 46 | { |
| 47 | main, |
| 48 | functionUrl: true, |
| 49 | // Deployment ops fan out SDK calls — AWS's 3s default intermittently |
| 50 | // times out under cold starts. |
| 51 | timeout: Duration.seconds(30), |
| 52 | }, |
| 53 | Effect.gen(function* () { |
| 54 | const role = yield* IAM.Role("CodeDeployBindingsRole", { |
| 55 | assumeRolePolicyDocument: { |
| 56 | Version: "2012-10-17", |
| 57 | Statement: [ |
| 58 | { |
| 59 | Effect: "Allow", |
| 60 | Principal: { Service: "codedeploy.amazonaws.com" }, |
| 61 | Action: ["sts:AssumeRole"], |
| 62 | }, |
| 63 | ], |
| 64 | }, |
| 65 | managedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda"], |
| 66 | }); |
| 67 | |
| 68 | const app = yield* CodeDeploy.Application("BindingsApp", { |
| 69 | applicationName: FIXTURE_APPLICATION_NAME, |
| 70 | computePlatform: "Lambda", |
| 71 | }); |
| 72 | |
| 73 | const group = yield* CodeDeploy.DeploymentGroup("BindingsGroup", { |
| 74 | applicationName: app.applicationName, |
| 75 | deploymentGroupName: FIXTURE_GROUP_NAME, |
| 76 | serviceRoleArn: role.roleArn, |
| 77 | deploymentConfigName: "CodeDeployDefault.LambdaAllAtOnce", |
| 78 | deploymentStyle: { |
| 79 | deploymentType: "BLUE_GREEN", |
| 80 | deploymentOption: "WITH_TRAFFIC_CONTROL", |
| 81 | }, |
| 82 | }); |
| 83 | |
| 84 | // Deployment plane |
| 85 | const createDeployment = yield* CodeDeploy.CreateDeployment(group); |
| 86 | const getDeployment = yield* CodeDeploy.GetDeployment(group); |
| 87 | const batchGetDeployments = yield* CodeDeploy.BatchGetDeployments(group); |
| 88 | const listDeployments = yield* CodeDeploy.ListDeployments(group); |
| 89 | const stopDeployment = yield* CodeDeploy.StopDeployment(group); |
| 90 | const continueDeployment = yield* CodeDeploy.ContinueDeployment(group); |
| 91 | const putHookStatus = yield* CodeDeploy.PutLifecycleEventHookExecutionStatus(group); |
| 92 | // Target plane |
| 93 | const getDeploymentTarget = yield* CodeDeploy.GetDeploymentTarget(group); |
| 94 | const listDeploymentTargets = yield* CodeDeploy.ListDeploymentTargets(group); |
| 95 | const batchGetDeploymentTargets = yield* CodeDeploy.BatchGetDeploymentTargets(group); |
| 96 | // Revision plane |
| 97 | const registerApplicationRevision = yield* CodeDeploy.RegisterApplicationRevision(app); |
| 98 | const getApplicationRevision = yield* CodeDeploy.GetApplicationRevision(app); |
| 99 | const listApplicationRevisions = yield* CodeDeploy.ListApplicationRevisions(app); |
| 100 | const batchGetApplicationRevisions = yield* CodeDeploy.BatchGetApplicationRevisions(app); |
| 101 | |
| 102 | // Deploy-time: creates the EventBridge rule (default bus, source |
| 103 | // aws.codedeploy) targeting this Function. Runtime firing rides on real |
| 104 | // deployments; the test verifies the rule deploys. |
| 105 | yield* CodeDeploy.consumeDeploymentEvents( |
| 106 | { kinds: ["deployment"], applications: [FIXTURE_APPLICATION_NAME] }, |
| 107 | (events) => |
| 108 | Stream.runForEach(events, (event) => |
| 109 | Effect.log(`codedeploy event: ${event.detail.deploymentId} -> ${event.detail.state}`), |
| 110 | ), |
| 111 | ); |
| 112 | |
| 113 | return { |
| 114 | fetch: Effect.gen(function* () { |
| 115 | const request = yield* HttpServerRequest; |
| 116 | const url = new URL(request.originalUrl); |
| 117 | const pathname = url.pathname; |
| 118 | const route = `${request.method} ${pathname}`; |
| 119 | const param = (name: string) => url.searchParams.get(name)!; |
| 120 | |
| 121 | switch (route) { |
| 122 | // ---- deployment plane ---- |
| 123 | case "POST /deployment/create": { |
| 124 | // No revision on purpose — CodeDeploy must answer with the |
| 125 | // TYPED RevisionRequiredException, which proves the binding |
| 126 | // wiring, the name injection, and the IAM grant. |
| 127 | const result = yield* errorTagged(createDeployment()); |
| 128 | return yield* HttpServerResponse.json( |
| 129 | "errorTag" in result ? result : { deploymentId: result.deploymentId }, |
| 130 | ); |
| 131 | } |
| 132 | case "GET /deployment/get": { |
| 133 | const result = yield* errorTagged(getDeployment({ deploymentId: param("id") })); |
| 134 | return yield* HttpServerResponse.json( |
| 135 | "errorTag" in result ? result : { status: result.deploymentInfo?.status }, |
| 136 | ); |
| 137 | } |
| 138 | case "GET /deployment/batch-get": { |
| 139 | const result = yield* errorTagged( |
| 140 | batchGetDeployments({ deploymentIds: [param("id")] }), |
| 141 | ); |
| 142 | return yield* HttpServerResponse.json( |
| 143 | "errorTag" in result ? result : { count: (result.deploymentsInfo ?? []).length }, |
| 144 | ); |
| 145 | } |
| 146 | case "GET /deployment/list": { |
| 147 | const result = yield* errorTagged(listDeployments()); |
| 148 | return yield* HttpServerResponse.json( |
| 149 | "errorTag" in result ? result : { deployments: result.deployments ?? [] }, |
| 150 | ); |
| 151 | } |
| 152 | case "POST /deployment/stop": { |
| 153 | const body = (yield* request.json) as unknown as { id: string }; |
| 154 | const result = yield* errorTagged(stopDeployment({ deploymentId: body.id })); |
| 155 | return yield* HttpServerResponse.json( |
| 156 | "errorTag" in result ? result : { status: result.status }, |
| 157 | ); |
| 158 | } |
| 159 | case "POST /deployment/continue": { |
| 160 | const body = (yield* request.json) as unknown as { id: string }; |
| 161 | const result = yield* errorTagged(continueDeployment({ deploymentId: body.id })); |
| 162 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 163 | } |
| 164 | case "POST /hook/status": { |
| 165 | const body = (yield* request.json) as unknown as { |
| 166 | deploymentId: string; |
| 167 | executionId: string; |
| 168 | }; |
| 169 | const result = yield* errorTagged( |
| 170 | putHookStatus({ |
| 171 | deploymentId: body.deploymentId, |
| 172 | lifecycleEventHookExecutionId: body.executionId, |
| 173 | status: "Succeeded", |
| 174 | }), |
| 175 | ); |
| 176 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 177 | } |
| 178 | |
| 179 | // ---- target plane ---- |
| 180 | case "GET /target/get": { |
| 181 | const result = yield* errorTagged( |
| 182 | getDeploymentTarget({ |
| 183 | deploymentId: param("deploymentId"), |
| 184 | targetId: param("targetId"), |
| 185 | }), |
| 186 | ); |
| 187 | return yield* HttpServerResponse.json( |
| 188 | "errorTag" in result |
| 189 | ? result |
| 190 | : { status: result.deploymentTarget?.lambdaTarget?.status }, |
| 191 | ); |
| 192 | } |
| 193 | case "GET /target/list": { |
| 194 | const result = yield* errorTagged( |
| 195 | listDeploymentTargets({ deploymentId: param("deploymentId") }), |
| 196 | ); |
| 197 | return yield* HttpServerResponse.json( |
| 198 | "errorTag" in result ? result : { targetIds: result.targetIds ?? [] }, |
| 199 | ); |
| 200 | } |
| 201 | case "GET /target/batch-get": { |
| 202 | const result = yield* errorTagged( |
| 203 | batchGetDeploymentTargets({ |
| 204 | deploymentId: param("deploymentId"), |
| 205 | targetIds: [param("targetId")], |
| 206 | }), |
| 207 | ); |
| 208 | return yield* HttpServerResponse.json( |
| 209 | "errorTag" in result ? result : { count: (result.deploymentTargets ?? []).length }, |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | // ---- revision plane ---- |
| 214 | case "POST /revision/register": { |
| 215 | const result = yield* errorTagged( |
| 216 | registerApplicationRevision({ |
| 217 | revision: FIXTURE_REVISION, |
| 218 | description: "alchemy CodeDeploy bindings fixture revision", |
| 219 | }), |
| 220 | ); |
| 221 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 222 | } |
| 223 | case "GET /revision/get": { |
| 224 | const result = yield* errorTagged( |
| 225 | getApplicationRevision({ revision: FIXTURE_REVISION }), |
| 226 | ); |
| 227 | return yield* HttpServerResponse.json( |
| 228 | "errorTag" in result |
| 229 | ? result |
| 230 | : { |
| 231 | description: result.revisionInfo?.description, |
| 232 | }, |
| 233 | ); |
| 234 | } |
| 235 | case "GET /revision/list": { |
| 236 | const result = yield* errorTagged(listApplicationRevisions()); |
| 237 | return yield* HttpServerResponse.json( |
| 238 | "errorTag" in result ? result : { count: (result.revisions ?? []).length }, |
| 239 | ); |
| 240 | } |
| 241 | case "GET /revision/batch-get": { |
| 242 | const result = yield* errorTagged( |
| 243 | batchGetApplicationRevisions({ |
| 244 | revisions: [FIXTURE_REVISION], |
| 245 | }), |
| 246 | ); |
| 247 | return yield* HttpServerResponse.json( |
| 248 | "errorTag" in result ? result : { count: (result.revisions ?? []).length }, |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | default: |
| 253 | return yield* HttpServerResponse.json({ error: "Not found", route }, { status: 404 }); |
| 254 | } |
| 255 | }).pipe(Effect.orDie), |
| 256 | }; |
| 257 | }).pipe( |
| 258 | Effect.provide( |
| 259 | Layer.mergeAll( |
| 260 | Lambda.EventSource, |
| 261 | CodeDeploy.CreateDeploymentHttp, |
| 262 | CodeDeploy.GetDeploymentHttp, |
| 263 | CodeDeploy.BatchGetDeploymentsHttp, |
| 264 | CodeDeploy.ListDeploymentsHttp, |
| 265 | CodeDeploy.StopDeploymentHttp, |
| 266 | CodeDeploy.ContinueDeploymentHttp, |
| 267 | CodeDeploy.PutLifecycleEventHookExecutionStatusHttp, |
| 268 | CodeDeploy.GetDeploymentTargetHttp, |
| 269 | CodeDeploy.ListDeploymentTargetsHttp, |
| 270 | CodeDeploy.BatchGetDeploymentTargetsHttp, |
| 271 | CodeDeploy.RegisterApplicationRevisionHttp, |
| 272 | CodeDeploy.GetApplicationRevisionHttp, |
| 273 | CodeDeploy.ListApplicationRevisionsHttp, |
| 274 | CodeDeploy.BatchGetApplicationRevisionsHttp, |
| 275 | ), |
| 276 | ), |
| 277 | ), |
| 278 | ); |