-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_db_instance_provisioning.py
More file actions
84 lines (74 loc) · 3.27 KB
/
00_db_instance_provisioning.py
File metadata and controls
84 lines (74 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Personal License Agreement
# Copyright Notice
#
# © 2026 Voltaire Bazurto Blacio. All rights reserved.
# License Terms
#
# Ownership: All code contained in this portfolio is the sole property of Voltaire Bazurto Blacio and is hereby copyrighted by me.
#
# Permitted Use: Others are welcome to view and study the code for personal, educational, or non-commercial purposes. You may share insights or information about the code, but you cannot use it for any commercial products, either as-is or in a derivative form.
#
# Restrictions: The code may not be used, reproduced, or distributed for commercial purposes under any circumstances without my explicit written permission.
#
# Rights Reserved: I reserve the right to use the code, or any future versions thereof, for my own purposes in any way I choose, including but not limited to the development of future commercial derivative works under my name or personal brand.
#
# Disclaimer: The code is provided "as is" without warranty of any kind, either express or implied. I am not responsible for any damages resulting from the use of this code.
#
# By accessing this portfolio, you agree to abide by these terms.
import boto3
from botocore.exceptions import ClientError
import secrets
import string
AWS_PROFILE = "default"
AWS_REGION = 'us-east-1'
DB_MASTER_USERNAME = 'postgres'
DB_INSTANCE_ID = 'hr-recruit-app-demo-aws'
DB_NAME = 'hr_recruit_app'
def generate_password():
alphabet = string.ascii_letters + string.digits + '.$' #support alphanumeric characters and only a subset of allowed special chars
password = ''.join(secrets.choice(alphabet) for _ in range(20))
return password
def create_db_instance_if_not_exist(rds_client, master_pwd):
return rds_client.create_db_instance(
DBInstanceClass='db.t4g.micro',
DBInstanceIdentifier=DB_INSTANCE_ID,
Engine='postgres',
EngineVersion='17.6',
DBName=DB_NAME,
AllocatedStorage=20, #Min size for gp2 is 20G
MasterUsername=DB_MASTER_USERNAME,
MasterUserPassword=master_pwd,
)
# main script starts here
rds = boto3.client('rds', region_name=AWS_REGION)
DB_MASTER_USER_PASSWORD = generate_password()
try:
db_instance_result = create_db_instance_if_not_exist(rds, DB_MASTER_USER_PASSWORD)
print(f"New DB instance {DB_INSTANCE_ID} created with details: ")
print(f"Copy this value in a safe place: {DB_MASTER_USER_PASSWORD}\n")
except ClientError as e:
if 'instance already exists' in e.args[0] :
print(f'DB Instance already exists in RDS. Skipping DB creation')
else:
print(f'Error creating DB instance: {e}')
exit(1)
except Exception as e:
print(f'Error creating DB instance: {e}')
exit(1)
# List db instances
print('########### List of dbs in RDS ############')
dbs = rds.describe_db_instances()
for db in dbs['DBInstances']:
print(f'{db['DBInstanceIdentifier']} - {db['DBName']} - {db['Engine']}')
#Wait until instance is ready
waiter = rds.get_waiter('db_instance_available')
print(f"Waiting for RDS instance '{DB_INSTANCE_ID}' to be available...\n")
try:
waiter.wait(DBInstanceIdentifier=DB_INSTANCE_ID, WaiterConfig={
'Delay': 30,
'MaxAttempts': 60,
})
print(f"RDS instance '{DB_INSTANCE_ID}' is now [available]!\n")
except Exception as e:
print(f'Error waiting for RDS instance: {e}\n')
raise