Skip to content

Commit

Permalink
Node20.x and deps bump (#35)
Browse files Browse the repository at this point in the history
* Node20.x and deps bump
* Switch to node20 runtime
* Remove globally installed serverless framework and update lock files
* Switch to new cert ca
  • Loading branch information
Rocco Zanni authored Nov 28, 2023
1 parent 7dd9867 commit 0202e5e
Show file tree
Hide file tree
Showing 14 changed files with 10,364 additions and 35,638 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
- run: npm ci
working-directory: ./monitor
- run: npm ci
Expand Down
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM amazonlinux:2023.2.20230920.1
FROM public.ecr.aws/lambda/nodejs:20
ARG BUILDPLATFORM TARGETPLATFORM TARGETOS TARGETARCH

# Install nodejs
RUN yum install nodejs npm git wget unzip -y && yum clean all
RUN dnf install wget unzip -y && dnf clean all

# Install awscli
RUN if [ "$TARGETARCH" = "arm64" ]; then \
Expand All @@ -25,9 +25,6 @@ RUN mkdir -p /workspace/poduptime \
/workspace/poduptime/website
WORKDIR /workspace/poduptime

# Install the serverless framework
RUN npm install -g serverless@^3.35.2

# Install deps
COPY analytics/package*.json analytics
RUN cd analytics && npm install --production=false --loglevel=error
Expand All @@ -41,4 +38,7 @@ COPY analytics analytics
COPY monitor monitor
COPY website website
COPY conf_test.js .*
COPY package*.json ./
COPY package*.json ./

# Reset endpoint defined in base image
ENTRYPOINT []
33 changes: 32 additions & 1 deletion analytics/common/database.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pg from 'pg';
import { formatISO, parseISO } from 'date-fns';
import { Signer } from "@aws-sdk/rds-signer";
import fs from 'fs';
import url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const { Pool, types } = pg;

Expand All @@ -13,6 +16,7 @@ types.setTypeParser(1114, x => formatISO(parseISO(x)));

let pool = null;
let expiresAtEpoch = null;
let rdsCertificate = null;

const EXPIRE_TOLERANCE = 60 * 1000; // 1 minute
const TOKEN_LIFETIME = 15 * 60 * 1000; // 15 minutes
Expand Down Expand Up @@ -40,6 +44,26 @@ const _getAuthToken = async (hostname, port, username) => {
}
};

const _getSslCertificate = () => {

if (rdsCertificate) {
return Promise.resolve(rdsCertificate);
}

return new Promise((resolve, reject) => {
const filepath = `${__dirname}../support/ssl/us-east-1-bundle.pem`;
fs.readFile(filepath, (err, content) => {
if (err) {
rdsCertificate = null;
reject(err);
} else {
rdsCertificate = content;
resolve(content);
}
});
});
}

const _epoch = () => {
return (new Date()).getTime();
}
Expand Down Expand Up @@ -77,6 +101,13 @@ const _getPool = async () => {
let { token, ttl } = await _getAuthToken(
process.env.PGHOST, parseInt(process.env.PGPORT, 10), process.env.PGUSER
);

// Use ssl if we're running in AWS Lambda
let ssl = null;
if (process.env.AWS_EXECUTION_ENV) {
ssl = { ca: await _getSslCertificate() };
}

expiresAtEpoch = _epoch() + ttl;

pool = new Pool({
Expand All @@ -85,7 +116,7 @@ const _getPool = async () => {
database: process.env.PGDATABASE,
port: process.env.PGPORT,
password: token,
ssl: !!process.env.AWS_EXECUTION_ENV
ssl: ssl
});

return pool;
Expand Down
4 changes: 3 additions & 1 deletion analytics/deployment/resources/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Resources:
DBClusterIdentifier: !Ref MainDatabaseCluster
DBInstanceIdentifier: ${self:custom.appPrefix}-${opt:stage}-main-instance-1
EnablePerformanceInsights: true
CACertificateIdentifier: rds-ca-rsa2048-g1
MainDatabaseInstanceTwo:
Condition: EnableDatabaseRedundancy
Type: 'AWS::RDS::DBInstance'
Expand All @@ -44,4 +45,5 @@ Resources:
DBInstanceClass: db.serverless
DBClusterIdentifier: !Ref MainDatabaseCluster
DBInstanceIdentifier: ${self:custom.appPrefix}-${opt:stage}-main-instance-2
EnablePerformanceInsights: true
EnablePerformanceInsights: true
CACertificateIdentifier: rds-ca-rsa2048-g1
Loading

0 comments on commit 0202e5e

Please sign in to comment.