--- /** * "One line does three jobs": for each tool, the code you write to read * and write one DynamoDB table from a Lambda, the config it deploys (IAM * policy + env var), and what the function's code gets at runtime. * Line colors show where each piece comes from: in Alchemy, one capability * line produces its policy statement, the table name, and a typed client. * SST's link over-grants; Pulumi and Terraform deploy the policy and env var * you write line by line, and define no client. */ import { highlightTS } from "../marketing/highlightTS"; // get / put: produced by that Alchemy capability line; both: the shared // env var; link: generated by SST's link; over: access the code never uses; // hand: SDK calls written by hand, unchecked; policy / env: IAM and env // config you define line by line. type Src = "get" | "put" | "both" | "link" | "over" | "hand" | "policy" | "env"; type Lang = "ts" | "hcl" | "yaml"; type Line = { text: string; src?: Src; write?: boolean; file?: boolean; lang?: Lang }; type ToolSet = { id: "alchemy" | "sst" | "pulumi" | "terraform"; code: Line[]; config: Line[]; runtime: Line[]; }; // Pulumi and Terraform deploy exactly the policy you write, so every config // line maps back to a line of code. const HAND_CONFIG = (arn: string, env: string): Line[] => [ { text: "# deployed config for api" }, { text: "Role:", src: "policy" }, { text: " Policy:", src: "policy" }, { text: ' Version: "2012-10-17"', src: "policy" }, { text: " Statement:", src: "policy" }, { text: " - Effect: Allow", src: "policy" }, { text: " Action: [dynamodb:GetItem, dynamodb:PutItem]", src: "policy" }, { text: ` Resource: ${arn}`, src: "policy" }, { text: "Environment:", src: "env" }, { text: " Variables:", src: "env" }, { text: ` ${env}`, src: "env" }, ]; const NO_CLIENT: Line[] = [ { text: "// runtime: no client definition" }, { text: "// the handler builds its own SDK calls" }, { text: "// against process.env.TABLE_NAME" }, ]; const SETS: ToolSet[] = [ { id: "alchemy", code: [ { text: "src/api.ts", file: true }, { text: "export default class Api extends Lambda.Function()(" }, { text: ' "Api",' }, { text: " { main: import.meta.url }," }, { text: " Effect.gen(function* () {" }, { text: ' const sessions = yield* DynamoDB.Table("Sessions");' }, { text: " const getSession = yield* DynamoDB.GetItem(sessions);", src: "get" }, { text: " const saveSession = yield* DynamoDB.PutItem(sessions);", src: "put", write: true }, { text: " return {" }, { text: " fetch: Effect.gen(function* () {" }, { text: " const { Item } = yield* getSession({ Key });" }, { text: " yield* saveSession({ Item: next });", write: true }, { text: " })," }, { text: " };" }, { text: " })," }, { text: ") {}" }, ], config: [ { text: "# deployed config for Api" }, { text: "Role:" }, { text: " Policy:" }, { text: ' Version: "2012-10-17"' }, { text: " Statement:" }, { text: " - Effect: Allow", src: "get" }, { text: " Action: dynamodb:GetItem", src: "get" }, { text: " Resource: arn:…:table/api-sessions-8f2c", src: "get" }, { text: " - Effect: Allow", src: "put", write: true }, { text: " Action: dynamodb:PutItem", src: "put", write: true }, { text: " Resource: arn:…:table/api-sessions-8f2c", src: "put", write: true }, { text: "Environment:" }, { text: " Variables:" }, { text: " Sessions_tableName: api-sessions-8f2c", src: "both" }, ], runtime: [ { text: "// runtime: typed clients" }, { text: "const getSession: (", src: "get" }, { text: " request: GetItemRequest,", src: "get" }, { text: ") => Effect", src: "get" }, { text: "const saveSession: (", src: "put", write: true }, { text: " request: PutItemRequest,", src: "put", write: true }, { text: ") => Effect", src: "put", write: true }, ], }, { id: "sst", code: [ { text: "sst.config.ts", file: true }, { text: 'const sessions = new sst.aws.Dynamo("Sessions", { … });' }, { text: 'new sst.aws.Function("Api", {' }, { text: ' handler: "src/api.handler",' }, { text: " link: [sessions],", src: "link" }, { text: "});" }, { text: "src/api.ts", file: true }, { text: 'import { Resource } from "sst";' }, { text: "const client = new DynamoDBClient({});" }, { text: "export const handler = async () => {" }, { text: " const TableName = Resource.Sessions.name;", src: "link" }, { text: " const { Item } = await client.send(", src: "hand" }, { text: " new GetItemCommand({ TableName, Key }));", src: "hand" }, { text: " await client.send(", src: "hand" }, { text: " new PutItemCommand({ TableName, Item: next }));", src: "hand" }, { text: "};" }, ], config: [ { text: "# deployed config for Api" }, { text: "Role:" }, { text: " Policy:" }, { text: ' Version: "2012-10-17"' }, { text: " Statement:" }, { text: " - Effect: Allow", src: "link" }, { text: " # every DynamoDB action, DeleteTable too", src: "over" }, { text: ' Action: "dynamodb:*"', src: "over" }, { text: " Resource:", src: "link" }, { text: " - arn:…:table/app-sessions", src: "link" }, { text: " - arn:…:table/app-sessions/*", src: "link" }, { text: "Environment:" }, { text: " Variables:" }, { text: ` SST_RESOURCE_Sessions: '{"name":"app-sessions",…}'`, src: "link" }, ], runtime: [ { text: "// runtime" }, { text: "const TableName: string; // typed by link", src: "link" }, { text: "// no client definition: new DynamoDBClient()", src: "hand" }, { text: "// and send() any command, allowed or not", src: "hand" }, ], }, { id: "pulumi", code: [ { text: "index.ts", file: true }, { text: 'const sessions = new aws.dynamodb.Table("sessions", { … });' }, { text: 'const role = new aws.iam.Role("api", { assumeRolePolicy: … });', src: "policy" }, { text: 'new aws.iam.RolePolicy("api", {', src: "policy" }, { text: " role: role.id,", src: "policy" }, { text: " policy: sessions.arn.apply((arn) => JSON.stringify({", src: "policy" }, { text: ' Version: "2012-10-17",', src: "policy" }, { text: " Statement: [{", src: "policy" }, { text: ' Effect: "Allow",', src: "policy" }, { text: ' Action: ["dynamodb:GetItem", "dynamodb:PutItem"],', src: "policy" }, { text: " Resource: arn,", src: "policy" }, { text: " }],", src: "policy" }, { text: " })),", src: "policy" }, { text: "});", src: "policy" }, { text: 'new aws.lambda.Function("api", {' }, { text: " role: role.arn," }, { text: " environment: {", src: "env" }, { text: " variables: { TABLE_NAME: sessions.name },", src: "env" }, { text: " },", src: "env" }, { text: "});" }, ], config: HAND_CONFIG("arn:…:table/sessions-4d1a", "TABLE_NAME: sessions-4d1a"), runtime: NO_CLIENT, }, { id: "terraform", code: [ { text: "main.tf", file: true, lang: "hcl" }, { text: 'resource "aws_dynamodb_table" "sessions" { … }', lang: "hcl" }, { text: 'resource "aws_iam_role" "api" { assume_role_policy = … }', src: "policy", lang: "hcl" }, { text: 'resource "aws_iam_role_policy" "api" {', src: "policy", lang: "hcl" }, { text: " role = aws_iam_role.api.id", src: "policy", lang: "hcl" }, { text: " policy = jsonencode({", src: "policy", lang: "hcl" }, { text: ' Version = "2012-10-17"', src: "policy", lang: "hcl" }, { text: " Statement = [{", src: "policy", lang: "hcl" }, { text: ' Effect = "Allow"', src: "policy", lang: "hcl" }, { text: ' Action = ["dynamodb:GetItem", "dynamodb:PutItem"]', src: "policy", lang: "hcl" }, { text: " Resource = aws_dynamodb_table.sessions.arn", src: "policy", lang: "hcl" }, { text: " }]", src: "policy", lang: "hcl" }, { text: " })", src: "policy", lang: "hcl" }, { text: "}", src: "policy", lang: "hcl" }, { text: 'resource "aws_lambda_function" "api" {', lang: "hcl" }, { text: " role = aws_iam_role.api.arn", lang: "hcl" }, { text: " environment {", src: "env", lang: "hcl" }, { text: " variables = {", src: "env", lang: "hcl" }, { text: " TABLE_NAME = aws_dynamodb_table.sessions.name", src: "env", lang: "hcl" }, { text: " }", src: "env", lang: "hcl" }, { text: " }", src: "env", lang: "hcl" }, { text: "}", lang: "hcl" }, ], config: HAND_CONFIG("arn:…:table/sessions", "TABLE_NAME: sessions"), runtime: NO_CLIENT, }, ]; const esc = (t: string) => t.replace(/&/g, "&").replace(/ { if (/^\s*#/.test(t)) return `${esc(t)}`; const m = t.match(/^(\s*)(- )?([A-Za-z_]\w*):(\s.*|)$/); if (!m) { const d = t.match(/^(\s*)- (.*)$/); return d ? `${d[1]}- ${esc(d[2]!)}` : esc(t); } const [, indent, dash = "", key, rest] = m; const value = rest!.trim(); return ( indent + (dash ? `${dash}` : "") + `${esc(key!)}:` + (value ? ` ${esc(value)}` : "") ); }; const hcl = (t: string) => esc(t) .replace(/("[^"]*")/g, '$1') .replace(/^(\s*)(resource)\b/, '$1$2') .replace(/^(\s*)([A-Za-z_]\w*)(\s*=)/, '$1$2$3'); const render = (l: Line, lang: Lang) => l.file ? esc(l.text) : (l.lang ?? lang) === "hcl" ? hcl(l.text) : lang === "yaml" ? yaml(l.text) : highlightTS(l.text); // Classes for a line, plus run edges so each colored block gets a little // breathing room above and below. const classes = (lines: Line[], i: number) => { const l = lines[i]!; return [ "ol__line", l.src && `is-${l.src}`, l.write && "is-write", l.file && "is-file", l.src && lines[i - 1]?.src !== l.src && "is-run-start", l.src && lines[i + 1]?.src !== l.src && "is-run-end", ]; }; const label = { alchemy: "Alchemy", sst: "SST", pulumi: "Pulumi", terraform: "Terraform" }; ---
Show how each tool wires the function
{ SETS.map((set) => (
{
                set.code.map((l, i) => (
                  
                ))
              }
{
                  set.config.map((l, i) => (
                    
                  ))
                }
{
                  set.runtime.map((l, i) => (
                    
                  ))
                }
{set.id === "alchemy" && ( <>
  • GetItem grants read and returns getSession
  • PutItem grants write and returns saveSession

One line asks for read access. It grants exactly dynamodb:GetItem on this table, injects the table's name, and returns a typed client.

Add PutItem and its statement, its env var, and its client all follow. Remove a line and all three go with it.

)} {set.id === "sst" && ( <>
  • more access than the code uses
  • written by hand, unchecked

link writes the policy for you, but it grants dynamodb:* on the table, DeleteTable included, while the handler only calls GetItem and PutItem. There's no client definition, so the SDK calls are yours to keep in sync.

)} {(set.id === "pulumi" || set.id === "terraform") && ( <>
  • IAM policy: every line is yours to write
  • env var: yours to wire

{set.id === "pulumi" ? "Pulumi" : "Terraform"} deploys exactly the policy you define: each statement, action, and ARN in the config traces back to a line you wrote. There's no client definition, so nothing checks your handler's SDK calls against either.

)}
)) }