Skip to content

Domain.AWS reference

Source: packages/domain/src/AWS/DynamoKeyStore.ts Kind: Layer · Provides: Domain.KeyStore

A Vault’s keys in a DynamoDB table, one item per subject. The table’s partition key is subject, a string. Leave point-in-time recovery and backups off for it: a restored table brings shredded keys back.

import * as AWS from "@alchemy.run/aws";
import * as Domain from "@alchemy.run/domain";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const Keys = AWS.DynamoDB.Table("VaultKeys", {
partitionKey: "subject",
attributes: { subject: "S" },
});
export const RootKey = AWS.KMS.Key("VaultRootKey");
export const Sealing = Domain.VaultLive.pipe(
Layer.provide(DomainAWS.DynamoKeyStore(Keys)),
Layer.provide(DomainAWS.KmsKeyWrap(RootKey)),
Layer.provide(
Layer.mergeAll(
AWS.DynamoDB.GetItemHttp,
AWS.DynamoDB.PutItemHttp,
AWS.DynamoDB.DeleteItemHttp,
AWS.KMS.GenerateDataKeyHttp,
AWS.KMS.DecryptHttp,
),
),
);

Source: packages/domain/src/AWS/KmsKeyWrap.ts Kind: Layer · Provides: Domain.KeyWrap

Wrap each subject’s key under an AWS KMS key. KMS makes the subject’s key (GenerateDataKey) and unwraps it (Decrypt), with the subject as the encryption context, so a stored key unwraps only for the subject it was made for. The root key never leaves KMS.

Every seal and open is one Decrypt call.

import * as AWS from "@alchemy.run/aws";
import * as Domain from "@alchemy.run/domain";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const Keys = AWS.DynamoDB.Table("VaultKeys", {
partitionKey: "subject",
attributes: { subject: "S" },
});
export const RootKey = AWS.KMS.Key("VaultRootKey");
export const Sealing = Domain.VaultLive.pipe(
Layer.provide(DomainAWS.DynamoKeyStore(Keys)),
Layer.provide(DomainAWS.KmsKeyWrap(RootKey)),
Layer.provide(
Layer.mergeAll(
AWS.DynamoDB.GetItemHttp,
AWS.DynamoDB.PutItemHttp,
AWS.DynamoDB.DeleteItemHttp,
AWS.KMS.GenerateDataKeyHttp,
AWS.KMS.DecryptHttp,
),
),
);

intermediateKey has each process keep one intermediate key in memory and wrap subject keys under it locally, so KMS is called once per intermediate key instead of once per seal and open. The intermediate key is in the process’s memory, KMS no longer logs each subject’s access, and a process cut off from KMS reads on for up to keep. Shredding is unchanged.

DomainAWS.KmsKeyWrap(RootKey, { intermediateKey: { keep: "5 minutes" } });

Source: packages/domain/src/AWS/KmsMacReceiptKey.ts Kind: Layer · Provides: Domain.ReceiptKey

Sign receipts with an HMAC key in AWS KMS (GenerateMac, checked with VerifyMac). The key never leaves KMS: no process ever holds it, and CloudTrail logs every signature and every check.

That costs one KMS request per receipt: one GenerateMac per command (and per commit of a Decision), and one VerifyMac per signed receipt a refused read passes back. At a million commands a month that is a million requests, and KMS throttles them with your account’s request quota. Use KmsReceiptKey to call KMS once per process every few minutes instead.

KmsMacReceiptKey: Signing receipts inside KMS

Section titled “KmsMacReceiptKey: Signing receipts inside KMS”
import * as AWS from "@alchemy.run/aws";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const ReceiptMacKey = AWS.KMS.Key("ReceiptMacKey", {
keySpec: "HMAC_256",
keyUsage: "GENERATE_VERIFY_MAC",
});
export const Receipts = DomainAWS.KmsMacReceiptKey(ReceiptMacKey).pipe(
Layer.provide(Layer.mergeAll(AWS.KMS.GenerateMacHttp, AWS.KMS.VerifyMacHttp)),
);

Source: packages/domain/src/AWS/KmsReceiptKey.ts Kind: Layer · Provides: Domain.ReceiptKey

Sign receipts with keys an AWS KMS key wraps, calling KMS once per signing key instead of once per receipt. Each process has KMS make a signing key (GenerateDataKey), signs with it in memory for keep, then makes another. A signature carries its key as KMS wrapped it, so any process checks it with one Decrypt the first time it sees that key, and keeps the key for keep.

KMS requests grow with processes, not traffic: each process makes one GenerateDataKey per keep, and at most one Decrypt per key it is shown, never more than one per refused read. Fifty processes with the default keep of 15 minutes make about 144,000 GenerateDataKey requests a month, however many commands they serve. What it trades: a signing key is in the process’s memory, CloudTrail logs one request per key rather than per receipt, and a process whose access to KMS is revoked signs and checks for up to keep more.

Use KmsMacReceiptKey when the key must never leave KMS, at one KMS request per receipt. See KMS pricing.

KmsReceiptKey: Signing receipts with keys KMS wraps

Section titled “KmsReceiptKey: Signing receipts with keys KMS wraps”
import * as AWS from "@alchemy.run/aws";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const ReceiptRootKey = AWS.KMS.Key("ReceiptRootKey");
export const Receipts = DomainAWS.KmsReceiptKey(ReceiptRootKey).pipe(
Layer.provide(Layer.mergeAll(AWS.KMS.GenerateDataKeyHttp, AWS.KMS.DecryptHttp)),
);

KmsReceiptKey: Tuning how often KMS is called

Section titled “KmsReceiptKey: Tuning how often KMS is called”

keep sets how long a process signs with one key and keeps each key it unwrapped. Raise it to call KMS less; lower it to cut a revoked process off sooner.

DomainAWS.KmsReceiptKey(ReceiptRootKey, { keep: "1 hour" });