The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ag from "@distilled.cloud/aws/api-gateway"; |
| 2 | import { deepEqual, isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import { createInternalTags, tagRecord } from "alchemy/Tags"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { plainJson } from "../internal/Observe.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { syncTags } from "./common.ts"; |
| 13 | |
| 14 | export interface DomainNameProps { |
| 15 | /** The custom domain name (e.g. `api.example.com`). */ |
| 16 | domainName: string; |
| 17 | /** Display name of a self-managed edge certificate. */ |
| 18 | certificateName?: string; |
| 19 | /** PEM body of a self-managed edge certificate. */ |
| 20 | certificateBody?: string; |
| 21 | /** |
| 22 | * The private key of the server certificate (self-managed certificates). |
| 23 | * Secret key material — wrap with `Redacted.make` so state encoding |
| 24 | * preserves redaction. |
| 25 | */ |
| 26 | certificatePrivateKey?: Redacted.Redacted<string>; |
| 27 | /** PEM chain of a self-managed edge certificate. */ |
| 28 | certificateChain?: string; |
| 29 | /** ARN of an ACM certificate for EDGE endpoints (must live in us-east-1). */ |
| 30 | certificateArn?: string; |
| 31 | /** Display name of the regional certificate. */ |
| 32 | regionalCertificateName?: string; |
| 33 | /** ARN of an ACM certificate for REGIONAL endpoints (same region as the API). */ |
| 34 | regionalCertificateArn?: string; |
| 35 | /** Endpoint type for the domain (EDGE, REGIONAL, or PRIVATE). */ |
| 36 | endpointConfiguration?: ag.EndpointConfiguration; |
| 37 | /** |
| 38 | * Minimum TLS version served (e.g. `TLS_1_2`). |
| 39 | * @default "TLS_1_2" |
| 40 | */ |
| 41 | securityPolicy?: ag.SecurityPolicy; |
| 42 | /** Access mode of the domain name endpoint. */ |
| 43 | endpointAccessMode?: ag.EndpointAccessMode; |
| 44 | /** Mutual TLS (client certificate) authentication configuration. */ |
| 45 | mutualTlsAuthentication?: ag.MutualTlsAuthenticationInput; |
| 46 | /** ARN of the certificate proving domain ownership when mutual TLS is enabled. */ |
| 47 | ownershipVerificationCertificateArn?: string; |
| 48 | /** Resource policy (JSON) for private custom domain names. */ |
| 49 | policy?: string; |
| 50 | /** Routing mode of the domain name (base-path vs routing-rule based). */ |
| 51 | routingMode?: ag.RoutingMode; |
| 52 | /** User-defined tags for the domain name. */ |
| 53 | tags?: Record<string, string>; |
| 54 | } |
| 55 | |
| 56 | /** @resource */ |
| 57 | export interface DomainName extends Resource< |
| 58 | "AWS.ApiGateway.DomainName", |
| 59 | DomainNameProps, |
| 60 | { |
| 61 | domainName: string; |
| 62 | regionalDomainName: string | undefined; |
| 63 | regionalHostedZoneId: string | undefined; |
| 64 | distributionDomainName: string | undefined; |
| 65 | distributionHostedZoneId: string | undefined; |
| 66 | domainNameArn: string | undefined; |
| 67 | tags: Record<string, string>; |
| 68 | /** |
| 69 | * Certificates, TLS policy, endpoint configuration and mutual TLS |
| 70 | * settings, as observed. Lets `alchemy drift` detect out-of-band |
| 71 | * changes. |
| 72 | */ |
| 73 | configuration?: { |
| 74 | certificateArn?: string; |
| 75 | regionalCertificateArn?: string; |
| 76 | securityPolicy?: string; |
| 77 | endpointTypes?: string[]; |
| 78 | ipAddressType?: string; |
| 79 | mutualTlsAuthentication?: { truststoreUri?: string; truststoreVersion?: string }; |
| 80 | }; |
| 81 | }, |
| 82 | never, |
| 83 | Providers |
| 84 | > {} |
| 85 | |
| 86 | /** |
| 87 | * Custom domain name for an Amazon API Gateway REST API. |
| 88 | * |
| 89 | * ### Custom domain |
| 90 | * **Example:** Regional custom domain |
| 91 | * ```typescript |
| 92 | * const domain = yield* ApiGateway.DomainName("ApiDomain", { |
| 93 | * domainName: "api.example.com", |
| 94 | * regionalCertificateArn: cert.certificateArn, |
| 95 | * endpointConfiguration: { types: ["REGIONAL"] }, |
| 96 | * securityPolicy: "TLS_1_2", |
| 97 | * }); |
| 98 | * ``` |
| 99 | */ |
| 100 | const DomainNameResource = Resource<DomainName>("AWS.ApiGateway.DomainName"); |
| 101 | |
| 102 | export { DomainNameResource as DomainName }; |
| 103 | |
| 104 | /** Unwrap an optional redacted secret for the wire / diff comparison. */ |
| 105 | const redactedValue = (value: Redacted.Redacted<string> | undefined) => |
| 106 | value === undefined ? undefined : Redacted.value(value); |
| 107 | |
| 108 | const retryDomainNameMutation = Effect.retry({ |
| 109 | while: (e: any) => e._tag === "ConflictException" || e._tag === "TooManyRequestsException", |
| 110 | schedule: Schedule.spaced("1 second"), |
| 111 | times: 8, |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * The settings `read` and `reconcile` both report, so a converged domain |
| 116 | * name reads unchanged. |
| 117 | */ |
| 118 | const observeDomainNameConfiguration = (d: ag.DomainName) => |
| 119 | plainJson({ |
| 120 | certificateArn: d.certificateArn, |
| 121 | regionalCertificateArn: d.regionalCertificateArn, |
| 122 | securityPolicy: d.securityPolicy, |
| 123 | endpointTypes: d.endpointConfiguration?.types, |
| 124 | ipAddressType: d.endpointConfiguration?.ipAddressType, |
| 125 | mutualTlsAuthentication: d.mutualTlsAuthentication?.truststoreUri |
| 126 | ? { |
| 127 | truststoreUri: d.mutualTlsAuthentication.truststoreUri, |
| 128 | truststoreVersion: d.mutualTlsAuthentication.truststoreVersion, |
| 129 | } |
| 130 | : undefined, |
| 131 | }); |
| 132 | |
| 133 | export const DomainNameProvider = () => |
| 134 | Provider.effect( |
| 135 | DomainNameResource, |
| 136 | Effect.gen(function* () { |
| 137 | return { |
| 138 | stables: ["domainName"] as const, |
| 139 | diff: Effect.fn(function* ({ news: newsIn, olds }) { |
| 140 | if (!isResolved(newsIn)) return; |
| 141 | const news = newsIn as DomainNameProps; |
| 142 | if (news.domainName !== olds.domainName) { |
| 143 | return { action: "replace" } as const; |
| 144 | } |
| 145 | if (!deepEqual(news.endpointConfiguration, olds.endpointConfiguration)) { |
| 146 | return { action: "replace" } as const; |
| 147 | } |
| 148 | if (news.endpointAccessMode !== olds.endpointAccessMode) { |
| 149 | return { action: "replace" } as const; |
| 150 | } |
| 151 | if (news.certificateBody !== olds.certificateBody) { |
| 152 | return { action: "replace" } as const; |
| 153 | } |
| 154 | if ( |
| 155 | redactedValue(news.certificatePrivateKey) !== redactedValue(olds.certificatePrivateKey) |
| 156 | ) { |
| 157 | return { action: "replace" } as const; |
| 158 | } |
| 159 | if (news.certificateChain !== olds.certificateChain) { |
| 160 | return { action: "replace" } as const; |
| 161 | } |
| 162 | if (news.certificateName !== olds.certificateName) { |
| 163 | return { action: "replace" } as const; |
| 164 | } |
| 165 | if (news.regionalCertificateName !== olds.regionalCertificateName) { |
| 166 | return { action: "replace" } as const; |
| 167 | } |
| 168 | if (!deepEqual(news.mutualTlsAuthentication, olds.mutualTlsAuthentication)) { |
| 169 | return { action: "replace" } as const; |
| 170 | } |
| 171 | if (news.policy !== olds.policy) { |
| 172 | return { action: "replace" } as const; |
| 173 | } |
| 174 | if (news.routingMode !== olds.routingMode) { |
| 175 | return { action: "replace" } as const; |
| 176 | } |
| 177 | if ( |
| 178 | news.ownershipVerificationCertificateArn !== olds.ownershipVerificationCertificateArn |
| 179 | ) { |
| 180 | return { action: "replace" } as const; |
| 181 | } |
| 182 | }), |
| 183 | list: () => |
| 184 | ag.getDomainNames.pages({}).pipe( |
| 185 | Stream.runCollect, |
| 186 | Effect.map((chunk) => |
| 187 | Array.from(chunk).flatMap((page) => |
| 188 | (page.items ?? []) |
| 189 | .filter((d): d is ag.DomainName & { domainName: string } => d.domainName != null) |
| 190 | .map((d) => ({ |
| 191 | domainName: d.domainName, |
| 192 | regionalDomainName: d.regionalDomainName, |
| 193 | regionalHostedZoneId: d.regionalHostedZoneId, |
| 194 | distributionDomainName: d.distributionDomainName, |
| 195 | distributionHostedZoneId: d.distributionHostedZoneId, |
| 196 | domainNameArn: d.domainNameArn, |
| 197 | tags: tagRecord(d.tags), |
| 198 | })), |
| 199 | ), |
| 200 | ), |
| 201 | ), |
| 202 | read: Effect.fn(function* ({ output }) { |
| 203 | if (!output?.domainName) return undefined; |
| 204 | const d = yield* ag |
| 205 | .getDomainName({ domainName: output.domainName }) |
| 206 | .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined)); |
| 207 | if (!d?.domainName) return undefined; |
| 208 | return { |
| 209 | domainName: d.domainName, |
| 210 | regionalDomainName: d.regionalDomainName, |
| 211 | regionalHostedZoneId: d.regionalHostedZoneId, |
| 212 | distributionDomainName: d.distributionDomainName, |
| 213 | distributionHostedZoneId: d.distributionHostedZoneId, |
| 214 | domainNameArn: d.domainNameArn, |
| 215 | tags: tagRecord(d.tags), |
| 216 | configuration: observeDomainNameConfiguration(d), |
| 217 | }; |
| 218 | }), |
| 219 | reconcile: Effect.fn(function* ({ id, news: newsIn, output, session }) { |
| 220 | if (!isResolved(newsIn)) { |
| 221 | return yield* Effect.die("DomainName props were not resolved"); |
| 222 | } |
| 223 | const news = newsIn as DomainNameProps; |
| 224 | const domainName = output?.domainName ?? news.domainName; |
| 225 | const internalTags = yield* createInternalTags(id); |
| 226 | const desiredTags = { ...news.tags, ...internalTags }; |
| 227 | |
| 228 | // Observe — fetch the live domain name. Domain names are |
| 229 | // user-supplied physical names so the lookup key is stable; we |
| 230 | // never trust `output.tags`/`output.securityPolicy` etc. for |
| 231 | // diffing, only re-read the cloud state. |
| 232 | let observed = yield* ag |
| 233 | .getDomainName({ domainName }) |
| 234 | .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined)); |
| 235 | |
| 236 | // Ensure — create the domain name if it's missing. |
| 237 | if (!observed?.domainName) { |
| 238 | yield* ag.createDomainName({ |
| 239 | domainName: news.domainName, |
| 240 | certificateName: news.certificateName, |
| 241 | certificateBody: news.certificateBody, |
| 242 | certificatePrivateKey: redactedValue(news.certificatePrivateKey), |
| 243 | certificateChain: news.certificateChain, |
| 244 | certificateArn: news.certificateArn, |
| 245 | regionalCertificateName: news.regionalCertificateName, |
| 246 | regionalCertificateArn: news.regionalCertificateArn, |
| 247 | endpointConfiguration: news.endpointConfiguration, |
| 248 | tags: desiredTags, |
| 249 | securityPolicy: news.securityPolicy, |
| 250 | endpointAccessMode: news.endpointAccessMode, |
| 251 | mutualTlsAuthentication: news.mutualTlsAuthentication, |
| 252 | ownershipVerificationCertificateArn: news.ownershipVerificationCertificateArn, |
| 253 | policy: news.policy, |
| 254 | routingMode: news.routingMode, |
| 255 | }); |
| 256 | yield* session.note(`Created domain name ${news.domainName}`); |
| 257 | observed = yield* ag.getDomainName({ domainName }); |
| 258 | } |
| 259 | |
| 260 | // Sync mutable scalar fields — observed ↔ desired patch list. |
| 261 | const patches: ag.PatchOperation[] = []; |
| 262 | if (news.securityPolicy !== observed.securityPolicy) { |
| 263 | patches.push({ |
| 264 | op: news.securityPolicy === undefined ? "remove" : "replace", |
| 265 | path: "/securityPolicy", |
| 266 | value: news.securityPolicy, |
| 267 | }); |
| 268 | } |
| 269 | if (news.regionalCertificateArn !== observed.regionalCertificateArn) { |
| 270 | patches.push({ |
| 271 | op: news.regionalCertificateArn === undefined ? "remove" : "replace", |
| 272 | path: "/regionalCertificateArn", |
| 273 | value: news.regionalCertificateArn, |
| 274 | }); |
| 275 | } |
| 276 | if (news.certificateArn !== observed.certificateArn) { |
| 277 | patches.push({ |
| 278 | op: news.certificateArn === undefined ? "remove" : "replace", |
| 279 | path: "/certificateArn", |
| 280 | value: news.certificateArn, |
| 281 | }); |
| 282 | } |
| 283 | // The endpoint type and mutual TLS trust store are replacement |
| 284 | // props, so they only differ here after an out-of-band change. |
| 285 | const desiredType = news.endpointConfiguration?.types?.[0]; |
| 286 | const observedType = observed.endpointConfiguration?.types?.[0]; |
| 287 | if ( |
| 288 | desiredType !== undefined && |
| 289 | observedType !== undefined && |
| 290 | desiredType !== observedType |
| 291 | ) { |
| 292 | patches.push({ |
| 293 | op: "replace", |
| 294 | path: `/endpointConfiguration/types/${observedType}`, |
| 295 | value: desiredType, |
| 296 | }); |
| 297 | } |
| 298 | const desiredTrustStore = news.mutualTlsAuthentication; |
| 299 | if (desiredTrustStore?.truststoreUri !== undefined) { |
| 300 | if ( |
| 301 | desiredTrustStore.truststoreUri !== observed.mutualTlsAuthentication?.truststoreUri |
| 302 | ) { |
| 303 | patches.push({ |
| 304 | op: "replace", |
| 305 | path: "/mutualTlsAuthentication/truststoreUri", |
| 306 | value: desiredTrustStore.truststoreUri, |
| 307 | }); |
| 308 | } |
| 309 | if ( |
| 310 | desiredTrustStore.truststoreVersion !== undefined && |
| 311 | desiredTrustStore.truststoreVersion !== |
| 312 | observed.mutualTlsAuthentication?.truststoreVersion |
| 313 | ) { |
| 314 | patches.push({ |
| 315 | op: "replace", |
| 316 | path: "/mutualTlsAuthentication/truststoreVersion", |
| 317 | value: desiredTrustStore.truststoreVersion, |
| 318 | }); |
| 319 | } |
| 320 | } |
| 321 | if (patches.length > 0) { |
| 322 | // Domain name mutations can briefly conflict while API Gateway |
| 323 | // propagates certificate, policy, or routing changes. |
| 324 | yield* ag |
| 325 | .updateDomainName({ |
| 326 | domainName, |
| 327 | patchOperations: patches, |
| 328 | }) |
| 329 | .pipe(retryDomainNameMutation); |
| 330 | } |
| 331 | |
| 332 | // Sync tags — diff observed cloud tags against desired so adoption |
| 333 | // converges without fighting whatever was already there. |
| 334 | const observedTags = tagRecord(observed.tags); |
| 335 | if (!deepEqual(observedTags, desiredTags) && observed.domainNameArn) { |
| 336 | yield* syncTags({ |
| 337 | resourceArn: observed.domainNameArn, |
| 338 | oldTags: observedTags, |
| 339 | newTags: desiredTags, |
| 340 | }); |
| 341 | } |
| 342 | |
| 343 | yield* session.note(`Reconciled domain name ${domainName}`); |
| 344 | const final = yield* ag.getDomainName({ domainName }); |
| 345 | return { |
| 346 | domainName: final.domainName!, |
| 347 | regionalDomainName: final.regionalDomainName, |
| 348 | regionalHostedZoneId: final.regionalHostedZoneId, |
| 349 | distributionDomainName: final.distributionDomainName, |
| 350 | distributionHostedZoneId: final.distributionHostedZoneId, |
| 351 | domainNameArn: final.domainNameArn, |
| 352 | tags: tagRecord(final.tags), |
| 353 | configuration: observeDomainNameConfiguration(final), |
| 354 | }; |
| 355 | }), |
| 356 | delete: Effect.fn(function* ({ output, session }) { |
| 357 | yield* ag.deleteDomainName({ domainName: output.domainName }).pipe( |
| 358 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 359 | retryDomainNameMutation, |
| 360 | ); |
| 361 | yield* session.note(`Deleted domain name ${output.domainName}`); |
| 362 | }), |
| 363 | }; |
| 364 | }), |
| 365 | ); |