Hard
Hard
BRUCE MOMJIAN
There are many ways to easily install Postgres in the cloud strictly
from the command line.
1 / 47
Outline
1. Why do this?
2. Setting up awscli
3. Choosing an AMI
4. Creating an EC2 instance
5. Logging in and configuring
6. Installing Postgres
7. Connecting to Postgres
2 / 47
1. Why Do This?
3 / 47
What Are We Going to Use?
• Debian 10 (Buster)
• awscli
• AWS console
• PostgreSQL source code
4 / 47
2. Setting Up awscli
https://console.aws.amazon.com/console
5 / 47
Create an Access Key
https://console.aws.amazon.com/iam/#/security_credentials
6 / 47
EC2 Console
https://console.aws.amazon.com/ec2/v2/
7 / 47
Install awscli
8 / 47
Configure awscli
$ aws configure
AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: YYYY
Default region name [None]: us-east-1
Default output format [None]: text
https://aws.amazon.com/cli/
9 / 47
Create a Key Pair
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
10 / 47
Getting awscli Help
$ aws help
AWS() AWS()
NAME
aws -
DESCRIPTION
The AWS Command Line Interface is a unified tool to manage your AWS
services.
SYNOPSIS
aws [options] <command> <subcommand> [parameters]
Use aws command help for information on a specific command. Use aws
help topics to view a list of available help topics. The synopsis for
each command shows its parameters and their usage. Optional parameters
are shown in square brackets.
11 / 47
Getting awscli Help
12 / 47
3. Choosing an AMI
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html
13 / 47
Debian AMIs
$ DEBIAN_AMI=’136693071363’
https://wiki.debian.org/Amazon/EC2/HowTo/awscli
14 / 47
Debian AMIs
15 / 47
4. Creating an EC2 Instance:
What Will You Be Charged For?
https://www.apptio.com/blog/guide-to-aws-ec2-costs/
16 / 47
Find AMI Device
17 / 47
Cheap Setup
INSTANCE_OPTS=’--instance-type t3a.nano \
--credit-specification CpuCredits=standard’
EBS="--block-device-mappings \
DeviceName=’$DEVICE’,Ebs={VolumeType=’standard’,VolumeSize=8}"
18 / 47
Creating an EC2 Instance
https://www.simplilearn.com/tutorials/aws-tutorial/aws-vpc
19 / 47
EC2 Internals
Internet
Internet Gateway
Route Table
SG 1
Security Groups: SG 1
20 / 47
EC2 Internals
21 / 47
Create VPC, With Security Group and Route Table
22 / 47
Create Gateway and Attach to VPC
23 / 47
Add Route Table Entry for the Gateway
24 / 47
Create Subnet
25 / 47
Connect the Subnet to the Route Table
26 / 47
Adjust Security Group
# ssh
aws ec2 authorize-security-group-ingress\
--group-id "$SECGROUP" \
--protocol tcp --port 22 --cidr 0.0.0.0/0
# Postgres
aws ec2 authorize-security-group-ingress \
--group-id "$SECGROUP" \
--protocol tcp --port 5432 --cidr 0.0.0.0/0
27 / 47
Create Instance in the Subnet
28 / 47
Start the Instance
29 / 47
Running EC2 Console
https://console.aws.amazon.com/ec2/v2/
30 / 47
EC2 Internals
Internet
Internet Gateway
Route Table
SG 1
Security Groups: SG 1
31 / 47
More Complexity
32 / 47
Complex Configuration
Internet
Internet Gateway
SG 1 SG 2 SG 2 SG 2 SG 3 SG 3
VPC Subnet Instance Instance Subnet Instance Instance Subnet Instance Instance
in AZ 1 in AZ 2 in AZ 3
Security Groups: SG 1, SG 2, SG 3
33 / 47
4. Logging in and Configuring
LOGIN_USER=’admin’
HOST=$(aws ec2 describe-instances --instance-ids "$INSTANCE" \
--query ’Reservations[*].Instances[*].PublicDnsName’ --output text)
34 / 47
Setup Environment
35 / 47
Setup Shell Scripts
36 / 47
Setup Email
aws# # https://unix.stackexchange.com/questions/20570/mutt-how-to-safely-store-password
# set up SMTP authentication
cat <<END_MUTT > .muttrc
set smtp_url = "smtp://[email protected]:25/"
# PASSWORD HERE
set smtp_pass = "XXXXXX"
set from = "[email protected]"
set realname = "Bruce Momjian"
END_MUTT
37 / 47
Set Prompts
38 / 47
Set Environment Variables
39 / 47
Cleanup
40 / 47
6. Installing Postgres
aws# PGVER=’12.4’
aws# wget \
> https://ftp.postgresql.org/pub/source/v$PGVER/postgresql-$PGVER.tar.bz2 &&
> bzcat postgresql-$PGVER.tar.bz2 | tar xf -
aws# cd postgresql-$PGVER
41 / 47
Creating the Data Directory
42 / 47
Configuring Security
aws# su postgres
aws# cd /usr/local/pgdata
aws# echo ’host all all 0.0.0.0/0 scram-sha-256’ >> pg_hba.conf
aws# sed \
-e ’s/#password_encryption = md5/password_encryption = scram-sha-256/’ \
-e "s/#listen_addresses = ’localhost’/listen_addresses = ’\*’/" \
postgresql.conf > /tmp/$$ && mv /tmp/$$ postgresql.conf
43 / 47
Configuring Password
postgres=# \password
Enter new password:
Enter it again:
postgres=#
44 / 47
7. Connecting to Postgres
45 / 47
Using SSH Tunneling
46 / 47
Conclusion
https://momjian.us/presentations https://www.flickr.com/photos/adai/
47 / 47