Skip to content

Commit 22e0b24

Browse files
authored
Merge pull request #52 from jameshy/update-1.5
Update 1.5
2 parents e37ae3e + decd53b commit 22e0b24

30 files changed

+2149
-3307
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
end_of_line = lf

README.md

+51-40
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,43 @@ It can be configured to run periodically using CloudWatch events.
1111

1212
1. Create an AWS lambda function:
1313
- Author from scratch
14-
- Runtime: Node.js 14.x
14+
- Runtime: Node.js 16.x
15+
- Architecture: x86_64
1516
2. tab "Code" -> "Upload from" -> ".zip file":
1617
- Upload ([pgdump-aws-lambda.zip](https://github.com/jameshy/pgdump-aws-lambda/releases/latest))
1718
- tab "Configuration" -> "General Configuration" -> "Edit"
1819
- Timeout: 15 minutes
1920
- Edit the role and attach the policy "AmazonS3FullAccess"
2021
- Save
21-
3. Test
22+
3. Give your lambda permissions permissions to write to S3:
23+
24+
- tab "Configuration" -> "Permissions"
25+
- click the existing Execution role
26+
- "Add permissions" -> "Attach policies"
27+
- select "AmazonS3FullAccess" and click "Attach policies"
28+
29+
4. Test
30+
2231
- Create new test event, e.g.:
32+
2333
```json
2434
{
2535
"PGDATABASE": "dbname",
2636
"PGUSER": "postgres",
2737
"PGPASSWORD": "password",
2838
"PGHOST": "host",
29-
"S3_BUCKET" : "db-backups",
39+
"S3_BUCKET": "db-backups",
3040
"ROOT": "hourly-backups"
3141
}
3242
```
33-
- *Test* and check the output
3443

35-
4. Create a CloudWatch rule:
44+
- _Test_ and check the output
45+
46+
5. Create a CloudWatch rule:
3647
- Event Source: Schedule -> Fixed rate of 1 hour
3748
- Targets: Lambda Function (the one created in step #1)
3849
- Configure input -> Constant (JSON text) and paste your config (as per previous step)
3950

40-
4151
#### File Naming
4252

4353
This function will store your backup with the following s3 key:
@@ -46,8 +56,8 @@ s3://${S3_BUCKET}${ROOT}/YYYY-MM-DD/[email protected]
4656

4757
#### AWS Firewall
4858

49-
- If you run the Lambda function outside a VPC, you must enable public access to your database instance, a non VPC Lambda function executes on the public internet.
50-
- If you run the Lambda function inside a VPC, you must allow access from the Lambda Security Group to your database instance. Also you must either add a NAT gateway ([chargeable](https://aws.amazon.com/vpc/pricing/)) to your VPC so the Lambda can connect to S3 over the Internet, or add an [S3 VPC endpoint (free)](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-s3.html) and allow traffic to the appropriate S3 prefixlist.
59+
- If you run the Lambda function outside a VPC, you must enable public access to your database instance, a non VPC Lambda function executes on the public internet.
60+
- If you run the Lambda function inside a VPC, you must allow access from the Lambda Security Group to your database instance. Also you must either add a NAT gateway ([chargeable](https://aws.amazon.com/vpc/pricing/)) to your VPC so the Lambda can connect to S3 over the Internet, or add an [S3 VPC endpoint (free)](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-s3.html) and allow traffic to the appropriate S3 prefixlist.
5161

5262
#### Encryption
5363

@@ -59,7 +69,7 @@ You can add an encryption key to your event, e.g.
5969
"PGUSER": "postgres",
6070
"PGPASSWORD": "password",
6171
"PGHOST": "host",
62-
"S3_BUCKET" : "db-backups",
72+
"S3_BUCKET": "db-backups",
6373
"ROOT": "hourly-backups",
6474
"ENCRYPT_KEY": "c0d71d7ae094bdde1ef60db8503079ce615e71644133dc22e9686dc7216de8d0"
6575
}
@@ -88,14 +98,13 @@ Your context may require that you use IAM-based authentication to log into the P
8898
Support for this can be enabled my making your Cloudwatch Event look like this.
8999

90100
```json
91-
92101
{
93-
"PGDATABASE": "dbname",
94-
"PGUSER": "postgres",
95-
"PGHOST": "host",
96-
"S3_BUCKET" : "db-backups",
97-
"ROOT": "hourly-backups",
98-
"USE_IAM_AUTH": true
102+
"PGDATABASE": "dbname",
103+
"PGUSER": "postgres",
104+
"PGHOST": "host",
105+
"S3_BUCKET": "db-backups",
106+
"ROOT": "hourly-backups",
107+
"USE_IAM_AUTH": true
99108
}
100109
```
101110

@@ -111,67 +120,69 @@ NOTE: the execution role for the Lambda function must have access to GetSecretVa
111120
Support for this can be enabled by setting the SECRETS_MANAGER_SECRET_ID, so your Cloudwatch Event looks like this:
112121

113122
```json
114-
115123
{
116-
"SECRETS_MANAGER_SECRET_ID": "my/secret/id",
117-
"S3_BUCKET" : "db-backups",
118-
"ROOT": "hourly-backups"
124+
"SECRETS_MANAGER_SECRET_ID": "my/secret/id",
125+
"S3_BUCKET": "db-backups",
126+
"ROOT": "hourly-backups"
119127
}
120128
```
121129

122-
If you supply `SECRETS_MANAGER_SECRET_ID`, you can ommit the 'PG*' keys, and they will be fetched from your SecretsManager secret value instead with the following mapping:
123-
124-
| Secret Value | PG-Key |
125-
| ------------- | ------------- |
126-
| username | PGUSER |
127-
| password | PGPASSWORD |
128-
| dbname | PGDATABASE |
129-
| host | PGHOST |
130-
| port | PGPORT |
130+
If you supply `SECRETS_MANAGER_SECRET_ID`, you can ommit the 'PG\*' keys, and they will be fetched from your SecretsManager secret value instead with the following mapping:
131131

132+
| Secret Value | PG-Key |
133+
| ------------ | ---------- |
134+
| username | PGUSER |
135+
| password | PGPASSWORD |
136+
| dbname | PGDATABASE |
137+
| host | PGHOST |
138+
| port | PGPORT |
132139

133-
You can provide overrides in your event to any PG* keys as event parameters will take precedence over secret values.
140+
You can provide overrides in your event to any PG\* keys as event parameters will take precedence over secret values.
134141

135142
## Developer
136143

137144
#### Bundling a new `pg_dump` binary
145+
138146
1. Launch an EC2 instance with the Amazon Linux 2 AMI
139147
2. Connect via SSH and:
148+
140149
```bash
141150

142-
# install postgres 13
151+
# install postgres 15
143152
sudo amazon-linux-extras install epel
144153

145154
sudo tee /etc/yum.repos.d/pgdg.repo<<EOF
146-
[pgdg13]
147-
name=PostgreSQL 13 for RHEL/CentOS 7 - x86_64
148-
baseurl=https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-7-x86_64
155+
[pgdg15]
156+
name=PostgreSQL 15 for RHEL/CentOS 7 - x86_64
157+
baseurl=https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-7-x86_64
149158
enabled=1
150159
gpgcheck=0
151160
EOF
152161

153-
sudo yum install postgresql13 postgresql13-server
162+
sudo yum install postgresql15 postgresql15-server
154163

155164
exit
156165
```
157166

158167
#### Download the binaries
159168

160169
```bash
161-
scp -i ~/aws.pem ec2-user@18.157.84.236:/usr/bin/pg_dump ./bin/postgres-13.3/pg_dump
162-
scp -i ~/aws.pem ec2-user@18.157.84.236:/usr/lib64/{libcrypt.so.1,libnss3.so,libsmime3.so,libssl3.so,libsasl2.so.3,liblber-2.4.so.2,libldap_r-2.4.so.2} ./bin/postgres-13.3/
163-
scp -i ~/aws.pem ec2-user@18.157.84.236:/usr/pgsql-13/lib/libpq.so.5 ./bin/postgres-13.3/libpq.so.5
170+
scp ec2-user@your-ec2-hostname:/usr/bin/pg_dump ./bin/postgres-15.0/pg_dump
171+
scp ec2-user@your-ec2-hostname:/usr/lib64/{libcrypt.so.1,libnss3.so,libsmime3.so,libssl3.so,libsasl2.so.3,liblber-2.4.so.2,libldap_r-2.4.so.2} ./bin/postgres-15.0/
172+
scp ec2-user@your-ec2-hostname:/usr/pgsql-15/lib/libpq.so.5 ./bin/postgres-15.0/libpq.so.5
164173
```
174+
165175
3. To use the new postgres binary pass PGDUMP_PATH in the event:
176+
166177
```json
167178
{
168-
"PGDUMP_PATH": "bin/postgres-13.3"
179+
"PGDUMP_PATH": "bin/postgres-15.0"
169180
}
170181
```
171182

172183
#### Creating a new function zip
173184

174-
`npm run deploy`
185+
`npm run makezip`
175186

176187
#### Contributing
177188

bin/makezip.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cp -R * $BUNDLE_DIR/
2626
pushd $BUNDLE_DIR > /dev/null
2727
echo "cleaning.."
2828
rm -rf node_modules/*
29-
npm install --production --no-progress > /dev/null
29+
npm install --omit=dev --no-progress > /dev/null
3030
rm -rf dist coverage test
3131

3232
# create zip of bundle/

bin/postgres-11.6/liblber-2.4.so.2

100755100644
File mode changed.

bin/postgres-11.6/libldap_r-2.4.so.2

100755100644
File mode changed.

bin/postgres-11.6/libnss3.so

100755100644
File mode changed.

bin/postgres-11.6/libpq.so.5

100755100644
File mode changed.

bin/postgres-11.6/libsasl2.so.3

100755100644
File mode changed.

bin/postgres-11.6/libsmime3.so

100755100644
File mode changed.

bin/postgres-11.6/libssl3.so

100755100644
File mode changed.

bin/postgres-13.3/libcrypt.so.1

100755100644
File mode changed.

bin/postgres-13.3/liblber-2.4.so.2

100755100644
File mode changed.

bin/postgres-13.3/libldap_r-2.4.so.2

100755100644
File mode changed.

bin/postgres-13.3/libnss3.so

100755100644
File mode changed.

bin/postgres-13.3/libpq.so.5

100755100644
File mode changed.

bin/postgres-13.3/libsasl2.so.3

100755100644
File mode changed.

bin/postgres-13.3/libsmime3.so

100755100644
File mode changed.

bin/postgres-13.3/libssl3.so

100755100644
File mode changed.

bin/postgres-15.0/libcrypt.so.1

40.1 KB
Binary file not shown.

bin/postgres-15.0/liblber-2.4.so.2

60.4 KB
Binary file not shown.

bin/postgres-15.0/libldap_r-2.4.so.2

356 KB
Binary file not shown.

bin/postgres-15.0/libnss3.so

1.19 MB
Binary file not shown.

bin/postgres-15.0/libpq.so.5

334 KB
Binary file not shown.

bin/postgres-15.0/libsasl2.so.3

114 KB
Binary file not shown.

bin/postgres-15.0/libsmime3.so

156 KB
Binary file not shown.

bin/postgres-15.0/libssl3.so

387 KB
Binary file not shown.

bin/postgres-15.0/pg_dump

380 KB
Binary file not shown.

lib/config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const path = require("path");
1+
const path = require('path');
22

33
// default config that is overridden by the Lambda event
44
module.exports = {
55
S3_REGION: 'eu-west-1',
6-
PGDUMP_PATH: path.join(__dirname, "../bin/postgres-14.5"),
6+
PGDUMP_PATH: path.join(__dirname, '../bin/postgres-15.0'),
77
// maximum time allowed to connect to postgres before a timeout occurs
88
PGCONNECT_TIMEOUT: 15,
99
USE_IAM_AUTH: false,

0 commit comments

Comments
 (0)