Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_aurora(
- instance_identifier, # used for instance name and cluster name
- db_username,
- db_password,
- db_name,
- db_port,
- vpc_id,
- vpc_sg, # Must be an array
- dbsubnetgroup_name,
- public_access = False,
- AZ = None,
- instance_type = "db.t2.small",
- multi_az = True,
- nb_instance = 1,
- extratags = []
- ):
- rds = boto3.client('rds')
- # 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.
- #
- # Search if the cluster exists
- try:
- db_cluster = rds.describe_db_clusters(
- DBClusterIdentifier = instance_identifier
- )['DBClusters']
- db_cluster = db_cluster[0]
- except botocore.exceptions.ClientError as e:
- psa.printf("Creating empty clusterrn");
- res = rds.create_db_cluster(
- DBClusterIdentifier = instance_identifier,
- Engine="aurora",
- MasterUsername=db_username,
- MasterUserPassword=db_password,
- DBSubnetGroupName=dbsubnetgroup_name,
- VpcSecurityGroupIds=vpc_sg,
- AvailabilityZones=AZ
- )
- db_cluster = res['DBCluster']
- cluster_name = db_cluster['DBClusterIdentifier']
- instance_identifier = db_cluster['DBClusterIdentifier']
- psa.printf("Cluster identifier : %s, status : %s, members : %dn", instance_identifier , db_cluster['Status'], len(db_cluster['DBClusterMembers']))
- if (db_cluster['Status'] == 'deleting'):
- psa.printf(" Please wait for the cluster to be deleted and try again.n")
- return None
- psa.printf(" Writer Endpoint : %sn", db_cluster['Endpoint'])
- psa.printf(" Reader Endpoint : %sn", db_cluster['ReaderEndpoint'])
- # Now create instances
- # Loop on requested number of instance, and balance them on AZ
- for i in range(1, nb_instance+1):
- if AZ != None:
- the_AZ = AZ[i -1 % len(AZ)]
- dbinstance_id = instance_identifier+"-"+str(i)+"-"+the_AZ
- else:
- the_AZ = None
- dbinstance_id = instance_identifier+"-"+str(i)
- psa.printf("Creating instance %d named '%s' in AZ %sn", i, dbinstance_id, the_AZ)
- try:
- res = rds.create_db_instance(
- DBInstanceIdentifier=dbinstance_id,
- DBInstanceClass=instance_type,
- Engine='aurora',
- PubliclyAccessible=False,
- AvailabilityZone=the_AZ,
- DBSubnetGroupName=dbsubnetgroup_name,
- DBClusterIdentifier=instance_identifier,
- Tags = psa.tagsKeyValueToAWStags(extratags)
- )['DBInstance']
- psa.printf(" DbiResourceId=%sn", res['DbiResourceId'])
- except botocore.exceptions.ClientError as e:
- psa.printf(" Instance seems to exists.n")
- res = rds.describe_db_instances(DBInstanceIdentifier = dbinstance_id)['DBInstances']
- psa.printf(" Status is %sn", res[0]['DBInstanceStatus'])
- return db_cluster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement