Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. def create_aurora(
  2. instance_identifier, # used for instance name and cluster name
  3. db_username,
  4. db_password,
  5. db_name,
  6. db_port,
  7. vpc_id,
  8. vpc_sg, # Must be an array
  9. dbsubnetgroup_name,
  10. public_access = False,
  11. AZ = None,
  12. instance_type = "db.t2.small",
  13. multi_az = True,
  14. nb_instance = 1,
  15. extratags = []
  16. ):
  17. rds = boto3.client('rds')
  18. # Assume a DB SUBNET Groups exists before creating the cluster. You must have created a DBSUbnetGroup associated to the Subnet of the VPC of your cluster. AWS will find it automatically.
  19.  
  20. #
  21. # Search if the cluster exists
  22. try:
  23. db_cluster = rds.describe_db_clusters(
  24. DBClusterIdentifier = instance_identifier
  25. )['DBClusters']
  26. db_cluster = db_cluster[0]
  27. except botocore.exceptions.ClientError as e:
  28. psa.printf("Creating empty clusterrn");
  29. res = rds.create_db_cluster(
  30. DBClusterIdentifier = instance_identifier,
  31. Engine="aurora",
  32. MasterUsername=db_username,
  33. MasterUserPassword=db_password,
  34. DBSubnetGroupName=dbsubnetgroup_name,
  35. VpcSecurityGroupIds=vpc_sg,
  36. AvailabilityZones=AZ
  37. )
  38. db_cluster = res['DBCluster']
  39.  
  40.  
  41. cluster_name = db_cluster['DBClusterIdentifier']
  42. instance_identifier = db_cluster['DBClusterIdentifier']
  43. psa.printf("Cluster identifier : %s, status : %s, members : %dn", instance_identifier , db_cluster['Status'], len(db_cluster['DBClusterMembers']))
  44. if (db_cluster['Status'] == 'deleting'):
  45. psa.printf(" Please wait for the cluster to be deleted and try again.n")
  46. return None
  47. psa.printf(" Writer Endpoint : %sn", db_cluster['Endpoint'])
  48. psa.printf(" Reader Endpoint : %sn", db_cluster['ReaderEndpoint'])
  49. # Now create instances
  50. # Loop on requested number of instance, and balance them on AZ
  51. for i in range(1, nb_instance+1):
  52. if AZ != None:
  53. the_AZ = AZ[i -1 % len(AZ)]
  54. dbinstance_id = instance_identifier+"-"+str(i)+"-"+the_AZ
  55. else:
  56. the_AZ = None
  57. dbinstance_id = instance_identifier+"-"+str(i)
  58. psa.printf("Creating instance %d named '%s' in AZ %sn", i, dbinstance_id, the_AZ)
  59.  
  60. try:
  61. res = rds.create_db_instance(
  62. DBInstanceIdentifier=dbinstance_id,
  63. DBInstanceClass=instance_type,
  64. Engine='aurora',
  65. PubliclyAccessible=False,
  66. AvailabilityZone=the_AZ,
  67. DBSubnetGroupName=dbsubnetgroup_name,
  68. DBClusterIdentifier=instance_identifier,
  69. Tags = psa.tagsKeyValueToAWStags(extratags)
  70. )['DBInstance']
  71. psa.printf(" DbiResourceId=%sn", res['DbiResourceId'])
  72.  
  73. except botocore.exceptions.ClientError as e:
  74. psa.printf(" Instance seems to exists.n")
  75. res = rds.describe_db_instances(DBInstanceIdentifier = dbinstance_id)['DBInstances']
  76. psa.printf(" Status is %sn", res[0]['DBInstanceStatus'])
  77. return db_cluster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement