LAMBDA Configuration to invoke script to STOP & START RDS Instance
STEP1
==============IAM Policy============
You will have to create and IAM Policy. Please follow the below steps:
1. Open link https://console.aws.amazon.com/iam
2. Select Policies -> Create Policy -> Click on JSON and past the below JSON code in the 'Policy Document' field:
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "rds:*" ], "Effect": "Allow", "Resource": "*" } ] }
3. Click on 'Review policy'
4. Enter a name for your IAM Policy and Description form example LambdaPolicy
5. Click on 'Create policy'.
STEP2
==============IAM Role for Lambda============
Now, you will have to create a role which will be associated with lambda function in order to allow it to manage RDS instances.
1. Open link https://console.aws.amazon.com/iam
2. Navigate to Roles -> Create Role.
3. In 'AWS Service' select 'Lambda'. Click on 'Next: Permissions'.
4. Select the policy you created previously 'LambdaPolicy' and click on 'Next: Review'.
5. Put 'LambdaRDSManagement' as a Role Name, set some description and press 'Create Role'.
STEP3
==============Lambda Function==============
Now you have to create lambda function which will manage the RDS instance.
1. Open Lambda console in the same region where your RDS instance resides https://console.aws.amazon.com/lambda
2. Click on 'Create function'
3. Select 'Author from Scratch'.
3. In name for example let's call the lambda function 'StartStopInstance'.
--> In Runtime choose Node.js 8.10
--> In role choose 'Choose an existing role'
--> In Existing role drop down menu choose 'LambdaRDSManagement'
--> Press 'Create function'
4. You will land on 'Configuration' tab of your function's page.
5. In 'Function code' section under 'Code entry type' choose Upload a .zip file, and attach Codes.zip file which I have attached to the correspondence
--> In Runtime choose Node.js 8.10
--> In Handler choose index.handler
6. Click on 'Save' at the top.
Now we have created a lambda function that can stop your RDS instance.
TESTING
Please note to test that your RDS instance needs to be in active state, not stopped, but if it is stopped, change the action to start "action": "start" and the function will start the instance. ************************************************
Go to the top right corner and choose from 'Test'. In the ' Configure test event' page select 'Create new test event' and give it a name 'StopRDSinstance' and pass the below details, just change ‘sample-test-instance’ to your instance name, and click create.
{ "instances": [ "sample-test-instance" ], "action": "stop" }
Now if the function executes properly, you will be able to see your RDS instance stopping or starting, depend what option you have chosen.
********************************************************
STEP4
==============Cloudwatch rule to schedule lambda=========
Now you need to setup CloudWatch rules to trigger our lambda function on schedule.
--> Stop Instance
1. Open Cloudwatch console in the same region where your RDS instance resides https://us-east-1.console.aws.amazon.com/cloudwatch/
2. Navigate to 'Rules' -> 'Create Rule'.
3. Select 'Schedule' instead of default Event Pattern.
4. Select 'Cron expression'. You will have to create a cron expression according to your requirement.
For more details on how to create a cron expression, you can refer to link [1].
For example:
010 13 ? * THU *
The above cron expression will invoke our function every Thursday at 13:10:00 GMT.
5. After you set cron time, click on 'Add Target' and choose 'Lambda function' as a Target in place of default 'SNS Topic', and pick your newly create lambda function 'StartStopInstance'. Then in 'Configure Input' section select Constanct (JSON text)
{ "instances": [ "SQLServerInstance" ], "action": "stop" }
6. Click on 'Configure Details' -> 'Update rule'.
--> Start instance - perform the same steps as above just change point 4 and 5 as per below
For example:
0 8 ? * THU *
The above cron expression will invoke our function every Thursday at 08:05:00 GMT.
After you set cron time, click on 'Add Target' and choose 'Lambda function' as a Target in place of default 'SNS Topic', and pick your newly create lambda function 'StartStopInstance'. Then in 'Configure Input' section select Constanct (JSON text)
{ "instances": [ "SQLServerInstance" ], "action": "start" }
When you perform the above settings you will control when to start the instance and when to stop. The process will be automated
[1]
CronMaker
http://www.cronmaker.com/
Schedule Expressions Using Rate or Cron - https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html
Schedule Expressions for Rules
https://docs.aws.amazon.com/console/cloudwatch/events/createrule
Click to Download CODES