Make a Backup Bot Using AWS Lex, Lambda and Slack

Vinayak Pandey
2 min readOct 22, 2020

In this post, we’ll create a bot using Lex, Lambda, and Slack to take EC2 and RDS snapshots.

Step 1: Create a role for Lambda. For simplicity's sake, we’ll give the Administrator permission to this function so that we can use it across different functions.

Note: All the lambda functions that we’ll create, will use Python 3.6 as runtime and the role we created in step 1.

Step 2: Create a lambda function named ec2_validation and set the handler as validate.

For this function, use code given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/lex_bot/ec2_validation.py

Step 3: Create a lambda function named ec2_fulfilment with code given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/lex_bot/ec2_fulfilment.py

Set fulfilment_handler as the handler.

Step 4: Create a lambda function named rds_validation with code given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/lex_bot/rds_validate.py

Step 5: Create a lambda function named rds_fulfilment with code given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/lex_bot/rds_fulfilment.py

Step 6: Now go to the Lex console and create a bot by importing the JSON file given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/lex_bot/backupbot.json. You need to change your Lambda functions’ ARN in this file before you import it.

Edit: You need to grant Lex permission to invoke lambda.

 aws lambda add-permission --function-name ec2_validation --statement-id chatbot-fulfillment --action "lambda:InvokeFunction" --principal "lex.amazonaws.com"
aws lambda add-permission --function-name ec2_fulfilment--statement-id chatbot-fulfillment --action "lambda:InvokeFunction" --principal "lex.amazonaws.com"
aws lambda add-permission --function-name rds_validation --statement-id chatbot-fulfillment --action "lambda:InvokeFunction" --principal "lex.amazonaws.com"
aws lambda add-permission --function-name rds_fulfilment--statement-id chatbot-fulfillment --action "lambda:InvokeFunction" --principal "lex.amazonaws.com"

Now we are all set from the AWS side. We have a bot named backupBot which can take EC2 and RDS snapshots. You can test the bot from the Lex console.

Step 7: Launch 1 EC2 instance in us-east-1 with 2 volumes. We’ll use our bot to create a snapshot of these volumes

Step 8: Follow Step2 to Step6 given at https://docs.aws.amazon.com/lex/latest/dg/slack-bot-association.html to create a Slack application that integrates with our Lex bot. The instructions given here are pretty straightforward so I am not going to elaborate on them.

Once you are done with the integration, you’ll see your app in the Slack console. Let’s test it out.

Now go to AWS console and you should see your volume snapshot got created.

--

--