Skip to content

Instantly share code, notes, and snippets.

@brianfoody
Last active October 28, 2022 12:21
Show Gist options
  • Save brianfoody/04dc7855fb03b19fb53a5f8ca1cee430 to your computer and use it in GitHub Desktop.
Save brianfoody/04dc7855fb03b19fb53a5f8ca1cee430 to your computer and use it in GitHub Desktop.
Aurora v2 Lambda Invoke
-- Docs: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/PostgreSQL-Lambda.html
-- Add the lambda extension
CREATE EXTENSION IF NOT EXISTS aws_lambda CASCADE;
-- Add a trigger to a table
create trigger item_insert_trigger after
insert
on
public.item for each row execute function item_insert_entry();
-- function to handle it which invokes lambda
CREATE OR REPLACE FUNCTION public.item_insert_entry()
RETURNS TABLE(status_code integer, payload character varying)
LANGUAGE plpgsql
AS $function$
BEGIN
SELECT status_code,
payload
FROM aws_lambda.invoke(
aws_commons.create_lambda_function_arn(
'arn:aws:lambda:ap-southeast-2:123456:function:aurora_insert_handler_fn',
'ap-southeast-2'
),
-- Not sure about the syntax to parse NEW.* to string heree
NEW.*::json
);
END;
$function$
// VPC
1. You need to create a VPC endpoint if your rds is in a priivate subnet even if lambda also is
2. Allow rds security group incoming on port 443 to lambda security group
3. Probably need a VPC endpoint for the lambda to then invoke other servicees
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment