The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as imagebuilder from "@distilled.cloud/aws/imagebuilder"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | driftedFrom, |
| 14 | imageBuilderArn, |
| 15 | retryWhileDependedOn, |
| 16 | syncImageBuilderTags, |
| 17 | toTagRecord, |
| 18 | } from "./internal.ts"; |
| 19 | |
| 20 | export class DistributionConfigurationUnexpectedResponse extends Data.TaggedError( |
| 21 | "DistributionConfigurationUnexpectedResponse", |
| 22 | )<{ |
| 23 | readonly message: string; |
| 24 | readonly cause?: unknown; |
| 25 | }> {} |
| 26 | |
| 27 | export interface DistributionConfigurationProps { |
| 28 | /** |
| 29 | * Name of the distribution configuration. If omitted, a deterministic |
| 30 | * physical name is generated. Changing the name replaces the |
| 31 | * configuration. |
| 32 | */ |
| 33 | distributionConfigurationName?: string; |
| 34 | /** |
| 35 | * Per-region distribution settings: output AMI naming/tags/permissions, |
| 36 | * launch template updates, SSM parameter publication, and more. At |
| 37 | * least one entry (the build region) is required. |
| 38 | */ |
| 39 | distributions: imagebuilder.Distribution[]; |
| 40 | /** |
| 41 | * Description of the configuration. |
| 42 | */ |
| 43 | description?: string; |
| 44 | /** |
| 45 | * User-defined tags for the configuration. |
| 46 | */ |
| 47 | tags?: Record<string, string>; |
| 48 | } |
| 49 | |
| 50 | export interface DistributionConfiguration extends Resource< |
| 51 | "AWS.ImageBuilder.DistributionConfiguration", |
| 52 | DistributionConfigurationProps, |
| 53 | { |
| 54 | /** The name of the distribution configuration. */ |
| 55 | distributionConfigurationName: string; |
| 56 | /** The ARN of the distribution configuration. */ |
| 57 | distributionConfigurationArn: string; |
| 58 | /** When the distribution configuration was created. */ |
| 59 | dateCreated: string | undefined; |
| 60 | }, |
| 61 | never, |
| 62 | Providers |
| 63 | > {} |
| 64 | |
| 65 | /** |
| 66 | * An EC2 Image Builder distribution configuration — defines where and how |
| 67 | * the output AMIs (or containers) of a pipeline are distributed across |
| 68 | * regions and accounts. |
| 69 | * ### Creating a Distribution Configuration |
| 70 | * **Example:** Distribute in the Build Region |
| 71 | * ```typescript |
| 72 | * const distribution = yield* ImageBuilder.DistributionConfiguration("Dist", { |
| 73 | * distributions: [{ |
| 74 | * region: "us-west-2", |
| 75 | * amiDistributionConfiguration: { |
| 76 | * name: "my-app-{{ imagebuilder:buildDate }}", |
| 77 | * amiTags: { project: "my-app" }, |
| 78 | * }, |
| 79 | * }], |
| 80 | * }); |
| 81 | * ``` |
| 82 | * |
| 83 | * ### Using in a Pipeline |
| 84 | * **Example:** Wire into an Image Pipeline |
| 85 | * ```typescript |
| 86 | * const pipeline = yield* ImageBuilder.ImagePipeline("Pipeline", { |
| 87 | * imageRecipeArn: recipe.imageRecipeArn, |
| 88 | * infrastructureConfigurationArn: infra.infrastructureConfigurationArn, |
| 89 | * distributionConfigurationArn: distribution.distributionConfigurationArn, |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * @resource |
| 94 | */ |
| 95 | export const DistributionConfiguration = Resource<DistributionConfiguration>( |
| 96 | "AWS.ImageBuilder.DistributionConfiguration", |
| 97 | ); |
| 98 | |
| 99 | export const DistributionConfigurationProvider = () => |
| 100 | Provider.effect( |
| 101 | DistributionConfiguration, |
| 102 | Effect.gen(function* () { |
| 103 | const toName = (id: string, props: DistributionConfigurationProps) => |
| 104 | props.distributionConfigurationName |
| 105 | ? Effect.succeed(props.distributionConfigurationName) |
| 106 | : createPhysicalName({ id, maxLength: 126 }); |
| 107 | |
| 108 | const toArn = (name: string) => imageBuilderArn("distribution-configuration", name); |
| 109 | |
| 110 | const getConfiguration = Effect.fn(function* (arn: string) { |
| 111 | const response = yield* imagebuilder |
| 112 | .getDistributionConfiguration({ |
| 113 | distributionConfigurationArn: arn, |
| 114 | }) |
| 115 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 116 | return response?.distributionConfiguration; |
| 117 | }); |
| 118 | |
| 119 | const toAttrs = Effect.fn(function* (config: imagebuilder.DistributionConfiguration) { |
| 120 | if (!config.arn || !config.name) { |
| 121 | return yield* new DistributionConfigurationUnexpectedResponse({ |
| 122 | message: "Image Builder distribution configuration is missing its ARN or name", |
| 123 | }); |
| 124 | } |
| 125 | return { |
| 126 | distributionConfigurationName: config.name, |
| 127 | distributionConfigurationArn: config.arn, |
| 128 | dateCreated: config.dateCreated, |
| 129 | }; |
| 130 | }); |
| 131 | |
| 132 | return { |
| 133 | stables: ["distributionConfigurationName", "distributionConfigurationArn"], |
| 134 | |
| 135 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 136 | if (!isResolved(news)) return undefined; |
| 137 | if ((yield* toName(id, olds)) !== (yield* toName(id, news))) { |
| 138 | return { action: "replace" } as const; |
| 139 | } |
| 140 | }), |
| 141 | |
| 142 | read: Effect.fn(function* ({ id, olds, output }) { |
| 143 | const arn = |
| 144 | output?.distributionConfigurationArn ?? (yield* toArn(yield* toName(id, olds))); |
| 145 | const config = yield* getConfiguration(arn); |
| 146 | if (config === undefined) return undefined; |
| 147 | const attrs = yield* toAttrs(config); |
| 148 | const tags = toTagRecord(config.tags); |
| 149 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 150 | }), |
| 151 | |
| 152 | reconcile: Effect.fn(function* ({ id, news, olds, output, session }) { |
| 153 | // One idempotency token per reconcile — retries within this run |
| 154 | // are deduplicated by the API. |
| 155 | const clientToken = yield* Effect.sync(() => crypto.randomUUID()); |
| 156 | const name = output?.distributionConfigurationName ?? (yield* toName(id, news)); |
| 157 | const internalTags = yield* createInternalTags(id); |
| 158 | const desiredTags = { ...internalTags, ...news.tags }; |
| 159 | |
| 160 | // 1. Observe. |
| 161 | const arn = output?.distributionConfigurationArn ?? (yield* toArn(name)); |
| 162 | let observed = yield* getConfiguration(arn); |
| 163 | |
| 164 | // 2. Ensure — create if missing; tolerate an AlreadyExists race. |
| 165 | if (observed === undefined) { |
| 166 | const created = yield* imagebuilder |
| 167 | .createDistributionConfiguration({ |
| 168 | name, |
| 169 | description: news.description, |
| 170 | distributions: news.distributions, |
| 171 | tags: desiredTags, |
| 172 | clientToken, |
| 173 | }) |
| 174 | .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.undefined)); |
| 175 | observed = yield* getConfiguration(created?.distributionConfigurationArn ?? arn); |
| 176 | if (observed === undefined) { |
| 177 | return yield* new DistributionConfigurationUnexpectedResponse({ |
| 178 | message: `created Image Builder distribution configuration '${name}' is not readable`, |
| 179 | }); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // 3. Sync — compare OBSERVED distributions/description against |
| 184 | // desired; a full-PUT update converges any drift. |
| 185 | const drifted = |
| 186 | driftedFrom(observed.distributions, news.distributions) || |
| 187 | driftedFrom(observed.description, news.description) || |
| 188 | (news.description === undefined && olds?.description !== undefined); |
| 189 | if (drifted && observed.arn) { |
| 190 | yield* imagebuilder.updateDistributionConfiguration({ |
| 191 | distributionConfigurationArn: observed.arn, |
| 192 | description: news.description, |
| 193 | distributions: news.distributions, |
| 194 | clientToken, |
| 195 | }); |
| 196 | observed = (yield* getConfiguration(observed.arn)) ?? observed; |
| 197 | } |
| 198 | |
| 199 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 200 | if (observed.arn) { |
| 201 | yield* syncImageBuilderTags(observed.arn, desiredTags); |
| 202 | } |
| 203 | |
| 204 | // 4. Return fresh attributes. |
| 205 | yield* session.note(name); |
| 206 | return yield* toAttrs(observed); |
| 207 | }), |
| 208 | |
| 209 | delete: Effect.fn(function* ({ output }) { |
| 210 | yield* retryWhileDependedOn( |
| 211 | imagebuilder.deleteDistributionConfiguration({ |
| 212 | distributionConfigurationArn: output.distributionConfigurationArn, |
| 213 | }), |
| 214 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 215 | }), |
| 216 | |
| 217 | list: () => |
| 218 | imagebuilder.listDistributionConfigurations.pages({}).pipe( |
| 219 | Stream.runCollect, |
| 220 | Effect.map((chunk) => |
| 221 | Array.from(chunk).flatMap((page) => |
| 222 | (page.distributionConfigurationSummaryList ?? []).filter( |
| 223 | ( |
| 224 | summary, |
| 225 | ): summary is imagebuilder.DistributionConfigurationSummary & { |
| 226 | arn: string; |
| 227 | name: string; |
| 228 | } => summary.arn !== undefined && summary.name !== undefined, |
| 229 | ), |
| 230 | ), |
| 231 | ), |
| 232 | Effect.map((summaries) => |
| 233 | summaries.map((summary) => ({ |
| 234 | distributionConfigurationName: summary.name, |
| 235 | distributionConfigurationArn: summary.arn, |
| 236 | dateCreated: summary.dateCreated, |
| 237 | })), |
| 238 | ), |
| 239 | ), |
| 240 | }; |
| 241 | }), |
| 242 | ); |