| 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 { ReplicationGroup } from "./ReplicationGroup.ts"; |
| 9 | |
| 10 | export interface ReplicationGroupConnectionInfo { |
| 11 | host: string; |
| 12 | port: number; |
| 13 | readerHost: string | undefined; |
| 14 | readerPort: number | undefined; |
| 15 | tls: boolean; |
| 16 | } |
| 17 | |
| 18 | export interface ConnectReplicationGroupOptions { |
| 19 | subnetIds?: Input<SubnetId[]>; |
| 20 | securityGroupIds?: Input<SecurityGroupId[]>; |
| 21 | } |
| 22 | |
| 23 | export const replicationGroupConnectEnvPrefix = (logicalId: string) => |
| 24 | makeConnectEnvPrefix("ELASTICACHE", logicalId); |
| 25 | |
| 26 | /** |
| 27 | * Runtime binding for a provisioned Valkey or Redis OSS replication group. |
| 28 | * |
| 29 | * At deploy time it publishes the primary endpoint and TLS setting as |
| 30 | * `ELASTICACHE_{LOGICAL_ID}_{HOST,PORT,TLS}` variables on the host Function. |
| 31 | * When replicas exist, it also publishes the reader endpoint. Passing VPC |
| 32 | * options attaches the host Lambda to the cache network. ElastiCache data |
| 33 | * plane access is controlled by security groups, not IAM. |
| 34 | * |
| 35 | * ### Connecting from a Lambda |
| 36 | * **Example:** Use the primary endpoint with iovalkey |
| 37 | * ```typescript |
| 38 | * const connect = yield* AWS.ElastiCache.ConnectReplicationGroup(cache, { |
| 39 | * subnetIds: privateSubnetIds, |
| 40 | * securityGroupIds: [functionSecurityGroup.groupId], |
| 41 | * }); |
| 42 | * |
| 43 | * // inside the handler: |
| 44 | * const { host, port, tls } = yield* connect; |
| 45 | * const client = new Valkey({ host, port, ...(tls ? { tls: {} } : {}) }); |
| 46 | * ``` |
| 47 | * |
| 48 | * @binding |
| 49 | */ |
| 50 | export interface ConnectReplicationGroup extends Binding.Service< |
| 51 | ConnectReplicationGroup, |
| 52 | "AWS.ElastiCache.ConnectReplicationGroup", |
| 53 | ( |
| 54 | group: ReplicationGroup, |
| 55 | options?: ConnectReplicationGroupOptions, |
| 56 | ) => Effect.Effect< |
| 57 | Effect.Effect<ReplicationGroupConnectionInfo, never, RuntimeContext> |
| 58 | > |
| 59 | > {} |
| 60 | export const ConnectReplicationGroup = Binding.Service<ConnectReplicationGroup>( |
| 61 | "AWS.ElastiCache.ConnectReplicationGroup", |
| 62 | ); |