|
| 1 | +import { |
| 2 | + CloudFormationCustomResourceEvent, |
| 3 | + CloudFormationCustomResourceCreateEvent, |
| 4 | + CloudFormationCustomResourceUpdateEvent, |
| 5 | + CloudFormationCustomResourceDeleteEvent, |
| 6 | +} from "aws-lambda/trigger/cloudformation-custom-resource"; |
| 7 | + |
| 8 | +import { validateConnection, hashCode, getConnectedClient } from "./util"; |
| 9 | +import { Connection } from "./lambda.types"; |
| 10 | +import * as postgres from "./postgres"; |
| 11 | + |
| 12 | +interface Props { |
| 13 | + ServiceToken: string; |
| 14 | + Connection: Connection; |
| 15 | + Name: string; |
| 16 | + Plugin: string; |
| 17 | + RequiredPublication?: string; |
| 18 | +} |
| 19 | + |
| 20 | +export const handler = async (event: CloudFormationCustomResourceEvent) => { |
| 21 | + switch (event.RequestType) { |
| 22 | + case "Create": |
| 23 | + return handleCreate(event); |
| 24 | + case "Update": |
| 25 | + return handleUpdate(event); |
| 26 | + case "Delete": |
| 27 | + return handleDelete(event); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const handleCreate = async (event: CloudFormationCustomResourceCreateEvent) => { |
| 32 | + const props = event.ResourceProperties as Props; |
| 33 | + validateProps(props); |
| 34 | + await createSlotIfAbsent(props); |
| 35 | + return { |
| 36 | + PhysicalResourceId: generatePhysicalId(props), |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * A slot cannot be altered in place: its name is its identity and its plugin is |
| 42 | + * fixed at creation. A name change reports a new physical id, so CloudFormation |
| 43 | + * creates the new slot and then deletes the old resource. A plugin change under |
| 44 | + * the same name is refused, because the existing slot would silently keep its |
| 45 | + * old plugin. |
| 46 | + */ |
| 47 | +const handleUpdate = async (event: CloudFormationCustomResourceUpdateEvent) => { |
| 48 | + const props = event.ResourceProperties as Props; |
| 49 | + validateProps(props); |
| 50 | + |
| 51 | + const oldProps = event.OldResourceProperties as Props; |
| 52 | + const physicalResourceId = generatePhysicalId(props); |
| 53 | + const isDifferentSlot = physicalResourceId != event.PhysicalResourceId; |
| 54 | + |
| 55 | + if (isDifferentSlot) { |
| 56 | + await createSlotIfAbsent(props); |
| 57 | + return { PhysicalResourceId: physicalResourceId }; |
| 58 | + } |
| 59 | + |
| 60 | + if (props.Plugin != oldProps.Plugin) { |
| 61 | + throw new Error( |
| 62 | + `The plugin of replication slot "${props.Name}" cannot change in place; drop and recreate the slot deliberately instead` |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return { PhysicalResourceId: physicalResourceId }; |
| 67 | +}; |
| 68 | + |
| 69 | +const handleDelete = async (event: CloudFormationCustomResourceDeleteEvent) => { |
| 70 | + const props = event.ResourceProperties as Props; |
| 71 | + validateProps(props); |
| 72 | + |
| 73 | + console.log("Dropping replication slot", props.Name); |
| 74 | + const client = await getConnectedClient(props.Connection); |
| 75 | + |
| 76 | + try { |
| 77 | + await postgres.dropReplicationSlot({ client, name: props.Name }); |
| 78 | + } finally { |
| 79 | + await client.end(); |
| 80 | + } |
| 81 | + |
| 82 | + return {}; |
| 83 | +}; |
| 84 | + |
| 85 | +const validateProps = (props: Props) => { |
| 86 | + if (!("Connection" in props)) { |
| 87 | + throw "Connection property is required"; |
| 88 | + } |
| 89 | + validateConnection(props.Connection); |
| 90 | + |
| 91 | + if (!("Name" in props)) { |
| 92 | + throw "Name property is required"; |
| 93 | + } |
| 94 | + if (!("Plugin" in props)) { |
| 95 | + throw "Plugin property is required"; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +const generatePhysicalId = (props: Props): string => { |
| 100 | + const { Host, Port } = props.Connection; |
| 101 | + const suffix = Math.abs(hashCode(`${Host}-${Port}-${props.Name}`)); |
| 102 | + return `replication-slot-${suffix}`; |
| 103 | +}; |
| 104 | + |
| 105 | +const createSlotIfAbsent = async (props: Props) => { |
| 106 | + const client = await getConnectedClient(props.Connection); |
| 107 | + |
| 108 | + try { |
| 109 | + if (props.RequiredPublication) { |
| 110 | + const { rows } = await client.query( |
| 111 | + "SELECT 1 FROM pg_publication WHERE pubname = $1", |
| 112 | + [props.RequiredPublication] |
| 113 | + ); |
| 114 | + if (rows.length === 0) { |
| 115 | + // A slot created before its publication decodes from a catalog snapshot |
| 116 | + // in which the publication does not exist, and replication then fails |
| 117 | + // continuously, so refusing here turns a subtle runtime failure into a |
| 118 | + // loud deploy failure: |
| 119 | + throw new Error( |
| 120 | + `publication "${props.RequiredPublication}" does not exist in database "${props.Connection.Database}"; deploy the migration that creates it before this slot` |
| 121 | + ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // `pg_replication_slots` lists the whole cluster, so a slot of the same |
| 126 | + // name decoding another database must not be adopted as this one; letting |
| 127 | + // Postgres raise "replication slot already exists" is the loud failure: |
| 128 | + const { rows: existing } = await client.query( |
| 129 | + "SELECT 1 FROM pg_replication_slots WHERE slot_name = $1 AND database = current_database()", |
| 130 | + [props.Name] |
| 131 | + ); |
| 132 | + if (existing.length === 0) { |
| 133 | + console.log("Creating replication slot", props.Name); |
| 134 | + await postgres.createReplicationSlot({ |
| 135 | + client, |
| 136 | + name: props.Name, |
| 137 | + plugin: props.Plugin, |
| 138 | + }); |
| 139 | + } else { |
| 140 | + console.log("Replication slot already exists", props.Name); |
| 141 | + } |
| 142 | + } finally { |
| 143 | + await client.end(); |
| 144 | + } |
| 145 | +}; |
0 commit comments