| 1 | import type * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import type { Input } from "../../Input.ts"; |
| 4 | import type { RuntimeContext } from "../../RuntimeContext.ts"; |
| 5 | import { connectEnvPrefix as makeConnectEnvPrefix } from "../Connection/internal.ts"; |
| 6 | import type { SecurityGroupId } from "../EC2/SecurityGroup.ts"; |
| 7 | import type { SubnetId } from "../EC2/Subnet.ts"; |
| 8 | import type { Cluster } from "./Cluster.ts"; |
| 9 | import type { User } from "./User.ts"; |
| 10 | |
| 11 | /** |
| 12 | * Connection descriptor for a MemoryDB cluster. Alchemy does not bundle a |
| 13 | * valkey/redis client — pass these settings to the cluster-mode client of |
| 14 | * your choice (`iovalkey`, `ioredis`, ...). |
| 15 | */ |
| 16 | export interface ClusterConnectionInfo { |
| 17 | /** Cluster (configuration) endpoint hostname. */ |
| 18 | host: string; |
| 19 | /** Cluster endpoint port (6379 by default). */ |
| 20 | port: number; |
| 21 | /** |
| 22 | * Whether the cluster requires TLS connections (MemoryDB clusters default |
| 23 | * to in-transit encryption on). |
| 24 | */ |
| 25 | tls: boolean; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Environment variable prefix under which {@link Connect} publishes the |
| 30 | * cluster endpoint on the host Function, derived from the cluster's logical |
| 31 | * ID. A cluster with logical ID `SessionStore` yields `MEMORYDB_SESSIONSTORE` |
| 32 | * and the variables `MEMORYDB_SESSIONSTORE_HOST`, |
| 33 | * `MEMORYDB_SESSIONSTORE_PORT`, and `MEMORYDB_SESSIONSTORE_TLS`. |
| 34 | */ |
| 35 | export const connectEnvPrefix = (logicalId: string): string => |
| 36 | makeConnectEnvPrefix("MEMORYDB", logicalId); |
| 37 | |
| 38 | export interface ConnectOptions { |
| 39 | /** |
| 40 | * MemoryDB {@link User}s the host authenticates as via IAM authentication |
| 41 | * (`authenticationMode: { type: "iam" }`). Each listed user's ARN is added |
| 42 | * to the `memorydb:Connect` grant — IAM auth authorizes against both the |
| 43 | * cluster and the user. Password-authenticated users need no IAM grant; |
| 44 | * omit this option and supply the password to your client instead. |
| 45 | */ |
| 46 | users?: User[]; |
| 47 | /** |
| 48 | * Subnets the host Function should be attached to so it can open a socket |
| 49 | * to the cluster — MemoryDB is VPC-only. Wired through the host's `vpc` |
| 50 | * binding channel, equivalent to setting the Function's `vpc` prop. |
| 51 | */ |
| 52 | subnetIds?: Input<SubnetId[]>; |
| 53 | /** |
| 54 | * Security groups for the host Function's VPC attachment. The referenced |
| 55 | * groups must be allowed ingress on the cluster port by the cluster's |
| 56 | * security groups. |
| 57 | */ |
| 58 | securityGroupIds?: Input<SecurityGroupId[]>; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Runtime binding that resolves connection settings for a MemoryDB |
| 63 | * {@link Cluster}. |
| 64 | * |
| 65 | * At deploy time it publishes the cluster endpoint as |
| 66 | * `MEMORYDB_{LOGICAL_ID}_{HOST,PORT,TLS}` environment variables on the host |
| 67 | * Function and grants `memorydb:Connect` on the cluster (plus any IAM-auth |
| 68 | * {@link User}s passed via `options.users`); at runtime it resolves the same |
| 69 | * values into a typed {@link ClusterConnectionInfo}. Network access is |
| 70 | * governed by VPC security groups — the host Function must: |
| 71 | * |
| 72 | * 1. be attached to the cluster's VPC (`vpc: { subnetIds, securityGroupIds }` |
| 73 | * or the binding's `subnetIds`/`securityGroupIds` options), and |
| 74 | * 2. have a security group allowed ingress on the cluster's port by one of |
| 75 | * the cluster's `securityGroupIds`. |
| 76 | * |
| 77 | * Provide the implementation with `Effect.provide(AWS.MemoryDB.ConnectHttp)`. |
| 78 | * ### Connecting to a Cluster |
| 79 | * **Example:** Resolve Connection Info inside a Function |
| 80 | * ```typescript |
| 81 | * // init — publishes env vars, grants memorydb:Connect, attaches the VPC |
| 82 | * const connect = yield* AWS.MemoryDB.Connect(cluster, { |
| 83 | * users: [appUser], |
| 84 | * subnetIds: [subnetA.subnetId, subnetB.subnetId], |
| 85 | * securityGroupIds: [appSecurityGroup.groupId], |
| 86 | * }); |
| 87 | * |
| 88 | * // inside a handler: |
| 89 | * const { host, port, tls } = yield* connect; |
| 90 | * ``` |
| 91 | * |
| 92 | * @binding |
| 93 | */ |
| 94 | export interface Connect extends Binding.Service< |
| 95 | Connect, |
| 96 | "AWS.MemoryDB.Connect", |
| 97 | ( |
| 98 | cluster: Cluster, |
| 99 | options?: ConnectOptions, |
| 100 | ) => Effect.Effect< |
| 101 | Effect.Effect<ClusterConnectionInfo, never, RuntimeContext> |
| 102 | > |
| 103 | > {} |
| 104 | export const Connect = Binding.Service<Connect>("AWS.MemoryDB.Connect"); |