Last active
October 14, 2015 20:06
-
-
Save instaBOT/a361c3092af196c1670f to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
cd | |
set -e | |
#################### | |
# parameters | |
#################### | |
# dev-env-db-subnet-group: RDS subnet group of subnets | |
# subnet-35760642: subnet for VPC 'vpc-89b6fbec' in 'us-west-2a' | |
# subnet-ef7d2f8a: subnet for VPC 'vpc-89b6fbec' in 'us-west-2b' | |
# subnet-a9901af0: subnet for VPC 'vpc-89b6fbec' in 'us-west-2c' | |
db_subnet_group_name="dev-env-db-subnet-group" | |
# AWS account number not returned by `aws rds describe-db-instances` | |
aws_account_number="837173812892" | |
# region not returned by `aws rds describe-db-instances` | |
db_instance_region="us-west-2" | |
# db.t2.micro: the cheapest type | |
db_instance_class="db.t2.micro" | |
# rds_snapshot | |
if [ "$1" == "" ]; then | |
echo "snapshot ID (e.g. 'rds:instagis2015-2015-09-23-07-23)' is required as first argument" | |
exit 1 | |
fi | |
rds_snapshot=$1 | |
# http://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/ | |
rand_hex_value=$(< /dev/urandom tr -dc a-f0-9 | head -c5) | |
rds_instance_id="ig-dev-$rand_hex_value" | |
master_username="pgadmin" | |
# note: password in lowercase | |
# http://stackoverflow.com/questions/32762426/what-are-the-limitations-of-postgres-master-password-in-rds | |
master_user_password=$(< /dev/urandom tr -dc a-z0-9 | head -c32) | |
#################### | |
# create instance | |
#################### | |
echo "creating instance from snapshot $rds_snapshot ..." | |
db_instance_id=$(\ | |
aws rds restore-db-instance-from-db-snapshot \ | |
--db-instance-identifier $rds_instance_id --db-snapshot-identifier $rds_snapshot \ | |
--db-instance-class $db_instance_class --no-multi-az \ | |
--db-subnet-group-name $db_subnet_group_name \ | |
--query 'DBInstance.DBInstanceIdentifier' \ | |
--output text) | |
echo "db instance id: $db_instance_id" | |
# e.g. ig-dev-d30b4 | |
echo "waiting for the instance to be available (at least a few minutes) ..." | |
aws rds wait db-instance-available --db-instance-identifier $db_instance_id | |
db_instance_arn="arn:aws:rds:$db_instance_region:$aws_account_number:db:$db_instance_id" | |
# e.g. arn:aws:rds:us-west-2:837173812892:db:ig-dev-7c8d8 | |
db_instance_address=$( | |
aws rds describe-db-instances --db-instance-identifier $db_instance_id \ | |
--query 'DBInstances[*].Endpoint.Address' \ | |
--output text) | |
echo "instance arn: $db_instance_arn" | |
echo "instance endpoint address: $db_instance_address" | |
#################### | |
# modify instance | |
#################### | |
echo "" | |
echo "requesting instance modifications ..." | |
# note: we may not request modifications when the instance is not available | |
# "A client error (InvalidDBInstanceState) occurred when calling the | |
# ModifyDBInstance operation: Database instance is not in available state." | |
echo "db instance values pending to be modified:" | |
aws rds modify-db-instance --db-instance-identifier $db_instance_id \ | |
--master-user-password $master_user_password \ | |
--backup-retention-period 0 --no-auto-minor-version-upgrade --apply-immediately \ | |
--query 'DBInstance.PendingModifiedValues' | |
echo "waiting for the modifications to be applied ..." | |
aws rds wait db-instance-available --db-instance-identifier $db_instance_id | |
aws rds add-tags-to-resource --resource-name $db_instance_arn --tags Key=env,Value=dev | |
echo "" | |
echo "instance master username: $master_username" | |
echo "instance master user password: $master_user_password" | |
echo "" | |
echo "command for connecting to Postgres:" | |
echo "(you may omit the password and enter it when prompted)" | |
echo " psql postgresql://$master_username:$master_user_password@$db_instance_address:5432/pstn_db" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment