def configure_sns_to_lambda(topic_arn, function_arn): client = boto3.client('sns') custom_log("Subscribing lambda to SNS", "DEBUG") response = client.subscribe(TopicArn=topic_arn, Protocol='lambda', Endpoint=function_arn, ReturnSubscriptionArn=True) custom_log(response, "DEBUG")
def configure_s3_to_sns(original_bucket_name, topic_arn): s3 = boto3.resource('s3') bucket_notification = s3.BucketNotification(original_bucket_name) custom_log("Cofiguring bucket notification on put method", "DEBUG") response = bucket_notification.put( NotificationConfiguration={ 'TopicConfigurations': [{ 'TopicArn': topic_arn, 'Events': ['s3:ObjectCreated:Put'] }] }) custom_log(response, "DEBUG")
def create_s3_backup_file(): random_string = misc.create_random_string(4) s3 = boto3.client('s3') response = s3.create_bucket(Bucket='backupfiles' + str(random_string)) misc.custom_log("Checking for Return code", "DEBUG") if not (response['ResponseMetadata']['HTTPStatusCode'] >= 200 and response['ResponseMetadata']['HTTPStatusCode'] <= 299): print(response) raise misc.Custom_Exception("S3 bucket creation failed") location = response['Location'] bucket_name = location[1:] misc.custom_log(response, "DEBUG") return bucket_name
def create_lambda(destination_name, role_arn): lambda_client = boto3.client('lambda') misc.custom_log("Opening zip file for lambda code", "DEBUG") with open(zip_file_path, 'rb') as f: zipped_code = f.read() random_string = misc.create_random_string(4) function_name = 'Automatic_backup_creator'+str(random_string) response = lambda_client.create_function( FunctionName= function_name, Environment={ 'Variables': { 'dest_bucket_name': destination_name } }, Runtime='python3.6', Role=role_arn, Handler='lambda_handler.lambda_handler', Code={ 'ZipFile':zipped_code }, Description='Creates backup of your S3 bucket which you have given whenever a new file has been uploaded to it' ) misc.custom_log("Checking for Return code", "DEBUG") if not(response['ResponseMetadata']['HTTPStatusCode'] >= 200 and response['ResponseMetadata']['HTTPStatusCode'] <= 299): raise misc.Custom_Exception("Lambda creation failed") response_add_permissions = lambda_client.add_permission( FunctionName=function_name, StatementId='sns_invoke_1567890', Action='lambda:InvokeFunction', Principal="sns.amazonaws.com" ) misc.custom_log(response, "DEBUG") return response['FunctionArn']
def create_sns(): sns = boto3.client('sns') random_string = misc.create_random_string(4) name = 'NewSNSTopic' + str(random_string) response = sns.create_topic( Name= name, Attributes={ 'DisplayName':'New file is uploaded' }, Tags=[ { 'Key': 'SNS to trigger Lambda', 'Value': 'whenever new file is uploaded_take backup' } ] ) misc.custom_log("Taking topic arn from SNS topic ", "DEBUG") output_get = sns.get_topic_attributes( TopicArn=response['TopicArn'] ) attribute_policy_dict =json.loads(output_get['Attributes']['Policy']) # print(attribute_policy_dict) attribute_policy_dict['Statement'][0].pop('Condition', None) attribute_policy_string = json.dumps(attribute_policy_dict) # print(attribute_policy_string) misc.custom_log("Setting attribute's access policy of topic arn ", "DEBUG") output_set = sns.set_topic_attributes( TopicArn=response['TopicArn'], AttributeName='Policy', AttributeValue=attribute_policy_string ) if not(response['ResponseMetadata']['HTTPStatusCode'] >= 200 and response['ResponseMetadata']['HTTPStatusCode'] <= 299): raise misc.Custom_Exception("SNS topic creation failed") misc.custom_log(response, "DEBUG") return response['TopicArn']
import boto3 from Lib.misc import custom_log custom_log("Configuring S3 to SNS topic", "DEBUG") def configure_s3_to_sns(original_bucket_name, topic_arn): s3 = boto3.resource('s3') bucket_notification = s3.BucketNotification(original_bucket_name) custom_log("Cofiguring bucket notification on put method", "DEBUG") response = bucket_notification.put( NotificationConfiguration={ 'TopicConfigurations': [{ 'TopicArn': topic_arn, 'Events': ['s3:ObjectCreated:Put'] }] }) custom_log(response, "DEBUG") # print(response)
import boto3 from Lib.misc import custom_log custom_log("Configure SNS to Lambda", "DEBUG") def configure_sns_to_lambda(topic_arn, function_arn): client = boto3.client('sns') custom_log("Subscribing lambda to SNS", "DEBUG") response = client.subscribe(TopicArn=topic_arn, Protocol='lambda', Endpoint=function_arn, ReturnSubscriptionArn=True) custom_log(response, "DEBUG") # print(response)
from Infrastructure import * from Lib.misc import custom_log # Required Original bucket name from where to copy the new uploaded file custom_log("Reqesting for bucket name") original_bucket_name = input("Enter bucket name") custom_log("Received bucket name " + str(original_bucket_name)) custom_log("Now creating custom role") role_arn = create_role.create_role() custom_log("Custom role has been created") custom_log("Now creating S3 backup bucket ") bucket_name = creation_of_S3_bucket.create_s3_backup_file() custom_log("Backup bucket has been successfully created") custom_log("Creating Lambda function and attaching role to it") function_arn = creation_of_lambda.create_lambda(bucket_name, role_arn) custom_log("Lambda function successfully created") custom_log("Creating SNS Topic") topic_arn = creation_of_SNS_Topic.create_sns() custom_log("SNS topic has been created") custom_log("Configuring Original S3 bucket with SNS topic") configure_origS3_to_SNS_topic.configure_s3_to_sns(original_bucket_name, topic_arn) custom_log("Configuration of S3 to SNS successfull") custom_log("Configuring SNS to Lambda function") configure_sns_to_lambda.configure_sns_to_lambda(topic_arn, function_arn)
import boto3 import random from Lib import misc misc.custom_log("Creating S3 bucket for backup", "DEBUG") def create_s3_backup_file(): random_string = misc.create_random_string(4) s3 = boto3.client('s3') response = s3.create_bucket(Bucket='backupfiles' + str(random_string)) misc.custom_log("Checking for Return code", "DEBUG") if not (response['ResponseMetadata']['HTTPStatusCode'] >= 200 and response['ResponseMetadata']['HTTPStatusCode'] <= 299): print(response) raise misc.Custom_Exception("S3 bucket creation failed") location = response['Location'] bucket_name = location[1:] misc.custom_log(response, "DEBUG") return bucket_name
import boto3 import os from Lib import misc misc.custom_log("Making Zip file for lambda code", "DEBUG") zip_file_path = os.path.join("Lambda_Code", "lambda_code.zip") if not (os.path.exists(zip_file_path)): print("Lambda file location => {}".format(zip_file_path)) raise misc.Custom_Exception("Lambda Code file does not exist !!") # print(zip_file_path) # with open(zip_file_path, 'rb') as fp: # print (fp) misc.custom_log("Zip file successfully created", "DEBUG") misc.custom_log("Creating Lambda", "DEBUG") def create_lambda(destination_name, role_arn): lambda_client = boto3.client('lambda') misc.custom_log("Opening zip file for lambda code", "DEBUG") with open(zip_file_path, 'rb') as f: zipped_code = f.read() random_string = misc.create_random_string(4) function_name = 'Automatic_backup_creator'+str(random_string) response = lambda_client.create_function( FunctionName= function_name, Environment={ 'Variables': { 'dest_bucket_name': destination_name } }, Runtime='python3.6', Role=role_arn, Handler='lambda_handler.lambda_handler',
import boto3 import json from Lib import misc misc.custom_log("Creating SNS topic", "DEBUG") def create_sns(): sns = boto3.client('sns') random_string = misc.create_random_string(4) name = 'NewSNSTopic' + str(random_string) response = sns.create_topic( Name= name, Attributes={ 'DisplayName':'New file is uploaded' }, Tags=[ { 'Key': 'SNS to trigger Lambda', 'Value': 'whenever new file is uploaded_take backup' } ] ) misc.custom_log("Taking topic arn from SNS topic ", "DEBUG") output_get = sns.get_topic_attributes( TopicArn=response['TopicArn'] ) attribute_policy_dict =json.loads(output_get['Attributes']['Policy']) # print(attribute_policy_dict) attribute_policy_dict['Statement'][0].pop('Condition', None) attribute_policy_string = json.dumps(attribute_policy_dict) # print(attribute_policy_string)
def create_role(): misc.custom_log("Assume role policy document", "DEBUG") assume_role_string = json.dumps({ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }) client = boto3.client('iam') misc.custom_log("Generating random string for role name", "DEBUG") random_string = misc.create_random_string(4) misc.custom_log("Initialising role name", "DEBUG") role_name = 'Lambda_execution_and_S3_full_permissions1'+ str(random_string) misc.custom_log("Now trying to create role", "DEBUG") response1 = client.create_role( RoleName= role_name, AssumeRolePolicyDocument=assume_role_string ) misc.custom_log("S3 full access Policy arn", "DEBUG") policy_s3 = 'arn:aws:iam::aws:policy/AmazonS3FullAccess' misc.custom_log("Lambda basic execution role policy arn", "DEBUG") policy_lambda_execution = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' misc.custom_log("Attaching S3 full access policy to role", "DEBUG") response2 = client.attach_role_policy( RoleName=role_name, PolicyArn= policy_s3) misc.custom_log("Attaching Lambda basic execution role policy to Role ", "DEBUG") response3 = client.attach_role_policy( RoleName = role_name, PolicyArn =policy_lambda_execution ) misc.custom_log("Now sleeping for 10 seconds to ensure IAM role is created successfully", "DEBUG") time.sleep(10) misc.custom_log("Returning Newly created Role's arn", "DEBUG") misc.custom_log( response1, "DEBUG") return response1['Role']['Arn']
import boto3 import json from Lib import misc import time misc.custom_log("Creatin custom role for lambda", "DEBUG") def create_role(): misc.custom_log("Assume role policy document", "DEBUG") assume_role_string = json.dumps({ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }) client = boto3.client('iam') misc.custom_log("Generating random string for role name", "DEBUG") random_string = misc.create_random_string(4) misc.custom_log("Initialising role name", "DEBUG") role_name = 'Lambda_execution_and_S3_full_permissions1'+ str(random_string) misc.custom_log("Now trying to create role", "DEBUG") response1 = client.create_role( RoleName= role_name, AssumeRolePolicyDocument=assume_role_string )