The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as datazone from "@distilled.cloud/aws/datazone"; |
| 2 | import * as lf from "@distilled.cloud/aws/lakeformation"; |
| 3 | import { Region } from "@distilled.cloud/aws/Region"; |
| 4 | import { isResolved } from "alchemy/Diff"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { RegionID } from "../Region.ts"; |
| 12 | |
| 13 | /** |
| 14 | * Lake Formation provisioning configuration for a blueprint. |
| 15 | */ |
| 16 | export interface BlueprintLakeFormationConfiguration { |
| 17 | /** The ARN of the role used to register S3 locations with Lake Formation. */ |
| 18 | locationRegistrationRole?: string; |
| 19 | /** S3 locations to exclude from Lake Formation registration. */ |
| 20 | locationRegistrationExcludeS3Locations?: string[]; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * A provisioning configuration attached to a blueprint configuration. |
| 25 | */ |
| 26 | export interface BlueprintProvisioningConfiguration { |
| 27 | /** Lake Formation settings applied when the blueprint provisions environments. */ |
| 28 | lakeFormationConfiguration: BlueprintLakeFormationConfiguration; |
| 29 | } |
| 30 | |
| 31 | export interface EnvironmentBlueprintConfigurationProps { |
| 32 | /** |
| 33 | * The identifier of the {@link Domain} to configure the blueprint in. |
| 34 | * Accepts a domain's `domainId` output. Changing it triggers a |
| 35 | * replacement. |
| 36 | */ |
| 37 | domainId: string; |
| 38 | /** |
| 39 | * The managed environment blueprint to configure — a blueprint name |
| 40 | * (e.g. `"DefaultDataLake"`, `"DefaultDataWarehouse"`) or a raw blueprint |
| 41 | * identifier. Changing it triggers a replacement. |
| 42 | */ |
| 43 | environmentBlueprint: string; |
| 44 | /** |
| 45 | * The regions in which environments may be created from this blueprint. |
| 46 | */ |
| 47 | enabledRegions: string[]; |
| 48 | /** |
| 49 | * The ARN of the role DataZone uses to provision environment resources |
| 50 | * (CloudFormation stacks) from this blueprint. |
| 51 | */ |
| 52 | provisioningRoleArn?: string; |
| 53 | /** |
| 54 | * The ARN of the role DataZone uses to manage access grants to |
| 55 | * environment resources (e.g. Lake Formation permissions). |
| 56 | */ |
| 57 | manageAccessRoleArn?: string; |
| 58 | /** |
| 59 | * The ARN of a permission boundary policy to apply to environment roles |
| 60 | * created from this blueprint. |
| 61 | */ |
| 62 | environmentRolePermissionBoundary?: string; |
| 63 | /** |
| 64 | * Region-scoped provisioning parameters, keyed by region then parameter |
| 65 | * name (e.g. `{ "us-west-2": { S3Location: "s3://bucket" } }`). |
| 66 | */ |
| 67 | regionalParameters?: Record<string, Record<string, string>>; |
| 68 | /** |
| 69 | * Global provisioning parameters applied in every enabled region. |
| 70 | */ |
| 71 | globalParameters?: Record<string, string>; |
| 72 | /** |
| 73 | * Additional provisioning configurations (e.g. Lake Formation location |
| 74 | * registration). |
| 75 | */ |
| 76 | provisioningConfigurations?: BlueprintProvisioningConfiguration[]; |
| 77 | } |
| 78 | |
| 79 | export interface EnvironmentBlueprintConfiguration extends Resource< |
| 80 | "AWS.DataZone.EnvironmentBlueprintConfiguration", |
| 81 | EnvironmentBlueprintConfigurationProps, |
| 82 | { |
| 83 | /** The identifier of the domain the configuration lives in. */ |
| 84 | domainId: string; |
| 85 | /** The identifier of the configured environment blueprint. */ |
| 86 | environmentBlueprintId: string; |
| 87 | /** The name of the configured environment blueprint. */ |
| 88 | environmentBlueprintName: string; |
| 89 | /** The regions in which the blueprint is enabled. */ |
| 90 | enabledRegions: string[]; |
| 91 | /** The ARN of the provisioning role. */ |
| 92 | provisioningRoleArn: string | undefined; |
| 93 | /** The ARN of the manage-access role. */ |
| 94 | manageAccessRoleArn: string | undefined; |
| 95 | /** |
| 96 | * Lake Formation administrator entries this configuration is responsible |
| 97 | * for: the provisioning and manage-access roles, per enabled region, |
| 98 | * wherever the role was not already an administrator. DataZone makes both |
| 99 | * roles administrators when it deploys an environment; delete removes |
| 100 | * these entries again. |
| 101 | */ |
| 102 | lakeFormationAdmins?: LakeFormationAdminEntry[]; |
| 103 | } |
| 104 | > {} |
| 105 | |
| 106 | /** |
| 107 | * The account/domain configuration of an Amazon DataZone environment |
| 108 | * blueprint — enables a managed blueprint (like `DefaultDataLake`) in |
| 109 | * specific regions with the IAM roles DataZone should provision with. |
| 110 | * |
| 111 | * The blueprint itself is an AWS-managed definition; this resource owns only |
| 112 | * its per-domain configuration (a `PUT`-style singleton keyed by domain + |
| 113 | * blueprint). |
| 114 | * |
| 115 | * ### Configuring Blueprints |
| 116 | * **Example:** Enable the DefaultDataLake Blueprint |
| 117 | * ```typescript |
| 118 | * import * as DataZone from "@alchemy.run/aws/DataZone"; |
| 119 | * |
| 120 | * const config = yield* DataZone.EnvironmentBlueprintConfiguration( |
| 121 | * "datalake", |
| 122 | * { |
| 123 | * domainId: domain.domainId, |
| 124 | * environmentBlueprint: "DefaultDataLake", |
| 125 | * enabledRegions: ["us-west-2"], |
| 126 | * provisioningRoleArn: provisioningRole.roleArn, |
| 127 | * manageAccessRoleArn: manageAccessRole.roleArn, |
| 128 | * regionalParameters: { |
| 129 | * "us-west-2": { S3Location: "s3://my-datalake-bucket" }, |
| 130 | * }, |
| 131 | * }, |
| 132 | * ); |
| 133 | * ``` |
| 134 | * |
| 135 | * **Example:** Blueprint with Lake Formation Provisioning |
| 136 | * ```typescript |
| 137 | * const config = yield* DataZone.EnvironmentBlueprintConfiguration( |
| 138 | * "datalake", |
| 139 | * { |
| 140 | * domainId: domain.domainId, |
| 141 | * environmentBlueprint: "DefaultDataLake", |
| 142 | * enabledRegions: ["us-west-2"], |
| 143 | * provisioningRoleArn: provisioningRole.roleArn, |
| 144 | * provisioningConfigurations: [ |
| 145 | * { |
| 146 | * lakeFormationConfiguration: { |
| 147 | * locationRegistrationRole: registrationRole.roleArn, |
| 148 | * }, |
| 149 | * }, |
| 150 | * ], |
| 151 | * }, |
| 152 | * ); |
| 153 | * ``` |
| 154 | * |
| 155 | * @resource |
| 156 | */ |
| 157 | export const EnvironmentBlueprintConfiguration = Resource<EnvironmentBlueprintConfiguration>( |
| 158 | "AWS.DataZone.EnvironmentBlueprintConfiguration", |
| 159 | ); |
| 160 | |
| 161 | /** No blueprint with the requested name or id exists in the domain. */ |
| 162 | export class EnvironmentBlueprintNotFound extends Data.TaggedError( |
| 163 | "AWS.DataZone.EnvironmentBlueprintNotFound", |
| 164 | )<{ |
| 165 | readonly domainId: string; |
| 166 | readonly environmentBlueprint: string; |
| 167 | }> {} |
| 168 | |
| 169 | /** |
| 170 | * Freshly created IAM roles are eventually consistent; the PUT can |
| 171 | * transiently reject a provisioning/manage-access role it cannot yet assume. |
| 172 | * Wrapped in an explicitly-typed helper so the `Effect.retry` conditional |
| 173 | * return type does not leak into declaration emit (see PATTERNS §7). |
| 174 | */ |
| 175 | const retryWhileRoleAssumeFails = <A, E extends { _tag: string }, R>( |
| 176 | self: Effect.Effect<A, E, R>, |
| 177 | ): Effect.Effect<A, E, R> => |
| 178 | Effect.retry(self, { |
| 179 | while: (e) => |
| 180 | (e._tag === "ValidationException" || e._tag === "AccessDeniedException") && |
| 181 | "message" in e && |
| 182 | typeof e.message === "string" && |
| 183 | (e.message.toLowerCase().includes("role") || e.message.toLowerCase().includes("assume")), |
| 184 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 185 | }); |
| 186 | |
| 187 | /** A Lake Formation administrator entry in one region's catalog. */ |
| 188 | export interface LakeFormationAdminEntry { |
| 189 | /** The administrator principal (an IAM role ARN). */ |
| 190 | principal: string; |
| 191 | /** The region whose Data Catalog lists the administrator. */ |
| 192 | region: string; |
| 193 | } |
| 194 | |
| 195 | /** Run a Lake Formation call against a specific region's catalog. */ |
| 196 | const inRegion = (region: string) => |
| 197 | Effect.provideService(Region, Effect.succeed(region as RegionID)); |
| 198 | |
| 199 | const isLakeFormationAdmin = Effect.fn(function* (roleArn: string, region: string) { |
| 200 | const { DataLakeSettings: settings } = yield* lf.getDataLakeSettings({}).pipe(inRegion(region)); |
| 201 | return (settings?.DataLakeAdmins ?? []).some((p) => p.DataLakePrincipalIdentifier === roleArn); |
| 202 | }); |
| 203 | |
| 204 | /** |
| 205 | * Remove `roleArn` from the region's Lake Formation administrators. Left |
| 206 | * behind, the entry dangles once the role is deleted and Lake Formation |
| 207 | * rejects every later administrator write in the region as an invalid |
| 208 | * principal. |
| 209 | */ |
| 210 | const removeLakeFormationAdmin = Effect.fn(function* (roleArn: string, region: string) { |
| 211 | const { DataLakeSettings: settings = {} } = yield* lf |
| 212 | .getDataLakeSettings({}) |
| 213 | .pipe(inRegion(region)); |
| 214 | const admins = settings.DataLakeAdmins ?? []; |
| 215 | if (!admins.some((p) => p.DataLakePrincipalIdentifier === roleArn)) return; |
| 216 | yield* lf |
| 217 | .putDataLakeSettings({ |
| 218 | DataLakeSettings: { |
| 219 | ...settings, |
| 220 | DataLakeAdmins: admins.filter((p) => p.DataLakePrincipalIdentifier !== roleArn), |
| 221 | }, |
| 222 | }) |
| 223 | .pipe(inRegion(region)); |
| 224 | }); |
| 225 | |
| 226 | export const EnvironmentBlueprintConfigurationProvider = () => |
| 227 | Provider.effect( |
| 228 | EnvironmentBlueprintConfiguration, |
| 229 | Effect.gen(function* () { |
| 230 | // Resolve a blueprint name-or-id to its { id, name } within the domain. |
| 231 | const resolveBlueprint = Effect.fn(function* (domainId: string, nameOrId: string) { |
| 232 | const match = yield* datazone.listEnvironmentBlueprints |
| 233 | .items({ |
| 234 | domainIdentifier: domainId, |
| 235 | managed: true, |
| 236 | }) |
| 237 | .pipe( |
| 238 | Stream.filter((b) => b.name === nameOrId || b.id === nameOrId), |
| 239 | Stream.runHead, |
| 240 | Effect.map((head) => (head._tag === "Some" ? head.value : undefined)), |
| 241 | ); |
| 242 | if (match === undefined) { |
| 243 | return yield* new EnvironmentBlueprintNotFound({ |
| 244 | domainId, |
| 245 | environmentBlueprint: nameOrId, |
| 246 | }); |
| 247 | } |
| 248 | return { id: match.id, name: match.name }; |
| 249 | }); |
| 250 | |
| 251 | // A deleted configuration — or one inside a deleted domain — is |
| 252 | // reported as AccessDeniedException, NOT ResourceNotFoundException: |
| 253 | // DataZone evaluates domain-scoped authorization before existence. |
| 254 | // Both mean "absent". |
| 255 | const getConfigurationOrUndefined = Effect.fn(function* ( |
| 256 | domainId: string, |
| 257 | blueprintId: string, |
| 258 | ) { |
| 259 | return yield* datazone |
| 260 | .getEnvironmentBlueprintConfiguration({ |
| 261 | domainIdentifier: domainId, |
| 262 | environmentBlueprintIdentifier: blueprintId, |
| 263 | }) |
| 264 | .pipe( |
| 265 | Effect.catchTags({ |
| 266 | ResourceNotFoundException: () => Effect.undefined, |
| 267 | AccessDeniedException: () => Effect.undefined, |
| 268 | }), |
| 269 | ); |
| 270 | }); |
| 271 | |
| 272 | return EnvironmentBlueprintConfiguration.Provider.of({ |
| 273 | stables: ["domainId", "environmentBlueprintId"], |
| 274 | |
| 275 | // Configurations are keyed by their parent domain — there is no |
| 276 | // account-level enumeration without a domain identifier. |
| 277 | list: () => Effect.succeed([]), |
| 278 | |
| 279 | read: Effect.fn(function* ({ olds, output }) { |
| 280 | const domainId = output?.domainId ?? olds?.domainId; |
| 281 | if (domainId === undefined) return undefined; |
| 282 | const blueprint = output?.environmentBlueprintId |
| 283 | ? { |
| 284 | id: output.environmentBlueprintId, |
| 285 | name: output.environmentBlueprintName, |
| 286 | } |
| 287 | : olds?.environmentBlueprint |
| 288 | ? yield* resolveBlueprint(domainId, olds.environmentBlueprint).pipe( |
| 289 | Effect.catchTags({ |
| 290 | "AWS.DataZone.EnvironmentBlueprintNotFound": () => Effect.undefined, |
| 291 | // the domain itself may already be gone (reported as |
| 292 | // NotFound or as AccessDenied — auth precedes existence) |
| 293 | ResourceNotFoundException: () => Effect.undefined, |
| 294 | AccessDeniedException: () => Effect.undefined, |
| 295 | }), |
| 296 | ) |
| 297 | : undefined; |
| 298 | if (blueprint === undefined) return undefined; |
| 299 | const config = yield* getConfigurationOrUndefined(domainId, blueprint.id); |
| 300 | // An un-configured blueprint returns an empty configuration shell — |
| 301 | // treat "no enabled regions and no roles" as absent. |
| 302 | if ( |
| 303 | config === undefined || |
| 304 | ((config.enabledRegions ?? []).length === 0 && |
| 305 | config.provisioningRoleArn === undefined && |
| 306 | config.manageAccessRoleArn === undefined) |
| 307 | ) { |
| 308 | return undefined; |
| 309 | } |
| 310 | // Blueprint configurations have no tags — ownership is tracked |
| 311 | // purely by identity. |
| 312 | return { |
| 313 | domainId: config.domainId, |
| 314 | environmentBlueprintId: config.environmentBlueprintId, |
| 315 | environmentBlueprintName: blueprint.name, |
| 316 | enabledRegions: [...(config.enabledRegions ?? [])], |
| 317 | provisioningRoleArn: config.provisioningRoleArn, |
| 318 | manageAccessRoleArn: config.manageAccessRoleArn, |
| 319 | lakeFormationAdmins: output?.lakeFormationAdmins, |
| 320 | }; |
| 321 | }), |
| 322 | |
| 323 | diff: Effect.fn(function* ({ news, olds }) { |
| 324 | if (!isResolved(news)) return undefined; |
| 325 | if (olds === undefined) return undefined; |
| 326 | if (olds.domainId !== news.domainId) { |
| 327 | return { action: "replace" } as const; |
| 328 | } |
| 329 | if (olds.environmentBlueprint !== news.environmentBlueprint) { |
| 330 | return { action: "replace" } as const; |
| 331 | } |
| 332 | // regions, roles, and parameters converge via the idempotent PUT. |
| 333 | }), |
| 334 | |
| 335 | reconcile: Effect.fn(function* ({ news, output, session }) { |
| 336 | const domainId = news.domainId; |
| 337 | const blueprint = output?.environmentBlueprintId |
| 338 | ? { |
| 339 | id: output.environmentBlueprintId, |
| 340 | name: output.environmentBlueprintName, |
| 341 | } |
| 342 | : yield* resolveBlueprint(domainId, news.environmentBlueprint); |
| 343 | |
| 344 | // The (role, region) administrator entries DataZone may add for |
| 345 | // this configuration. |
| 346 | const roles = [news.provisioningRoleArn, news.manageAccessRoleArn].filter( |
| 347 | (role): role is string => role !== undefined, |
| 348 | ); |
| 349 | const candidates = roles.flatMap((principal) => |
| 350 | news.enabledRegions.map((region) => ({ principal, region })), |
| 351 | ); |
| 352 | const same = (a: LakeFormationAdminEntry) => (b: LakeFormationAdminEntry) => |
| 353 | a.principal === b.principal && a.region === b.region; |
| 354 | const previous = output?.lakeFormationAdmins ?? []; |
| 355 | |
| 356 | // Release entries for roles or regions no longer configured. |
| 357 | for (const entry of previous.filter((e) => !candidates.some(same(e)))) { |
| 358 | yield* removeLakeFormationAdmin(entry.principal, entry.region); |
| 359 | } |
| 360 | |
| 361 | // Observe, before DataZone can add it, where each role is not yet an |
| 362 | // administrator — those entries are ours to remove. |
| 363 | const lakeFormationAdmins = yield* Effect.filter(candidates, (entry) => |
| 364 | previous.some(same(entry)) |
| 365 | ? Effect.succeed(true) |
| 366 | : isLakeFormationAdmin(entry.principal, entry.region).pipe( |
| 367 | Effect.map((admin) => !admin), |
| 368 | ), |
| 369 | ); |
| 370 | |
| 371 | // PutEnvironmentBlueprintConfiguration is a true upsert — one call |
| 372 | // converges greenfield, update, and adoption alike. |
| 373 | const config = yield* retryWhileRoleAssumeFails( |
| 374 | datazone.putEnvironmentBlueprintConfiguration({ |
| 375 | domainIdentifier: domainId, |
| 376 | environmentBlueprintIdentifier: blueprint.id, |
| 377 | enabledRegions: news.enabledRegions, |
| 378 | provisioningRoleArn: news.provisioningRoleArn, |
| 379 | manageAccessRoleArn: news.manageAccessRoleArn, |
| 380 | environmentRolePermissionBoundary: news.environmentRolePermissionBoundary, |
| 381 | regionalParameters: news.regionalParameters, |
| 382 | globalParameters: news.globalParameters, |
| 383 | provisioningConfigurations: news.provisioningConfigurations, |
| 384 | }), |
| 385 | ); |
| 386 | |
| 387 | yield* session.note(`${blueprint.name} (${blueprint.id})`); |
| 388 | return { |
| 389 | domainId: config.domainId, |
| 390 | environmentBlueprintId: config.environmentBlueprintId, |
| 391 | environmentBlueprintName: blueprint.name, |
| 392 | enabledRegions: [...(config.enabledRegions ?? news.enabledRegions)], |
| 393 | provisioningRoleArn: config.provisioningRoleArn, |
| 394 | manageAccessRoleArn: config.manageAccessRoleArn, |
| 395 | lakeFormationAdmins, |
| 396 | }; |
| 397 | }), |
| 398 | |
| 399 | delete: Effect.fn(function* ({ output }) { |
| 400 | yield* datazone |
| 401 | .deleteEnvironmentBlueprintConfiguration({ |
| 402 | domainIdentifier: output.domainId, |
| 403 | environmentBlueprintIdentifier: output.environmentBlueprintId, |
| 404 | }) |
| 405 | .pipe( |
| 406 | // Environments created from the configuration may still be |
| 407 | // deleting; DataZone refuses until they are gone. |
| 408 | Effect.retry({ |
| 409 | while: (e) => e._tag === "EnvironmentBlueprintConfigurationInUse", |
| 410 | schedule: Schedule.spaced("5 seconds"), |
| 411 | times: 24, |
| 412 | }), |
| 413 | Effect.catchTags({ |
| 414 | ResourceNotFoundException: () => Effect.void, |
| 415 | // deleting a configuration whose domain is already gone |
| 416 | // surfaces as AccessDenied — auth is checked before existence. |
| 417 | AccessDeniedException: () => Effect.void, |
| 418 | }), |
| 419 | ); |
| 420 | for (const entry of output.lakeFormationAdmins ?? []) { |
| 421 | yield* removeLakeFormationAdmin(entry.principal, entry.region); |
| 422 | } |
| 423 | }), |
| 424 | }); |
| 425 | }), |
| 426 | ); |