Aurora Triggers to Call AWS lambda function

(0 comments)

Recently I needed to call my lambda function when CRUD happen on my aurora db table. AWS Aurora supports accesing AWS services. 
So If you want integrate such architecture , you can follow following step by step guide to make it work. 

1) Create RDS & Lambda full access Role with principal as "rds.amazonaws.com". ( arn:aws:iam::<account_id>:role/RDS-Lambda-Access )
2) Edit aurora parameter group and assign ARN of 1)
3) Edit aurora Clustor and also from `Managed IAM Roles` assign role created in 1).
4) Rebooted aurora instance.

Now you need to create stored procedure that will invoke your lambda function.

/********** Stored Procedure to call lambda *******/

DROP PROCEDURE IF EXISTS invoke_lambda_func;
DELIMITER ;;
CREATE PROCEDURE invoke_lambda_func(IN table_name_v VARCHAR(255),
        IN pk_val VARCHAR(255)) LANGUAGE SQL
BEGIN
   CALL mysql.lambda_async(
      'arn:aws:lambda:<region>:<account_id>:function:<lambda-func-name>',
        CONCAT('{ "event_type": "invoke",
        '", "table_name_v" : "', table_name_v,
        '", "pk_val" : "', pk_val,
        '"}')
        );
END
;;
DELIMITER ;

To create trigger that call this stored procedure, your trigger will look like this.

DROP TRIGGER IF EXISTS TR_table_name_v_IRT;
DELIMITER ;;
CREATE TRIGGER TR_table_name_v_IRT
        AFTER INSERT ON table_name_v
        FOR EACH ROW
BEGIN
        SELECT NEW.id INTO @id_val;
        CALL invoke_lambda_func('table_name_v', @id_val);
END
;;
DELIMITER ;

Thats It.
Happy Serverless !

Current rating: 4.3

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required

captcha

required