SHARE
TWEET

Untitled

a guest Jan 21st, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from botocore.vendored import requests
  2. import json
  3. import pymysql
  4.  
  5. SUCCESS = "SUCCESS"
  6. FAILED = "FAILED"
  7.  
  8.  
  9. def create_db(dbname, dbuser, dbpassword, rdsendpoint, rdsuser, rdspassword):
  10.     conn = pymysql.connect(host=rdsendpoint,
  11.                            user=rdsuser,
  12.                            password=rdspassword)
  13.     create_db_query = f"CREATE DATABASE {dbname};"
  14.     create_user_query = f"CREATE USER {dbuser}@'%' IDENTIFIED BY '{dbpassword}';"
  15.     grant_query = f"GRANT ALL PRIVILEGES ON {dbname}.* TO {dbuser}@'%'; FLUSH PRIVILEGES;"
  16.     try:
  17.         cursor = conn.cursor()
  18.         cursor.execute(create_db_query)
  19.         cursor.execute(create_user_query)
  20.         cursor.execute(grant_query)
  21.         cursor.close()
  22.         conn.commit()
  23.         conn.close()
  24.     except Exception as err:
  25.         return err
  26.     return None
  27.  
  28.  
  29. def handler(event, context):
  30.     db_name = ""
  31.     db_user = ""
  32.     db_password = ""
  33.     rds_endpoint = ""
  34.     rds_user = ""
  35.     rds_password = ""
  36.     physicalResourceId = ""
  37.     input_props = event['ResourceProperties']
  38.     required_props = ["DBName", "RDSEndpoint", "RDSUser", "RDSPassword"]
  39.     missing_props = []
  40.     for prop in required_props:
  41.         if not prop in input_props:
  42.             missing_props.append(prop)
  43.     if missing_props:
  44.         print(f"Required properties are missing: {missing_props}")
  45.         send(event, context, FAILED, physicalResourceId="", responseData="")
  46.     db_name = input_props['DBName']
  47.     rds_endpoint = input_props['RDSEndpoint']
  48.     rds_user = input_props['RDSUser']
  49.     rds_password = input_props['RDSPassword']
  50.     if not "DBUser" in input_props or len(input_props['DBUser']) == 0:
  51.         db_user = db_name
  52.     db_user = input_props['DBUser']
  53.     if not "DBPassword" in input_props or len(input_props['DBPassword']) == 0:
  54.         db_password = db_name
  55.     db_name = input_props['DBPassword']
  56.     err = create_db(db_name, db_user, db_password, rds_endpoint, rds_user, rds_password)
  57.     if err:
  58.         send(event, context, FAILED, physicalResourceId="", responseData="")
  59.  
  60.     send(event, context, SUCCESS, physicalResourceId=physicalResourceId, responseData="")
  61.  
  62.  
  63. def send(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False):
  64.     responseUrl = event['ResponseURL']
  65.  
  66.     print(responseUrl)
  67.  
  68.     responseBody = {}
  69.     responseBody['Status'] = responseStatus
  70.     responseBody['Reason'] = 'See the details in CloudWatch Log Stream: ' + context.log_stream_name
  71.     responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name
  72.     responseBody['StackId'] = event['StackId']
  73.     responseBody['RequestId'] = event['RequestId']
  74.     responseBody['LogicalResourceId'] = event['LogicalResourceId']
  75.     responseBody['NoEcho'] = noEcho
  76.     responseBody['Data'] = responseData
  77.  
  78.     json_responseBody = json.dumps(responseBody)
  79.  
  80.     print("Response body:\n" + json_responseBody)
  81.  
  82.     headers = {
  83.         'content-type': '',
  84.         'content-length': str(len(json_responseBody))
  85.     }
  86.  
  87.     try:
  88.         response = requests.put(responseUrl,
  89.                                 data=json_responseBody,
  90.                                 headers=headers)
  91.         print("Status code: " + response.reason)
  92.     except Exception as e:
  93.         print("send(..) failed executing requests.put(..): " + str(e))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top