1import * as AWS from "@/AWS";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6/**
7 * Every resource the serverless story is composed of, shared between the
8 * API function (which binds to all of them), the worker function (which
9 * consumes the jobs queue), and the test's stack program (which wires the
10 * HTTP API's JWT authorizer to the pool). Declared once as a
11 * Context.Service so all consumers observe the SAME logical resources.
12 */
13export class ServerlessResources extends Context.Service<
14 ServerlessResources,
15 {
16 pool: AWS.Cognito.UserPool;
17 client: AWS.Cognito.UserPoolClient;
18 table: AWS.DynamoDB.Table;
19 bucket: AWS.S3.Bucket;
20 jobsQueue: AWS.SQS.Queue;
21 resultsQueue: AWS.SQS.Queue;
22 machine: AWS.StepFunctions.StateMachine;
23 }
24>()("ServerlessResources") {}
25
26export const ServerlessResourcesLive = Layer.effect(
27 ServerlessResources,
28 Effect.gen(function* () {
29 const pool = yield* AWS.Cognito.UserPool("SmokeUserPool", {
30 passwordPolicy: {
31 minimumLength: 12,
32 requireSymbols: false,
33 },
34 accountRecovery: [{ name: "admin_only", priority: 1 }],
35 tags: { Purpose: "serverless-smoke-fixture" },
36 });
37 const client = yield* AWS.Cognito.UserPoolClient("SmokeUserPoolClient", {
38 userPoolId: pool.userPoolId,
39 explicitAuthFlows: [
40 "ALLOW_USER_PASSWORD_AUTH",
41 "ALLOW_ADMIN_USER_PASSWORD_AUTH",
42 "ALLOW_REFRESH_TOKEN_AUTH",
43 ],
44 });
45 const table = yield* AWS.DynamoDB.Table("SmokeTable", {
46 partitionKey: "pk",
47 sortKey: "sk",
48 attributes: { pk: "S", sk: "S" },
49 });
50 const bucket = yield* AWS.S3.Bucket("SmokeBucket", {
51 forceDestroy: true,
52 });
53 const jobsQueue = yield* AWS.SQS.Queue("SmokeJobsQueue");
54 const resultsQueue = yield* AWS.SQS.Queue("SmokeResultsQueue");
55 const machine = yield* AWS.StepFunctions.StateMachine(
56 "SmokeExpressMachine",
57 {
58 type: "EXPRESS",
59 definition: {
60 StartAt: "Compute",
61 States: {
62 Compute: {
63 Type: "Pass",
64 Parameters: { "echo.$": "$", computed: true },
65 End: true,
66 },
67 },
68 },
69 },
70 );
71 return { pool, client, table, bucket, jobsQueue, resultsQueue, machine };
72 }),
73);