AWS Lambda Function To start DMS Task


INTRODUCTION:


Lambda function:

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.

You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use

DMS :

DMS cloud service is used to migrate relational databases, data warehouses, NoSQL databases, and other sorts of data stores.

It can also capture ongoing changes from the source. DMS Change data capture (CDC) is the process that captures ongoing changes after 

the completion of initial migration to a supported target data store. This keeps source and target in sync.

Creating an AWS Lambda function to start DMS task:- 

Services --> Lambda --> Functions --> create function:  

Give a Function name, choose python 3.9 as runtime 




Select create a new role with basic lambda permissions and click on create function.





then click create function. the function will be created as shown in figure.



if you click myfunction-rg you will be directed to this page on successful creation.  



then scroll the page down we will find the functions editor.  


import boto3

import os

import time

#lambda_handler works like main function and is required for code execution

def lambda_handler(event, context):

    client = boto3.client('dms',region_name='us-west-2',    

    #acessing access key and secret key from local system

    aws_access_key_id=os.environ.get('AWS_DMS_KEY'),

    aws_secret_access_key=os.environ.get('AWS_DMS_PASSWD'))

    # connecting to dms task and starting it

    response = client.start_replication_task(ReplicationTaskArn='arn:aws:dms:us-west-2:092484317105:task:2YCBBDEXBTAJG2JUMFOHV45CQAVLOKW2SHLZD5Q', StartReplicationTaskType='reload-target')

    print(response)

 then save the code.

 and then go to configuration and give the permissions to the the role.



Note:


if multiple .py files are present , change the Handler to the main file_name.lambda_handler 

Now, scroll to top and click on configurations and then click on permissions and then click on role name and will be directed to the IAM role .
  • Now click on the attached policy name and edit the policy’s json code with the below code: 



  • Now click on the attached policy name and edit the policy’s json code with the below code: 
  • Add the below code as shown in the screenshot and click on review policy (here alaram lambda function to start the dms replication task) 

{

             "Action":[

                 "dms:StartReplicationTask"

                 ],

                 "Effect":"Allow",

                 "Resource":"*"

         }






  • Now, go back to your lambda function and click on Test to check the function. 

  • Give an event name and keep the remaining same and click on create on the bottom. 






  • On successful execution it returns null as response. 

  • Now, again click on Test and then open the DMS service and the task will get started (rds-s3-dms). 




after execution of the code the DMS Task will be started sucessfully as shown in above fig.

  • PURPOSE: To start the DMS Task instead of running manually by using AWS Lambda function.
  • Intend Audience : Useful for the people working on AWS DMS service and Lambda function        











 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 









Comments