We need three lambda function
1) Create RDS instance
2) Wait function - check that RDS instance is available or not
3) Create Snapshot
1) Create RDS instance
2) Wait function - check that RDS instance is available or not
3) Create Snapshot
1) Create RDS DB instance - :test_create_dbinstance
import boto3
import datetime
import time
def lambda_handler(event, context):
client = boto3.client('rds')
client.create_db_instance(DBInstanceIdentifier=’testRDS1234’,
AllocatedStorage =200,
DBInstanceClass='db.m4.large',
MasterUsername='sa',
MasterUserPassword=abcdert',
Port=1433,
Engine='sqlserver-se',
DBSubnetGroupName='test_rds_subnet_group',
LicenseModel='license-included'
)
return
2) Lambda function to check that RDS instance is
fully available or not - test_dbinstance_poll
import boto3
import datetime
import time
def lambda_handler(event, context):
client = boto3.client('rds')
status=False
response = client.describe_db_instances(DBInstanceIdentifier = ’testRDS1234’)
for
DBInstance in response['DBInstances']:
if (DBInstance['DBInstanceStatus'] == 'available'):
status=True
return status
3 Create snapshot - test_rds_snapshot
import boto3
import datetime
import time
import sys
def lambda_handler(event, context):
client = boto3.client('rds')
date=time.strftime("%Y-%m-%d-%H-%M-%S")
snapshot_name = ’testRDS1234’+date
print
(snapshot_name)
response = client.create_db_snapshot(
DBSnapshotIdentifier= snapshot_name,
DBInstanceIdentifier=’testRDS1234’,
Tags=[
{
'Key': ‘RDS’,
'Value': 'snapshot'
}
]
)
Now you have all 3 function. Now create step function and
execute
{
"Comment": "A state machine that submits a Job to AWS
Batch and monitors the Job until it completes.",
"StartAt": "createinstance",
"States": {
"createinstance": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<<accounted>>:function:test_create_dbinstance",
"ResultPath": "$.guid",
"Next": "Wait X Seconds"
},
"Wait X Seconds": {
"Type": "Wait",
"Seconds": 300,
"Next": "Get Job Status"
},
"Get Job Status": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<<accounted>>:function:test_dbinstance_poll",
"Next": "Job Complete?",
"InputPath": "$.guid",
"ResultPath": "$.status"
},
"Job Complete?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"StringEquals": "FAILED",
"Next": "Job Failed"
},
{
"Variable": "$.status",
"BooleanEquals": true,
"Next": "Get Final Job Status"
}
],
"Default": "Wait X Seconds"
},
"Job Failed": {
"Type": "Fail",
"Cause": "AWS Batch Job Failed",
"Error": "DescribeJob returned FAILED"
},
"Get Final Job Status": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:<<accounted>>:function:test_rds_snapshot",
"InputPath": "$.guid",
"End": true
}
}
}
Below policy attached with Lambda
functions
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:DescribeNetworkInterfaces"
],
"Resource": [
"*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"rds:DescribeDBInstances",
"rds:DescribeDBSnapshots"
],
"Resource": [
"*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"rds:CreateDBSnapshot",
"rds:CreateDBInstance",
"rds:CopyDBSnapshot",
"rds:DeleteDBSnapshot"
],
"Resource": [
"*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"rds:CreateDBSnapshot"
],
"Resource": [
"*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"rds:CopyDBSnapshot",
"rds:DeleteDBSnapshot"
],
"Resource": [
"arn:aws:rds:us-east-1:*:snapshot:*"
]
},
{
"Sid": "abcd",
"Effect": "Allow",
"Action": [
"events:DescribeRule"
],
"Resource": [
"arn:aws:events:*:*:rule/*"
]
}
]
}
It's A Nice Post.
ReplyDeleteDigital Marketing Courses in Bangalore
It's Really A Great Post. Looking For Some More Stuff.
ReplyDeleteshriram break free