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 { Distribution, OriginAccessControl } from "@alchemy.run/aws/CloudFront"; |
| 3 | import type { PolicyStatement } from "@alchemy.run/aws/IAM/Policy"; |
| 4 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 5 | import { AssetDeployment } from "@alchemy.run/aws/Website/AssetDeployment"; |
| 6 | import { expect } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 9 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 10 | import * as Output from "alchemy/Output"; |
| 11 | import * as Provider from "alchemy/Provider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Path from "effect/Path"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: AWS.providers() }); |
| 17 | |
| 18 | test.provider.skipIf(!!process.env.FAST)( |
| 19 | "create invalidation with explicit paths and wait for completion", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const deployed = yield* stack.deploy( |
| 25 | Effect.gen(function* () { |
| 26 | const bucket = yield* Bucket("WebsiteBucket", { forceDestroy: true }); |
| 27 | const oac = yield* OriginAccessControl("WebsiteOac", { originType: "s3" }); |
| 28 | const distribution = yield* Distribution("WebsiteDistribution", { |
| 29 | origins: [ |
| 30 | { |
| 31 | id: "site", |
| 32 | domainName: bucket.bucketRegionalDomainName, |
| 33 | s3Origin: true, |
| 34 | originAccessControlId: oac.originAccessControlId, |
| 35 | }, |
| 36 | ], |
| 37 | defaultRootObject: "index.html", |
| 38 | defaultCacheBehavior: { |
| 39 | targetOriginId: "site", |
| 40 | viewerProtocolPolicy: "redirect-to-https", |
| 41 | compress: true, |
| 42 | allowedMethods: ["GET", "HEAD"], |
| 43 | cachedMethods: ["GET", "HEAD"], |
| 44 | forwardedValues: { QueryString: false, Cookies: { Forward: "none" } }, |
| 45 | }, |
| 46 | }); |
| 47 | |
| 48 | const statement: PolicyStatement = { |
| 49 | Effect: "Allow", |
| 50 | Principal: { Service: "cloudfront.amazonaws.com" }, |
| 51 | Action: ["s3:GetObject"], |
| 52 | Resource: [Output.interpolate`${bucket.bucketArn}/*` as any], |
| 53 | Condition: { StringEquals: { "AWS:SourceArn": distribution.distributionArn as any } }, |
| 54 | }; |
| 55 | |
| 56 | yield* bucket.bind`Allow(${distribution}, CloudFront.Read(${bucket}))`({ |
| 57 | policyStatements: [statement], |
| 58 | }); |
| 59 | |
| 60 | const invalidation = yield* AWS.CloudFront.Invalidation("InvalidateDocs", { |
| 61 | distributionId: distribution.distributionId, |
| 62 | version: "v2", |
| 63 | wait: true, |
| 64 | paths: ["/index.html", "/docs/*"], |
| 65 | }); |
| 66 | |
| 67 | return { bucket, distribution, invalidation }; |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | yield* S3.putObject({ |
| 72 | Bucket: deployed.bucket.bucketName, |
| 73 | Key: "index.html", |
| 74 | Body: "<html>ok</html>", |
| 75 | ContentType: "text/html; charset=utf-8", |
| 76 | }); |
| 77 | |
| 78 | const current = yield* cloudfront.getInvalidation({ |
| 79 | DistributionId: deployed.distribution.distributionId, |
| 80 | Id: deployed.invalidation.invalidationId, |
| 81 | }); |
| 82 | expect(current.Invalidation?.Status).toEqual("Completed"); |
| 83 | // CloudFront returns invalidation paths in arbitrary order. |
| 84 | expect([...(current.Invalidation?.InvalidationBatch?.Paths?.Items ?? [])].sort()).toEqual([ |
| 85 | "/docs/*", |
| 86 | "/index.html", |
| 87 | ]); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | yield* assertDistributionDeleted(deployed.distribution.distributionId); |
| 91 | }), |
| 92 | { |
| 93 | tags: [ |
| 94 | "provider:aws", |
| 95 | "provider:aws:cloudfront", |
| 96 | "provider:aws:iam", |
| 97 | "provider:aws:s3", |
| 98 | "live", |
| 99 | ], |
| 100 | timeout: 600_000, |
| 101 | }, |
| 102 | ); |
| 103 | |
| 104 | test.provider.skipIf(!!process.env.FAST)( |
| 105 | "issues a new invalidation when an upstream content version changes", |
| 106 | (stack) => |
| 107 | Effect.gen(function* () { |
| 108 | const path = yield* Path.Path; |
| 109 | const site = (version: "v1" | "v2") => |
| 110 | path.join(import.meta.dirname, "fixtures", `invalidation-site-${version}`); |
| 111 | |
| 112 | // `version` comes from an AssetDeployment, so on a content change it is |
| 113 | // unresolved at plan time and the Invalidation plans as an update rather |
| 114 | // than a replace — the documented "invalidate on content change" setup. |
| 115 | const deploySite = (sourcePath: string) => |
| 116 | stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | const bucket = yield* Bucket("SiteBucket", { forceDestroy: true }); |
| 119 | const distribution = yield* Distribution("SiteDistribution", { |
| 120 | origins: [ |
| 121 | { |
| 122 | id: "site", |
| 123 | domainName: bucket.bucketRegionalDomainName, |
| 124 | s3Origin: true, |
| 125 | }, |
| 126 | ], |
| 127 | defaultCacheBehavior: { |
| 128 | targetOriginId: "site", |
| 129 | viewerProtocolPolicy: "redirect-to-https", |
| 130 | allowedMethods: ["GET", "HEAD"], |
| 131 | cachedMethods: ["GET", "HEAD"], |
| 132 | forwardedValues: { |
| 133 | QueryString: false, |
| 134 | Cookies: { Forward: "none" }, |
| 135 | }, |
| 136 | }, |
| 137 | }); |
| 138 | const files = yield* AssetDeployment("SiteFiles", { |
| 139 | bucket, |
| 140 | sourcePath, |
| 141 | }); |
| 142 | const invalidation = yield* AWS.CloudFront.Invalidation("SiteInvalidation", { |
| 143 | distributionId: distribution.distributionId, |
| 144 | version: files.version, |
| 145 | }); |
| 146 | return { distribution, files, invalidation }; |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | |
| 152 | const first = yield* deploySite(site("v1")); |
| 153 | expect(first.invalidation.version).toEqual(first.files.version); |
| 154 | |
| 155 | const second = yield* deploySite(site("v2")); |
| 156 | expect(second.files.version).not.toEqual(first.files.version); |
| 157 | expect(second.invalidation.version).toEqual(second.files.version); |
| 158 | expect(second.invalidation.invalidationId).not.toEqual(first.invalidation.invalidationId); |
| 159 | |
| 160 | const issued = yield* cloudfront.getInvalidation({ |
| 161 | DistributionId: second.distribution.distributionId, |
| 162 | Id: second.invalidation.invalidationId, |
| 163 | }); |
| 164 | expect(issued.Invalidation?.InvalidationBatch?.CallerReference).toEqual(second.files.version); |
| 165 | |
| 166 | yield* stack.destroy(); |
| 167 | yield* assertDistributionDeleted(second.distribution.distributionId); |
| 168 | }), |
| 169 | { |
| 170 | tags: [ |
| 171 | "provider:aws", |
| 172 | "provider:aws:cloudfront", |
| 173 | "provider:aws:s3", |
| 174 | "provider:aws:website", |
| 175 | "live", |
| 176 | ], |
| 177 | timeout: 600_000, |
| 178 | }, |
| 179 | ); |
| 180 | |
| 181 | test.provider( |
| 182 | "list returns [] for the non-listable ephemeral invalidation", |
| 183 | () => |
| 184 | Effect.gen(function* () { |
| 185 | const provider = yield* Provider.findProvider(AWS.CloudFront.Invalidation); |
| 186 | const all = yield* provider.list(); |
| 187 | expect(all).toEqual([]); |
| 188 | }), |
| 189 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"] }, |
| 190 | ); |
| 191 | |
| 192 | const assertDistributionDeleted = (distributionId: string) => |
| 193 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 194 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 195 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 196 | Effect.retry({ |
| 197 | while: (error) => error instanceof Error && error.message === "DistributionStillExists", |
| 198 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 199 | }), |
| 200 | ); |