The best way to control Claude Code, Codex and more from any computer or phone

main
md 78 lines 2.52 KB
Rendered Raw
1 # @alchemy.run/aws
2
3 AWS for [Alchemy](https://alchemy.run). Declare Lambda functions, S3, SQS, DynamoDB, ECS, EKS and 200 more services in the same TypeScript program as the code that uses them. Bindings write the least-privilege IAM policy for you.
4
5 ```sh
6 npm install alchemy @alchemy.run/aws effect
7 ```
8
9 A Lambda function binds two S3 operations on a bucket:
10
11 ```typescript
12 // src/api.ts
13 import * as AWS from "@alchemy.run/aws";
14 import * as Effect from "effect/Effect";
15 import { HttpServerRequest } from "effect/http/HttpServerRequest";
16 import * as HttpServerResponse from "effect/http/HttpServerResponse";
17 import * as Layer from "effect/Layer";
18
19 export default class Api extends AWS.Lambda.Function<Api>()(
20 "Api",
21 { main: import.meta.url, functionUrl: true },
22 Effect.gen(function* () {
23 const bucket = yield* AWS.S3.Bucket("Bucket");
24 // Each binding adds exactly one IAM permission to the function's role.
25 const putObject = yield* AWS.S3.PutObject(bucket);
26 const getObject = yield* AWS.S3.GetObject(bucket);
27
28 return {
29 fetch: Effect.gen(function* () {
30 const request = yield* HttpServerRequest;
31 const key = request.url.split("/").pop()!;
32 if (request.method === "PUT") {
33 yield* putObject({ Key: key, Body: yield* request.text });
34 return HttpServerResponse.empty({ status: 201 });
35 }
36 const object = yield* getObject({ Key: key }).pipe(
37 Effect.catchTag("NoSuchKey", () => Effect.succeed(undefined)),
38 );
39 if (!object?.Body) {
40 return HttpServerResponse.text("Not found", { status: 404 });
41 }
42 return HttpServerResponse.stream(object.Body);
43 }).pipe(Effect.orDie),
44 };
45 }).pipe(Effect.provide(Layer.mergeAll(AWS.S3.PutObjectHttp, AWS.S3.GetObjectHttp))),
46 ) {}
47 ```
48
49 The Stack deploys it:
50
51 ```typescript
52 // alchemy.run.ts
53 import * as Alchemy from "alchemy";
54 import * as AWS from "@alchemy.run/aws";
55 import * as Effect from "effect/Effect";
56 import Api from "./src/api.ts";
57
58 export default Alchemy.Stack(
59 "MyApp",
60 {
61 providers: AWS.providers(),
62 state: AWS.state(),
63 },
64 Effect.gen(function* () {
65 const api = yield* Api;
66 return { url: api.functionUrl };
67 }),
68 );
69 ```
70
71 `alchemy deploy` creates the bucket, the function, its role, and a policy that allows `s3:PutObject` and `s3:GetObject` on that bucket only.
72
73 ## Learn more
74
75 - [AWS on alchemy.run](https://alchemy.run/aws)
76 - [Setup](https://alchemy.run/aws/setup)
77 - [Tutorial](https://alchemy.run/aws/tutorial/part-1)
78 - [API reference](https://alchemy.run/providers)