alchemy 2.0.0-beta.17 → 2.0.0-beta.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/bin/alchemy.js +1 -1
  2. package/bin/alchemy.js.map +1 -1
  3. package/lib/AWS/CloudFront/CachePolicy.d.ts +106 -0
  4. package/lib/AWS/CloudFront/CachePolicy.d.ts.map +1 -0
  5. package/lib/AWS/CloudFront/CachePolicy.js +147 -0
  6. package/lib/AWS/CloudFront/CachePolicy.js.map +1 -0
  7. package/lib/AWS/CloudFront/KeyGroup.d.ts +72 -0
  8. package/lib/AWS/CloudFront/KeyGroup.d.ts.map +1 -0
  9. package/lib/AWS/CloudFront/KeyGroup.js +136 -0
  10. package/lib/AWS/CloudFront/KeyGroup.js.map +1 -0
  11. package/lib/AWS/CloudFront/OriginRequestPolicy.d.ts +84 -0
  12. package/lib/AWS/CloudFront/OriginRequestPolicy.d.ts.map +1 -0
  13. package/lib/AWS/CloudFront/OriginRequestPolicy.js +139 -0
  14. package/lib/AWS/CloudFront/OriginRequestPolicy.js.map +1 -0
  15. package/lib/AWS/CloudFront/PublicKey.d.ts +72 -0
  16. package/lib/AWS/CloudFront/PublicKey.d.ts.map +1 -0
  17. package/lib/AWS/CloudFront/PublicKey.js +137 -0
  18. package/lib/AWS/CloudFront/PublicKey.js.map +1 -0
  19. package/lib/AWS/CloudFront/ResponseHeadersPolicy.d.ts +114 -0
  20. package/lib/AWS/CloudFront/ResponseHeadersPolicy.d.ts.map +1 -0
  21. package/lib/AWS/CloudFront/ResponseHeadersPolicy.js +162 -0
  22. package/lib/AWS/CloudFront/ResponseHeadersPolicy.js.map +1 -0
  23. package/lib/AWS/CloudFront/index.d.ts +5 -0
  24. package/lib/AWS/CloudFront/index.d.ts.map +1 -1
  25. package/lib/AWS/CloudFront/index.js +5 -0
  26. package/lib/AWS/CloudFront/index.js.map +1 -1
  27. package/lib/AWS/ECS/CapacityProvider.d.ts +102 -0
  28. package/lib/AWS/ECS/CapacityProvider.d.ts.map +1 -0
  29. package/lib/AWS/ECS/CapacityProvider.js +217 -0
  30. package/lib/AWS/ECS/CapacityProvider.js.map +1 -0
  31. package/lib/AWS/ECS/index.d.ts +1 -0
  32. package/lib/AWS/ECS/index.d.ts.map +1 -1
  33. package/lib/AWS/ECS/index.js +1 -0
  34. package/lib/AWS/ECS/index.js.map +1 -1
  35. package/lib/AWS/Providers.d.ts.map +1 -1
  36. package/lib/AWS/Providers.js +7 -1
  37. package/lib/AWS/Providers.js.map +1 -1
  38. package/lib/GitHub/Secrets.d.ts +45 -0
  39. package/lib/GitHub/Secrets.d.ts.map +1 -0
  40. package/lib/GitHub/Secrets.js +30 -0
  41. package/lib/GitHub/Secrets.js.map +1 -0
  42. package/lib/GitHub/index.d.ts +1 -0
  43. package/lib/GitHub/index.d.ts.map +1 -1
  44. package/lib/GitHub/index.js +1 -0
  45. package/lib/GitHub/index.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/AWS/CloudFront/CachePolicy.ts +271 -0
  48. package/src/AWS/CloudFront/KeyGroup.ts +233 -0
  49. package/src/AWS/CloudFront/OriginRequestPolicy.ts +256 -0
  50. package/src/AWS/CloudFront/PublicKey.ts +245 -0
  51. package/src/AWS/CloudFront/ResponseHeadersPolicy.ts +308 -0
  52. package/src/AWS/CloudFront/index.ts +11 -0
  53. package/src/AWS/ECS/CapacityProvider.ts +336 -0
  54. package/src/AWS/ECS/index.ts +4 -0
  55. package/src/AWS/Providers.ts +12 -0
  56. package/src/GitHub/Secrets.ts +66 -0
  57. package/src/GitHub/index.ts +1 -0
@@ -0,0 +1,308 @@
1
+ import * as cloudfront from "@distilled.cloud/aws/cloudfront";
2
+ import * as Effect from "effect/Effect";
3
+ import { isResolved } from "../../Diff.ts";
4
+ import { createPhysicalName } from "../../PhysicalName.ts";
5
+ import * as Provider from "../../Provider.ts";
6
+ import { Resource } from "../../Resource.ts";
7
+ import type { Providers } from "../Providers.ts";
8
+
9
+ export interface ResponseHeadersPolicyProps {
10
+ /**
11
+ * Name of the response headers policy. If omitted, a deterministic name is
12
+ * generated. Names must be unique per AWS account. Changing the name
13
+ * triggers a replacement.
14
+ */
15
+ name?: string;
16
+ /**
17
+ * Optional comment describing the policy.
18
+ */
19
+ comment?: string;
20
+ /**
21
+ * CORS configuration applied to viewer responses.
22
+ */
23
+ corsConfig?: cloudfront.ResponseHeadersPolicyCorsConfig;
24
+ /**
25
+ * Standard security headers (HSTS, X-Frame-Options, etc.) added to
26
+ * viewer responses.
27
+ */
28
+ securityHeadersConfig?: cloudfront.ResponseHeadersPolicySecurityHeadersConfig;
29
+ /**
30
+ * Server-Timing header configuration for measuring CloudFront performance.
31
+ */
32
+ serverTimingHeadersConfig?: cloudfront.ResponseHeadersPolicyServerTimingHeadersConfig;
33
+ /**
34
+ * Custom headers to add to viewer responses.
35
+ */
36
+ customHeadersConfig?: cloudfront.ResponseHeadersPolicyCustomHeadersConfig;
37
+ /**
38
+ * Headers to remove from viewer responses.
39
+ */
40
+ removeHeadersConfig?: cloudfront.ResponseHeadersPolicyRemoveHeadersConfig;
41
+ }
42
+
43
+ export interface ResponseHeadersPolicy extends Resource<
44
+ "AWS.CloudFront.ResponseHeadersPolicy",
45
+ ResponseHeadersPolicyProps,
46
+ {
47
+ /**
48
+ * CloudFront-assigned response headers policy identifier.
49
+ */
50
+ responseHeadersPolicyId: string;
51
+ /**
52
+ * Name of the response headers policy.
53
+ */
54
+ name: string;
55
+ /**
56
+ * Most recent entity tag for update/delete operations.
57
+ */
58
+ etag: string | undefined;
59
+ /**
60
+ * Current comment on the policy.
61
+ */
62
+ comment: string | undefined;
63
+ /**
64
+ * Current CORS configuration.
65
+ */
66
+ corsConfig: cloudfront.ResponseHeadersPolicyCorsConfig | undefined;
67
+ /**
68
+ * Current security headers configuration.
69
+ */
70
+ securityHeadersConfig:
71
+ | cloudfront.ResponseHeadersPolicySecurityHeadersConfig
72
+ | undefined;
73
+ /**
74
+ * Current Server-Timing configuration.
75
+ */
76
+ serverTimingHeadersConfig:
77
+ | cloudfront.ResponseHeadersPolicyServerTimingHeadersConfig
78
+ | undefined;
79
+ /**
80
+ * Current custom headers configuration.
81
+ */
82
+ customHeadersConfig:
83
+ | cloudfront.ResponseHeadersPolicyCustomHeadersConfig
84
+ | undefined;
85
+ /**
86
+ * Current remove-headers configuration.
87
+ */
88
+ removeHeadersConfig:
89
+ | cloudfront.ResponseHeadersPolicyRemoveHeadersConfig
90
+ | undefined;
91
+ },
92
+ never,
93
+ Providers
94
+ > {}
95
+
96
+ /**
97
+ * A CloudFront response headers policy.
98
+ *
99
+ * Response headers policies add or remove headers in viewer responses,
100
+ * including CORS, standard security headers (HSTS, X-Frame-Options,
101
+ * X-Content-Type-Options, Referrer-Policy, etc.), Server-Timing, custom
102
+ * headers and explicit header removal. They are referenced by ID on a
103
+ * Distribution's default behavior or per-path cache behaviors.
104
+ *
105
+ * @section Creating Response Headers Policies
106
+ * @example Standard security + CORS
107
+ * ```typescript
108
+ * const responseHeadersPolicy = yield* ResponseHeadersPolicy("AppResponseHeaders", {
109
+ * comment: "Default app security + CORS",
110
+ * corsConfig: {
111
+ * AccessControlAllowOrigins: { Quantity: 1, Items: ["https://app.example.com"] },
112
+ * AccessControlAllowMethods: { Quantity: 2, Items: ["GET", "OPTIONS"] },
113
+ * AccessControlAllowHeaders: { Quantity: 1, Items: ["Authorization"] },
114
+ * AccessControlAllowCredentials: false,
115
+ * OriginOverride: true,
116
+ * },
117
+ * securityHeadersConfig: {
118
+ * StrictTransportSecurity: {
119
+ * AccessControlMaxAgeSec: 31536000,
120
+ * IncludeSubdomains: true,
121
+ * Preload: true,
122
+ * Override: true,
123
+ * },
124
+ * ContentTypeOptions: { Override: true },
125
+ * FrameOptions: { FrameOption: "DENY", Override: true },
126
+ * ReferrerPolicy: { ReferrerPolicy: "no-referrer", Override: true },
127
+ * },
128
+ * });
129
+ * ```
130
+ */
131
+ export const ResponseHeadersPolicy = Resource<ResponseHeadersPolicy>(
132
+ "AWS.CloudFront.ResponseHeadersPolicy",
133
+ );
134
+
135
+ export const ResponseHeadersPolicyProvider = () =>
136
+ Provider.effect(
137
+ ResponseHeadersPolicy,
138
+ Effect.gen(function* () {
139
+ const getById = Effect.fn(function* (id: string) {
140
+ const config = yield* cloudfront
141
+ .getResponseHeadersPolicyConfig({ Id: id })
142
+ .pipe(
143
+ Effect.catchTag("NoSuchResponseHeadersPolicy", () =>
144
+ Effect.succeed(undefined),
145
+ ),
146
+ );
147
+ if (!config?.ResponseHeadersPolicyConfig) return undefined;
148
+ return {
149
+ config: config.ResponseHeadersPolicyConfig,
150
+ etag: config.ETag,
151
+ };
152
+ });
153
+
154
+ const getByName = Effect.fn(function* (name: string) {
155
+ const listed = yield* cloudfront.listResponseHeadersPolicies({
156
+ Type: "custom",
157
+ });
158
+ const summary = listed.ResponseHeadersPolicyList?.Items?.find(
159
+ (item) =>
160
+ item.ResponseHeadersPolicy?.ResponseHeadersPolicyConfig?.Name ===
161
+ name,
162
+ );
163
+ if (!summary?.ResponseHeadersPolicy?.Id) return undefined;
164
+ return yield* getById(summary.ResponseHeadersPolicy.Id).pipe(
165
+ Effect.map((found) =>
166
+ found
167
+ ? { id: summary.ResponseHeadersPolicy.Id, ...found }
168
+ : undefined,
169
+ ),
170
+ );
171
+ });
172
+
173
+ const buildConfig = (
174
+ name: string,
175
+ props: ResponseHeadersPolicyProps,
176
+ ): cloudfront.ResponseHeadersPolicyConfig => ({
177
+ Name: name,
178
+ Comment: props.comment,
179
+ CorsConfig: props.corsConfig,
180
+ SecurityHeadersConfig: props.securityHeadersConfig,
181
+ ServerTimingHeadersConfig: props.serverTimingHeadersConfig,
182
+ CustomHeadersConfig: props.customHeadersConfig,
183
+ RemoveHeadersConfig: props.removeHeadersConfig,
184
+ });
185
+
186
+ const toAttrs = (
187
+ id: string,
188
+ config: cloudfront.ResponseHeadersPolicyConfig,
189
+ etag: string | undefined,
190
+ ) => ({
191
+ responseHeadersPolicyId: id,
192
+ name: config.Name,
193
+ etag,
194
+ comment: config.Comment,
195
+ corsConfig: config.CorsConfig,
196
+ securityHeadersConfig: config.SecurityHeadersConfig,
197
+ serverTimingHeadersConfig: config.ServerTimingHeadersConfig,
198
+ customHeadersConfig: config.CustomHeadersConfig,
199
+ removeHeadersConfig: config.RemoveHeadersConfig,
200
+ });
201
+
202
+ return {
203
+ stables: ["responseHeadersPolicyId"],
204
+ diff: Effect.fn(function* ({ id, news, olds }) {
205
+ if (!isResolved(news)) return undefined;
206
+ if (
207
+ (yield* createName(id, olds ?? {})) !==
208
+ (yield* createName(id, news))
209
+ ) {
210
+ return { action: "replace" } as const;
211
+ }
212
+ }),
213
+ read: Effect.fn(function* ({ id, olds, output }) {
214
+ if (output?.responseHeadersPolicyId) {
215
+ const found = yield* getById(output.responseHeadersPolicyId);
216
+ if (found) {
217
+ return toAttrs(
218
+ output.responseHeadersPolicyId,
219
+ found.config,
220
+ found.etag,
221
+ );
222
+ }
223
+ }
224
+ const name = yield* createName(id, olds ?? {});
225
+ const found = yield* getByName(name);
226
+ if (!found) return undefined;
227
+ return toAttrs(found.id, found.config, found.etag);
228
+ }),
229
+ create: Effect.fn(function* ({ id, news, session }) {
230
+ const name = yield* createName(id, news);
231
+ const created = yield* cloudfront
232
+ .createResponseHeadersPolicy({
233
+ ResponseHeadersPolicyConfig: buildConfig(name, news),
234
+ })
235
+ .pipe(
236
+ Effect.catchTag("ResponseHeadersPolicyAlreadyExists", () =>
237
+ getByName(name).pipe(
238
+ Effect.flatMap((existing) =>
239
+ existing
240
+ ? Effect.succeed({
241
+ ResponseHeadersPolicy: {
242
+ Id: existing.id,
243
+ LastModifiedTime: new Date(),
244
+ ResponseHeadersPolicyConfig: existing.config,
245
+ },
246
+ ETag: existing.etag,
247
+ Location: undefined,
248
+ })
249
+ : Effect.fail(
250
+ new Error(
251
+ `Response headers policy '${name}' already exists but could not be recovered`,
252
+ ),
253
+ ),
254
+ ),
255
+ ),
256
+ ),
257
+ );
258
+ if (!created.ResponseHeadersPolicy?.Id) {
259
+ return yield* Effect.fail(
260
+ new Error("createResponseHeadersPolicy returned no identifier"),
261
+ );
262
+ }
263
+ yield* session.note(created.ResponseHeadersPolicy.Id);
264
+ return toAttrs(
265
+ created.ResponseHeadersPolicy.Id,
266
+ created.ResponseHeadersPolicy.ResponseHeadersPolicyConfig,
267
+ created.ETag,
268
+ );
269
+ }),
270
+ update: Effect.fn(function* ({ news, output, session }) {
271
+ const current = yield* getById(output.responseHeadersPolicyId);
272
+ const updated = yield* cloudfront.updateResponseHeadersPolicy({
273
+ Id: output.responseHeadersPolicyId,
274
+ IfMatch: current?.etag ?? output.etag,
275
+ ResponseHeadersPolicyConfig: buildConfig(output.name, news),
276
+ });
277
+ if (!updated.ResponseHeadersPolicy?.Id) {
278
+ return yield* Effect.fail(
279
+ new Error("updateResponseHeadersPolicy returned no identifier"),
280
+ );
281
+ }
282
+ yield* session.note(output.responseHeadersPolicyId);
283
+ return toAttrs(
284
+ updated.ResponseHeadersPolicy.Id,
285
+ updated.ResponseHeadersPolicy.ResponseHeadersPolicyConfig,
286
+ updated.ETag,
287
+ );
288
+ }),
289
+ delete: Effect.fn(function* ({ output }) {
290
+ const current = yield* getById(output.responseHeadersPolicyId);
291
+ if (!current) return;
292
+ yield* cloudfront
293
+ .deleteResponseHeadersPolicy({
294
+ Id: output.responseHeadersPolicyId,
295
+ IfMatch: current.etag,
296
+ })
297
+ .pipe(
298
+ Effect.catchTag("NoSuchResponseHeadersPolicy", () => Effect.void),
299
+ );
300
+ }),
301
+ };
302
+ }),
303
+ );
304
+
305
+ const createName = (id: string, props: ResponseHeadersPolicyProps) =>
306
+ props.name
307
+ ? Effect.succeed(props.name)
308
+ : createPhysicalName({ id, maxLength: 128, lowercase: true });
@@ -1,6 +1,8 @@
1
+ export { CachePolicy, CachePolicyProvider } from "./CachePolicy.ts";
1
2
  export { Distribution, DistributionProvider } from "./Distribution.ts";
2
3
  export { Function, FunctionProvider } from "./Function.ts";
3
4
  export { Invalidation, InvalidationProvider } from "./Invalidation.ts";
5
+ export { KeyGroup, KeyGroupProvider } from "./KeyGroup.ts";
4
6
  export { KeyValueStore, KeyValueStoreProvider } from "./KeyValueStore.ts";
5
7
  export { KvEntries, KvEntriesProvider } from "./KvEntries.ts";
6
8
  export { KvRoutesUpdate, KvRoutesUpdateProvider } from "./KvRoutesUpdate.ts";
@@ -13,3 +15,12 @@ export {
13
15
  OriginAccessControl,
14
16
  OriginAccessControlProvider,
15
17
  } from "./OriginAccessControl.ts";
18
+ export {
19
+ OriginRequestPolicy,
20
+ OriginRequestPolicyProvider,
21
+ } from "./OriginRequestPolicy.ts";
22
+ export { PublicKey, PublicKeyProvider } from "./PublicKey.ts";
23
+ export {
24
+ ResponseHeadersPolicy,
25
+ ResponseHeadersPolicyProvider,
26
+ } from "./ResponseHeadersPolicy.ts";
@@ -0,0 +1,336 @@
1
+ import * as ecs from "@distilled.cloud/aws/ecs";
2
+ import { Region } from "@distilled.cloud/aws/Region";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Option from "effect/Option";
5
+ import { AdoptPolicy } from "../../AdoptPolicy.ts";
6
+ import { isResolved } from "../../Diff.ts";
7
+ import type { Input } from "../../Input.ts";
8
+ import { createPhysicalName } from "../../PhysicalName.ts";
9
+ import * as Provider from "../../Provider.ts";
10
+ import { Resource } from "../../Resource.ts";
11
+ import { createInternalTags, diffTags, hasTags } from "../../Tags.ts";
12
+ import { AWSEnvironment, type AccountID } from "../Environment.ts";
13
+ import type { Providers } from "../Providers.ts";
14
+ import type { RegionID } from "../Region.ts";
15
+
16
+ export type CapacityProviderName = string;
17
+ export type CapacityProviderArn =
18
+ `arn:aws:ecs:${RegionID}:${AccountID}:capacity-provider/${CapacityProviderName}`;
19
+
20
+ export interface CapacityProviderProps {
21
+ /**
22
+ * Capacity provider name. If omitted, a deterministic name is generated.
23
+ *
24
+ * Names beginning with `aws`, `ecs`, or `fargate` are reserved by AWS.
25
+ * Changing this triggers a replacement.
26
+ */
27
+ name?: string;
28
+ /**
29
+ * ARN of the EC2 Auto Scaling Group that backs this capacity provider.
30
+ *
31
+ * Cannot be changed after creation; changing this triggers a replacement.
32
+ */
33
+ autoScalingGroupArn: Input<string>;
34
+ /**
35
+ * Managed scaling configuration applied by ECS to the underlying ASG.
36
+ */
37
+ managedScaling?: ecs.ManagedScaling;
38
+ /**
39
+ * Whether ECS protects in-use container instances from ASG scale-in.
40
+ * @default "DISABLED"
41
+ */
42
+ managedTerminationProtection?: ecs.ManagedTerminationProtection;
43
+ /**
44
+ * Whether ECS sets container instances to DRAINING when terminated by ASG.
45
+ * @default "DISABLED"
46
+ */
47
+ managedDraining?: ecs.ManagedDraining;
48
+ /**
49
+ * User-defined tags to apply to the capacity provider.
50
+ */
51
+ tags?: Record<string, string>;
52
+ }
53
+
54
+ export interface CapacityProvider extends Resource<
55
+ "AWS.ECS.CapacityProvider",
56
+ CapacityProviderProps,
57
+ {
58
+ capacityProviderArn: CapacityProviderArn;
59
+ name: CapacityProviderName;
60
+ status: ecs.CapacityProviderStatus;
61
+ updateStatus: ecs.CapacityProviderUpdateStatus | undefined;
62
+ autoScalingGroupArn: string;
63
+ managedScaling: ecs.ManagedScaling | undefined;
64
+ managedTerminationProtection: ecs.ManagedTerminationProtection | undefined;
65
+ managedDraining: ecs.ManagedDraining | undefined;
66
+ tags: Record<string, string>;
67
+ },
68
+ never,
69
+ Providers
70
+ > {}
71
+
72
+ /**
73
+ * An Amazon ECS capacity provider backed by an EC2 Auto Scaling Group.
74
+ *
75
+ * Capacity providers are associated with one or more ECS clusters via
76
+ * {@link Cluster#capacityProviders} and are referenced by a service or task's
77
+ * capacity provider strategy.
78
+ *
79
+ * Only EC2 Auto Scaling Group-backed capacity providers are currently
80
+ * supported. The reserved AWS providers `FARGATE` and `FARGATE_SPOT` do not
81
+ * need to be created and can be referenced by name on a `Cluster` directly.
82
+ *
83
+ * @section Creating Capacity Providers
84
+ * @example ASG-Backed Capacity Provider
85
+ * ```typescript
86
+ * const provider = yield* CapacityProvider("AppCapacityProvider", {
87
+ * autoScalingGroupArn: asg.autoScalingGroupArn,
88
+ * managedScaling: {
89
+ * status: "ENABLED",
90
+ * targetCapacity: 80,
91
+ * minimumScalingStepSize: 1,
92
+ * maximumScalingStepSize: 10,
93
+ * },
94
+ * managedTerminationProtection: "ENABLED",
95
+ * });
96
+ *
97
+ * yield* Cluster("AppCluster", {
98
+ * capacityProviders: [provider.name],
99
+ * defaultCapacityProviderStrategy: [
100
+ * { capacityProvider: provider.name, weight: 1 },
101
+ * ],
102
+ * });
103
+ * ```
104
+ *
105
+ * @section Adopting Existing Capacity Providers
106
+ * @example Adopt an existing provider
107
+ * ```typescript
108
+ * import { adopt } from "alchemy/AdoptPolicy";
109
+ *
110
+ * yield* CapacityProvider("AppCapacityProvider", {
111
+ * name: "existing-provider",
112
+ * autoScalingGroupArn: asg.autoScalingGroupArn,
113
+ * }).pipe(adopt());
114
+ * ```
115
+ */
116
+ export const CapacityProvider = Resource<CapacityProvider>(
117
+ "AWS.ECS.CapacityProvider",
118
+ );
119
+
120
+ export const CapacityProviderProvider = () =>
121
+ Provider.effect(
122
+ CapacityProvider,
123
+ Effect.gen(function* () {
124
+ const adoptPolicy = yield* Effect.serviceOption(AdoptPolicy).pipe(
125
+ Effect.map(Option.getOrElse(() => false)),
126
+ );
127
+
128
+ const toEcsTags = (tags: Record<string, string>): ecs.Tag[] =>
129
+ Object.entries(tags).map(([key, value]) => ({ key, value }));
130
+
131
+ const fromEcsTags = (tags: ecs.Tag[] | undefined): Record<string, string> =>
132
+ Object.fromEntries(
133
+ (tags ?? [])
134
+ .filter((t): t is { key: string; value: string } =>
135
+ t.key !== undefined && t.value !== undefined,
136
+ )
137
+ .map((t) => [t.key, t.value]),
138
+ );
139
+
140
+ const toName = (id: string, props: { name?: string } = {}) =>
141
+ props.name
142
+ ? Effect.succeed(props.name)
143
+ : createPhysicalName({ id, maxLength: 255, lowercase: true });
144
+
145
+ const describe = (name: string) =>
146
+ ecs
147
+ .describeCapacityProviders({
148
+ capacityProviders: [name],
149
+ include: ["TAGS"],
150
+ })
151
+ .pipe(
152
+ Effect.map((res) =>
153
+ res.capacityProviders?.find((p) => p.name === name),
154
+ ),
155
+ );
156
+
157
+ return {
158
+ stables: ["capacityProviderArn", "name"],
159
+
160
+ diff: Effect.fn(function* ({ id, news, olds }) {
161
+ if (!isResolved(news)) return;
162
+ const oldName = yield* toName(id, olds ?? {});
163
+ const newName = yield* toName(id, news);
164
+ if (oldName !== newName) {
165
+ return { action: "replace" } as const;
166
+ }
167
+ if (
168
+ isResolved(olds.autoScalingGroupArn) &&
169
+ olds.autoScalingGroupArn !== news.autoScalingGroupArn
170
+ ) {
171
+ return { action: "replace" } as const;
172
+ }
173
+ }),
174
+
175
+ read: Effect.fn(function* ({ id, olds, output }) {
176
+ const name = output?.name ?? (yield* toName(id, olds ?? {}));
177
+ const found = yield* describe(name);
178
+ if (!found?.name || !found.capacityProviderArn) {
179
+ return undefined;
180
+ }
181
+ return {
182
+ capacityProviderArn:
183
+ found.capacityProviderArn as CapacityProviderArn,
184
+ name: found.name,
185
+ status: (found.status ?? "ACTIVE") as ecs.CapacityProviderStatus,
186
+ updateStatus: found.updateStatus,
187
+ autoScalingGroupArn:
188
+ found.autoScalingGroupProvider?.autoScalingGroupArn ?? "",
189
+ managedScaling: found.autoScalingGroupProvider?.managedScaling,
190
+ managedTerminationProtection:
191
+ found.autoScalingGroupProvider?.managedTerminationProtection,
192
+ managedDraining: found.autoScalingGroupProvider?.managedDraining,
193
+ tags: fromEcsTags(found.tags),
194
+ };
195
+ }),
196
+
197
+ create: Effect.fn(function* ({ id, news, session }) {
198
+ const region = yield* Region;
199
+ const { accountId } = yield* AWSEnvironment;
200
+ const name = yield* toName(id, news);
201
+ const internalTags = yield* createInternalTags(id);
202
+ const tags = { ...internalTags, ...news.tags };
203
+
204
+ const existing = yield* describe(name);
205
+ if (existing?.name && existing.capacityProviderArn) {
206
+ // The provider already exists. Only adopt when AdoptPolicy is true.
207
+ if (!adoptPolicy) {
208
+ return yield* Effect.die(
209
+ new Error(
210
+ `ECS capacity provider "${name}" already exists. ` +
211
+ `Provide adopt() on the resource or stack to adopt it.`,
212
+ ),
213
+ );
214
+ }
215
+ yield* session.note(
216
+ `Adopting existing ECS capacity provider ${name}`,
217
+ );
218
+
219
+ // If we own it (tags match), leave tags as-is. Otherwise apply
220
+ // our internal tags so future operations can identify ownership.
221
+ if (!hasTags(internalTags, fromEcsTags(existing.tags))) {
222
+ yield* ecs.tagResource({
223
+ resourceArn: existing.capacityProviderArn,
224
+ tags: toEcsTags(tags),
225
+ });
226
+ }
227
+
228
+ return {
229
+ capacityProviderArn:
230
+ existing.capacityProviderArn as CapacityProviderArn,
231
+ name: existing.name,
232
+ status: (existing.status ??
233
+ "ACTIVE") as ecs.CapacityProviderStatus,
234
+ updateStatus: existing.updateStatus,
235
+ autoScalingGroupArn:
236
+ existing.autoScalingGroupProvider?.autoScalingGroupArn ?? "",
237
+ managedScaling:
238
+ existing.autoScalingGroupProvider?.managedScaling,
239
+ managedTerminationProtection:
240
+ existing.autoScalingGroupProvider
241
+ ?.managedTerminationProtection,
242
+ managedDraining:
243
+ existing.autoScalingGroupProvider?.managedDraining,
244
+ tags: { ...fromEcsTags(existing.tags), ...tags },
245
+ };
246
+ }
247
+
248
+ const autoScalingGroupArn = news.autoScalingGroupArn as string;
249
+ const created = yield* ecs.createCapacityProvider({
250
+ name,
251
+ autoScalingGroupProvider: {
252
+ autoScalingGroupArn,
253
+ managedScaling: news.managedScaling,
254
+ managedTerminationProtection: news.managedTerminationProtection,
255
+ managedDraining: news.managedDraining,
256
+ },
257
+ tags: toEcsTags(tags),
258
+ });
259
+
260
+ const provider = created.capacityProvider;
261
+ const capacityProviderArn = (provider?.capacityProviderArn ??
262
+ `arn:aws:ecs:${region}:${accountId}:capacity-provider/${name}`) as CapacityProviderArn;
263
+ yield* session.note(capacityProviderArn);
264
+
265
+ return {
266
+ capacityProviderArn,
267
+ name,
268
+ status: (provider?.status ?? "ACTIVE") as ecs.CapacityProviderStatus,
269
+ updateStatus: provider?.updateStatus,
270
+ autoScalingGroupArn,
271
+ managedScaling: news.managedScaling,
272
+ managedTerminationProtection: news.managedTerminationProtection,
273
+ managedDraining: news.managedDraining,
274
+ tags,
275
+ };
276
+ }),
277
+
278
+ update: Effect.fn(function* ({ id, news, olds, output, session }) {
279
+ const internalTags = yield* createInternalTags(id);
280
+ const newTags = { ...internalTags, ...news.tags };
281
+ const oldTags = { ...internalTags, ...olds.tags };
282
+
283
+ yield* ecs.updateCapacityProvider({
284
+ name: output.name,
285
+ autoScalingGroupProvider: {
286
+ managedScaling: news.managedScaling,
287
+ managedTerminationProtection: news.managedTerminationProtection,
288
+ managedDraining: news.managedDraining,
289
+ },
290
+ });
291
+
292
+ const { removed, upsert } = diffTags(oldTags, newTags);
293
+ if (upsert.length > 0) {
294
+ yield* ecs.tagResource({
295
+ resourceArn: output.capacityProviderArn,
296
+ tags: upsert.map((t) => ({ key: t.Key, value: t.Value })),
297
+ });
298
+ }
299
+ if (removed.length > 0) {
300
+ yield* ecs.untagResource({
301
+ resourceArn: output.capacityProviderArn,
302
+ tagKeys: removed,
303
+ });
304
+ }
305
+
306
+ const found = yield* describe(output.name);
307
+ yield* session.note(output.capacityProviderArn);
308
+ return {
309
+ ...output,
310
+ status: (found?.status ?? output.status) as ecs.CapacityProviderStatus,
311
+ updateStatus: found?.updateStatus ?? output.updateStatus,
312
+ managedScaling:
313
+ found?.autoScalingGroupProvider?.managedScaling ??
314
+ news.managedScaling,
315
+ managedTerminationProtection:
316
+ found?.autoScalingGroupProvider?.managedTerminationProtection ??
317
+ news.managedTerminationProtection,
318
+ managedDraining:
319
+ found?.autoScalingGroupProvider?.managedDraining ??
320
+ news.managedDraining,
321
+ tags: newTags,
322
+ };
323
+ }),
324
+
325
+ delete: Effect.fn(function* ({ output }) {
326
+ yield* ecs
327
+ .deleteCapacityProvider({ capacityProvider: output.name })
328
+ .pipe(
329
+ // Already gone — treat as success.
330
+ Effect.catchTag("InvalidParameterException", () => Effect.void),
331
+ Effect.catchTag("ClientException", () => Effect.void),
332
+ );
333
+ }),
334
+ };
335
+ }),
336
+ );
@@ -1,3 +1,7 @@
1
+ export {
2
+ CapacityProvider,
3
+ CapacityProviderProvider,
4
+ } from "./CapacityProvider.ts";
1
5
  export { Cluster, ClusterProvider } from "./Cluster.ts";
2
6
  export {
3
7
  DescribeTasks,