Simulating RDS `aws_lambda.invoke_lambda` with Localstack

Viewed 79

I'm trying to simulate invoking lambda functions on my computer using LocalStack and PostgreSQL. In RDS, this can be done using the aws_lambda PostgreSQL extension. I was thinking of installing the aws_lambda in my local Postgre and make it trigger the lambda defined in LocalStack. However, I can't seem to find a way to install the aws_lambda extension.

Does anybody know if the extension is open source? Can I get my hands on it? If not, is there a way to simulate lambda invocation in Localstack?

1 Answers

You can use untrusted python in postgres ("plpythonu" extension) to emulate aws_lambda. I.e. something like:

CREATE LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION awslambda_fn(fn_name text, fn_args text)
    RETURNS text
    AS $$
     import boto3
     import boto.utils
     region=boto.utils.get_instance_identity()['document']['region']
     client=boto3.client('lambda',region)
     response=client.invoke(FunctionName=fn_name,
                            InvocationType='RequestResponse',
                            Payload=fn_args,
                           )
     r = response['Payload'].read()
     if ( 'FunctionError' in response ):
         raise Exception(r)
     return r
   $$ language plpythonu ;
GRANT EXECUTE ON FUNCTION awslambda_fn(text, text) TO PUBLIC;

But obviously need to copy the actual aws_lambda.invoke() API.

Related