Created
April 2, 2020 05:04
-
-
Save mulka/d10bce7b9d1cc3994239660706eb2cab to your computer and use it in GitHub Desktop.
Uses the RDS Data API to create a database inside an Amazon Aurora Serverless cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import boto3 | |
client = boto3.client('rds-data') | |
cluster_name = sys.argv[1] | |
db_name = sys.argv[2] | |
sql = 'CREATE DATABASE ' + db_name | |
sts_client = boto3.client('sts') | |
account_id = sts_client.get_caller_identity().get('Account') | |
region = sts_client.meta.region_name | |
clusters = boto3.client('rds').describe_db_clusters() | |
for cluster in clusters['DBClusters']: | |
if cluster['DBClusterIdentifier'] == cluster_name: | |
cluster_resource_id = cluster['DbClusterResourceId'] | |
cluster_arn = cluster['DBClusterArn'] | |
break | |
secrets = boto3.client('secretsmanager').list_secrets() | |
for secret in secrets['SecretList']: | |
if cluster_resource_id in secret['Name']: | |
secret_arn = secret['ARN'] | |
break | |
result = client.execute_statement( | |
resourceArn=cluster_arn, | |
secretArn=secret_arn, | |
sql=sql, | |
includeResultMetadata=True, | |
) | |
if result['ResponseMetadata']['HTTPStatusCode'] == 200: | |
print('SUCCESS!') | |
else: | |
print('ERROR') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment