def cluster(self): variables = self.get_variables() if variables["ClusterParameters"]: parameter_group = Ref(PARAMETER_GROUP) else: parameter_group = "default.%s" % variables["DBFamily"] engine_version = variables["EngineVersion"] or NoValue database_name = variables["DatabaseName"] or NoValue replication_source_arn = variables["ReplicationSourceArn"] or NoValue return DBCluster( DBCLUSTER, DeletionPolicy="Snapshot", BackupRetentionPeriod=variables["BackupRetentionPeriod"], DBClusterParameterGroupName=parameter_group, DBSubnetGroupName=Ref(SUBNET_GROUP), EnableIAMDatabaseAuthentication=variables[ "EnableIAMDatabaseAuthentication" ], Engine=self.engine() or variables["Engine"], EngineVersion=engine_version, MasterUsername=self.get_master_user(), MasterUserPassword=self.get_master_user_password(), DatabaseName=database_name, Port=variables["Port"] or self.port(), PreferredBackupWindow=variables["PreferredBackupWindow"], PreferredMaintenanceWindow=variables["PreferredMaintenanceWindow"], SnapshotIdentifier=self.get_snapshot_identifier(), Tags=self.get_tags(), VpcSecurityGroupIds=[self.security_group], StorageEncrypted=self.get_storage_encrypted(), ReplicationSourceIdentifier=replication_source_arn, )
def add_cluster(template): """ Function to add the cluster to the template :param troposphere.Template template: template to add the DB Cluster to. :return: cluster :rtype: troposphere.rds.DBCluster """ cluster = DBCluster( CLUSTER_T, template=template, Condition=rds_conditions.USE_CLUSTER_CON_T, DBSubnetGroupName=If( rds_conditions.DBS_SUBNET_GROUP_CON_T, Ref(CLUSTER_SUBNET_GROUP), Ref(DBS_SUBNET_GROUP), ), DatabaseName=Ref(DB_NAME), MasterUsername=If( rds_conditions.USE_CLUSTER_AND_SNAPSHOT_CON_T, Ref(AWS_NO_VALUE), Sub(f"{{{{resolve:secretsmanager:${{{DB_SECRET_T}}}:SecretString:username}}}}" ), ), MasterUserPassword=Sub( f"{{{{resolve:secretsmanager:${{{DB_SECRET_T}}}:SecretString:password}}}}" ), SnapshotIdentifier=If( rds_conditions.USE_CLUSTER_CON_T, If( rds_conditions.USE_DB_SNAPSHOT_CON_T, Ref(DB_SNAPSHOT_ID), Ref(AWS_NO_VALUE), ), Ref(AWS_NO_VALUE), ), Engine=Ref(DB_ENGINE_NAME), EngineVersion=Ref(DB_ENGINE_VERSION), DBClusterParameterGroupName=Ref(CLUSTER_PARAMETER_GROUP_T), VpcSecurityGroupIds=[Ref(DB_SG_T)], ) return cluster
def add_default_cluster_definition(db): """ Function to add the cluster to the template :param ecs_composex.rds.rds_stack.Rds db: The Rds resource :return: cluster :rtype: troposphere.rds.DBCluster """ props = { "Condition": rds_conditions.USE_CLUSTER_CON_T, "DBSubnetGroupName": Ref(db.db_subnet_group), "DatabaseName": Ref(DB_NAME), "MasterUsername": If( rds_conditions.USE_CLUSTER_AND_SNAPSHOT_CON_T, Ref(AWS_NO_VALUE), Sub( f"{{{{resolve:secretsmanager:${{{db.db_secret.title}}}:SecretString:username}}}}" ), ), "MasterUserPassword": Sub( f"{{{{resolve:secretsmanager:${{{db.db_secret.title}}}:SecretString:password}}}}" ), "SnapshotIdentifier": If( rds_conditions.USE_CLUSTER_CON_T, If( rds_conditions.USE_DB_SNAPSHOT_CON_T, Ref(DB_SNAPSHOT_ID), Ref(AWS_NO_VALUE), ), Ref(AWS_NO_VALUE), ), "Engine": Ref(DB_ENGINE_NAME), "EngineVersion": Ref(DB_ENGINE_VERSION), "DBClusterParameterGroupName": Ref(CLUSTER_PARAMETER_GROUP_T), "VpcSecurityGroupIds": [Ref(db.db_sg)], "Tags": Tags(SecretName=Ref(db.db_secret), Name=db.logical_name), "StorageEncrypted": True, } cluster = DBCluster(f"Cluster{db.logical_name}", **props) return cluster
def create_cluster(self): t = self.template variables = self.get_variables() parameter_group = Ref("AWS::NoValue") if variables["ClusterParameters"]: parameter_group = Ref(PARAMETER_GROUP) t.add_resource( DBCluster(DBCLUSTER, BackupRetentionPeriod=variables["BackupRetentionPeriod"], DBClusterParameterGroupName=parameter_group, DBSubnetGroupName=Ref(SUBNET_GROUP), Engine=self.engine() or variables["Engine"], EngineVersion=variables["EngineVersion"], MasterUsername=variables["MasterUser"], MasterUserPassword=Ref("MasterUserPassword"), PreferredBackupWindow=variables["PreferredBackupWindow"], PreferredMaintenanceWindow=variables[ "PreferredMaintenanceWindow"], Tags=self.get_tags(), VpcSecurityGroupIds=[ self.security_group, ]))
rdssecuritygroup = t.add_resource( SecurityGroup( "RDSSecurityGroup", GroupDescription="Security Group for the Aurora Serverless Cluster", VpcId=Ref(vpcid))) AuroraSLCluster = t.add_resource( DBCluster("AuroraSLCluster", Engine="aurora", MasterUsername=Ref(dbuser), MasterUserPassword=Join("", [ "{{resolve:secretsmanager:", { "Ref": "DbSecret" }, ":SecretString:password}}" ]), DBClusterParameterGroupName=Ref(slclusterparametergroup), DBClusterIdentifier="aurora-sl-cluster", EngineMode="serverless", ScalingConfiguration=ScalingConfiguration( AutoPause=False, MaxCapacity=Ref(clustermaxcapacity), MinCapacity=Ref(clustermincapacity)), DBSubnetGroupName=Ref(rdssubnetgroup), VpcSecurityGroupIds=[Ref(rdssecuritygroup)], StorageEncrypted="true")) t.add_resource( SecretTargetAttachment( "SecretRDSInstanceAttachment", TargetType="AWS::RDS::DBCluster", SecretId=Ref(DbSecret), TargetId=Ref(AuroraSLCluster),
def create_aurora_template(): template = Template() # vpc = template.add_parameter( # parameter=Parameter( # title='Vpc', # Type='String' # ) # ) # # subnet_a = template.add_parameter( # parameter=Parameter( # title='SubnetA', # Type='String' # ) # ) # # subnet_b = template.add_parameter( # parameter=Parameter( # title='SubnetB', # Type='String' # ) # ) master_user_name = template.add_parameter( parameter=Parameter(title='DBMasterUserName', Type='String')) master_user_password = template.add_parameter( parameter=Parameter(title='DBMasterUserPassword', Type='String')) instance_class = template.add_parameter(parameter=Parameter( title='InstanceClass', Default='db.t2.small', Type='String')) engine_version = template.add_parameter(parameter=Parameter( title='EngineVersion', Default='5.7.12', Type='String')) security_group = template.add_resource(resource=SecurityGroup( title='SampleSecurityGroup', GroupDescription='sample-rds', SecurityGroupIngress=[{ 'IpProtocol': 'tcp', 'FromPort': 3306, 'ToPort': 3306, 'CidrIp': '0.0.0.0/0', }], # VpcId=Ref(vpc) VpcId=ImportValue(CommonResource.ExportName.VPC_ID.value))) db_subnet_group = template.add_resource(resource=DBSubnetGroup( title='SampleDBSubnetGroup', DBSubnetGroupDescription='sample-aurora', DBSubnetGroupName='sample-aurora', # SubnetIds=[Ref(subnet_a), Ref(subnet_b)] SubnetIds=[ ImportValue(CommonResource.ExportName.PRIVATE_SUBNET_A_ID.value), ImportValue(CommonResource.ExportName.PRIVATE_SUBNET_B_ID.value), ])) db_cluster_parameter_group = template.add_resource( DBClusterParameterGroup(title='SampleDBClusterParameterGroup', Description='sample-aurora', Family='aurora-mysql5.7', Parameters={'time_zone': 'Asia/Tokyo'})) cluster = template.add_resource(resource=DBCluster( title='SampleDBCluster', DatabaseName='sample_aurora', DBClusterIdentifier='sample-aurora', DBClusterParameterGroupName=Ref(db_cluster_parameter_group), DBSubnetGroupName=Ref(db_subnet_group), Engine='aurora-mysql', EngineVersion=Ref(engine_version), MasterUsername=Ref(master_user_name), MasterUserPassword=Ref(master_user_password), VpcSecurityGroupIds=[GetAtt(security_group, 'GroupId')], )) for suffix in ['A', 'B']: template.add_resource(resource=DBInstance( title='SampleDBInstance' + suffix, DBClusterIdentifier=Ref(cluster), DBInstanceClass=Ref(instance_class), Engine='aurora-mysql', )) output_template_file(template, 'aurora.yml')