AWS:如何在Python / Boto3中创建RDS Aurora集群

时间:2017-05-08 18:32:06

标签: amazon-web-services amazon-rds boto3 amazon-rds-aurora

我的应用程序托管在Amazon Web Services上,我开始编写应用程序的所有基础结构(VPC,安全组,Beanstalk等)的脚本。我没有找到创建RDS Aurora集群的正确方法,但我无法使用Boto3在Python中重现RDS向导(帮助您创建数据库实例和集群)。也许我在基础设施和网络方面缺乏知识,但我认为创建一个Aurora集群必须是我可以访问的。

所以这是我的问题: 让我说我有一个VPC ID,一个安全组ID和一些数据库信息(用户,密码......),我必须做什么来创建一个集群,并让它可以被我的应用程序使用?该过程必须以集群读取器/写入器端点和仅读取器端点结束。

提前致谢。我将在此分享我对这一过程的所有发现。

2 个答案:

答案 0 :(得分:1)

  

是的,你走在正确的轨道上。以下是用于创建Aurora RDS群集的boto3 document

此外,为了解决更大的问题(即将整个基础架构作为代码进行管理),您应该查看Terraform等选项。

查看他们的Git Repo Terraform Git Repo因此,您可以使用此template

完成使用terraform创建Aurora群集的相同任务

答案 1 :(得分:0)

以下是我在Python / BOTO3中创建Aurora MySQL实例的方法。你必须自己实现一些缺失的功能。

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 cluster\r\n");
        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 : %d\n", 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 : %s\n", db_cluster['Endpoint'])
    psa.printf("   Reader Endpoint : %s\n", 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 %s\n", 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=%s\n", 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 %s\n", res[0]['DBInstanceStatus'])
    return db_cluster