Skip to content

AWS.CredentialBroker reference

Source: packages/aws/src/CredentialBroker/CredentialProviderPool.ts

An AWS Credential Broker credential provider pool: the container for credential providers, and the place their encryption is configured.

Provisioning is asynchronous. A deploy waits for the pool to reach READY and a destroy waits until it is gone.

A pool encrypted with the service-owned key

import * as AWS from "@alchemy.run/aws";
const pool = yield* AWS.CredentialBroker.CredentialProviderPool("Credentials");

A pool encrypted with your own KMS key

const key = yield* AWS.KMS.Key("CredentialsKey");
const pool = yield* AWS.CredentialBroker.CredentialProviderPool("Credentials", {
kmsKeyIdentifier: key.keyArn,
});
const openai = yield* AWS.CredentialBroker.CredentialProvider("OpenAI", {
credentialProviderPoolId: pool.credentialProviderPoolId,
apiKey: Redacted.make("sk-..."),
});

CredentialProviderPool: Looking up an existing pool

Section titled “CredentialProviderPool: Looking up an existing pool”

CredentialProviderPool.lookup references a pool Alchemy doesn’t manage, found by id or by name. It is read during plan/deploy and never created, updated or deleted.

const shared = AWS.CredentialBroker.CredentialProviderPool.lookup({ name: "shared" });
const provider = yield* AWS.CredentialBroker.CredentialProvider("OpenAI", {
credentialProviderPoolId: shared.credentialProviderPoolId,
apiKey: Redacted.make("sk-..."),
});

Source: packages/aws/src/CredentialBroker/GetApiKey.ts

Runtime binding for credentialbroker:GetApiKey.

Bind it to an API key CredentialProvider to get a callable that returns the provider’s key. The function is granted credentialbroker:GetApiKey on the provider. The broker reads the key from the provider’s Secrets Manager secret with the caller’s own permissions, so the function is also granted secretsmanager:GetSecretValue on that one secret, for requests made through the broker only (aws:CalledVia): it can’t read the secret directly. Provide the implementation with Effect.provide(AWS.CredentialBroker.GetApiKeyHttp).

export default ApiFunction.make(
{ main: import.meta.url, functionUrl: true },
Effect.gen(function* () {
const pool = yield* AWS.CredentialBroker.CredentialProviderPool("Credentials");
const openai = yield* AWS.CredentialBroker.CredentialProvider("OpenAI", {
credentialProviderPoolId: pool.credentialProviderPoolId,
apiKey: Redacted.make("sk-..."),
});
// init: bind the operation to the provider
const getApiKey = yield* AWS.CredentialBroker.GetApiKey(openai);
return {
fetch: Effect.gen(function* () {
// runtime: the key comes back Redacted
const { apiKey } = yield* getApiKey();
const response = yield* HttpClient.get("https://api.openai.com/v1/models", {
headers: { authorization: `Bearer ${Redacted.value(apiKey)}` },
});
return HttpServerResponse.text(String(response.status));
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(AWS.CredentialBroker.GetApiKeyHttp)),
);

Source: packages/aws/src/CredentialBroker/GetOAuthToken.ts

Runtime binding for credentialbroker:GetOAuthToken.

Bind it to an OAuth CredentialProvider to get a callable that returns an access token. The broker authenticates to the provider’s authorization server with the client credentials configured on the provider, so the function never handles the client secret. It is granted credentialbroker:GetOAuthToken on the provider. The broker reads the client secret with the caller’s own permissions, so the function is also granted secretsmanager:GetSecretValue on the provider’s secrets, for requests made through the broker only (aws:CalledVia). Provide the implementation with Effect.provide(AWS.CredentialBroker.GetOAuthTokenHttp).

The broker caches a token until it expires, for at most an hour. Pass forceAuthentication: true to request a fresh one. grantType must be one the provider allows.

A token for the workload itself

// init: bind the operation to the provider
const getOAuthToken = yield* AWS.CredentialBroker.GetOAuthToken(billing);
// runtime: the token comes back Redacted
const { accessToken, expiresAt } = yield* getOAuthToken({
grantType: "CLIENT_CREDENTIALS",
scopes: ["invoices/read"],
});

A token for an end user who has consented

// `sessionUri` names a consent session the user has completed
const { accessToken } = yield* getOAuthToken({
grantType: "AUTHORIZATION_CODE",
sessionUri,
});