The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { isResolved } from "alchemy/Diff"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type { Providers } from "../Providers.ts"; |
| 6 | import { describeCertificateDetail, waitForCertificateIssued } from "./Certificate.ts"; |
| 7 | |
| 8 | export interface CertificateValidationProps { |
| 9 | /** |
| 10 | * ARN of the ACM certificate to wait for. Changing it replaces the |
| 11 | * validation. |
| 12 | */ |
| 13 | certificateArn: string; |
| 14 | } |
| 15 | |
| 16 | export interface CertificateValidation extends Resource< |
| 17 | "AWS.ACM.CertificateValidation", |
| 18 | CertificateValidationProps, |
| 19 | { |
| 20 | /** ARN of the issued certificate. */ |
| 21 | certificateArn: string; |
| 22 | /** Certificate status — `ISSUED` once reconciled. */ |
| 23 | status: string | undefined; |
| 24 | /** When the certificate was issued. */ |
| 25 | issuedAt: Date | undefined; |
| 26 | /** When the certificate expires. */ |
| 27 | notAfter: Date | undefined; |
| 28 | }, |
| 29 | never, |
| 30 | Providers |
| 31 | > {} |
| 32 | |
| 33 | /** |
| 34 | * Waits for a DNS-validated ACM certificate to be issued. |
| 35 | * |
| 36 | * Pair it with an `AWS.ACM.Certificate` requested with |
| 37 | * `dnsValidation: "external"` whose validation records are published |
| 38 | * elsewhere — typically through a DNS adapter (see |
| 39 | * [DNS Adapters](/infrastructure-as-code/dns-adapters)). Resources that |
| 40 | * need an issued certificate (a CloudFront distribution, an HTTPS |
| 41 | * listener, an API Gateway domain) take this resource's `certificateArn`, |
| 42 | * so they deploy only after issuance. Deleting it deletes nothing. |
| 43 | * |
| 44 | * A `CAA_ERROR` fails with `CertificateCaaError`, naming the CAA records to |
| 45 | * add. Issuance is polled for up to ten minutes. |
| 46 | * ### Waiting For Issuance |
| 47 | * **Example:** Certificate Validated Through Cloudflare DNS |
| 48 | * ```typescript |
| 49 | * const cert = yield* AWS.ACM.Certificate("Cert", { |
| 50 | * domainName: "app.example.com", |
| 51 | * dnsValidation: "external", |
| 52 | * }); |
| 53 | * yield* Cloudflare.DNS.RecordList("CertValidation", { |
| 54 | * records: cert.domainValidationOptions.pipe( |
| 55 | * Output.map(AWS.ACM.validationRecordsOf), |
| 56 | * ), |
| 57 | * }); |
| 58 | * const issued = yield* AWS.ACM.CertificateValidation("CertIssued", { |
| 59 | * certificateArn: cert.certificateArn, |
| 60 | * }); |
| 61 | * // issued.certificateArn is safe to attach to a distribution or listener. |
| 62 | * ``` |
| 63 | * |
| 64 | * @resource |
| 65 | */ |
| 66 | export const CertificateValidation = Resource<CertificateValidation>( |
| 67 | "AWS.ACM.CertificateValidation", |
| 68 | ); |
| 69 | |
| 70 | export const CertificateValidationProvider = () => |
| 71 | Provider.succeed(CertificateValidation, { |
| 72 | stables: ["certificateArn"], |
| 73 | diff: Effect.fn(function* ({ olds, news }) { |
| 74 | if (!isResolved(news)) return undefined; |
| 75 | if (olds.certificateArn !== news.certificateArn) { |
| 76 | return { action: "replace" } as const; |
| 77 | } |
| 78 | }), |
| 79 | read: Effect.fn(function* ({ output }) { |
| 80 | if (output === undefined) return undefined; |
| 81 | const detail = yield* describeCertificateDetail(output.certificateArn); |
| 82 | if (detail === undefined) return undefined; |
| 83 | return { |
| 84 | certificateArn: output.certificateArn, |
| 85 | status: detail.Status, |
| 86 | issuedAt: detail.IssuedAt, |
| 87 | notAfter: detail.NotAfter, |
| 88 | }; |
| 89 | }), |
| 90 | reconcile: Effect.fn(function* ({ news, session }) { |
| 91 | const detail = yield* waitForCertificateIssued(news.certificateArn); |
| 92 | yield* session.note(`${news.certificateArn} ${detail.Status}`); |
| 93 | return { |
| 94 | certificateArn: news.certificateArn, |
| 95 | status: detail.Status, |
| 96 | issuedAt: detail.IssuedAt, |
| 97 | notAfter: detail.NotAfter, |
| 98 | }; |
| 99 | }), |
| 100 | // Existence-only: the certificate belongs to `AWS.ACM.Certificate`. |
| 101 | delete: () => Effect.void, |
| 102 | }); |