in lib/workshop.py [0:0]
def create_simple_mysql_rds(region, session, db_name, subnet_ids, rds_secret_name):
ENGINE_NAME = 'mysql'
DB_INSTANCE_TYPE = 'db.m5.large'
DB_NAME = db_name
DB_USER_NAME = 'db_user'
DB_USER_PASSWORD = 'db_pass_'+str(uuid.uuid4())[0:10]
SUBNET_GROUP_NAME = db_name + '-subnetgroup'
rds_client = boto3.client('rds')
ec2_client = boto3.client('ec2')
# create a subnet group first
try:
subnet_group_response = rds_client.create_db_subnet_group(DBSubnetGroupName=SUBNET_GROUP_NAME,
DBSubnetGroupDescription='subnet group for the simple rds',
SubnetIds=subnet_ids)
except ClientError as e:
if e.response['Error']['Code'] == "DBSubnetGroupAlreadyExists":
print("SubnetGroup Already exist, ignore")
else:
print(e)
try:
create_db_instance_response = rds_client.create_db_instance(DBInstanceIdentifier=DB_NAME,
DBInstanceClass=DB_INSTANCE_TYPE,
DBName=DB_NAME,
Engine=ENGINE_NAME,
AllocatedStorage=10,
MasterUsername=DB_USER_NAME,
MasterUserPassword=DB_USER_PASSWORD,
DBSubnetGroupName=SUBNET_GROUP_NAME,
PubliclyAccessible=True)
except ClientError as e:
if e.response['Error']['Code'] == "DBInstanceAlreadyExists":
print("DB instance with the same id already exists. You can either use the same instance or pick a new id")
except:
print("Failed to create the db")
raise
else:
print("Successfully created DB instance %s" % DB_NAME)
create_rds_secret(region, session, rds_secret_name, DB_NAME,'', '3306', DB_USER_NAME, DB_USER_PASSWORD)