My new favorite way to trigger lambdas from Tulip

Previously I set up a private API gateway and then accessed it through a VPC Endpoint, this works but is a lot to setup. https://michaelellerbeck.com/2020/11/02/sending-an-sms-to-any-phone-number-from-tulip/

Since we are using Aurora Postgres, you can actually execute a lambda from postgres.

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/PostgreSQL-Lambda.html

So let’s say we had a lambda that sends an SMS, it would be pretty similar to the one used by the API gateway but you don’t need to use queryStringParameters. (It will need SNS IAM access)

Grant your rds-lambda-role permission to execute the lambda. Then you can call it from postgres like this

SELECT * FROM aws_lambda.invoke(aws_commons.create_lambda_function_arn('your_function', 'us-east-1'),
'{
  "phone_number": "11111111111",
  "message": "hello from postgres"
}'::json
);

Super neat!

To make it more user friendly you can make a postgres function to wrap it.

CREATE OR REPLACE FUNCTION public.send_sms(phone_number text, message text)
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
declare

  rows_affected int;
  create_json TEXT;
  response TEXT;
  err_context text;
 
  
begin
RAISE INFO 'Started';	  
  
-- Create Json	  
select
	into
	create_json row_to_json(t)
from
	(
	select
	   phone_number,
	   message
	   ) t;
	  
SELECT into response payload FROM aws_lambda.invoke(aws_commons.create_lambda_function_arn('your_function', 'us-east-1'),
create_json::json,'RequestResponse'
);

RAISE INFO 'response:%',response;

-- parse the response
select into response response::json->'response';
-- remove qutoes
select into response replace(response,'"','');

if response = 'sent' then 
	return 1;
else
	return 0;
END IF;

end;
$function$
;

Then to call it you simply need to use

select public.send_sms('11111111111','hello from postgres') as status

So then you use this from within Tulip like this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s