This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update example to use Aurora Serverless (#158)
- Loading branch information
Showing
12 changed files
with
573 additions
and
918 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,33 @@ | ||
const https = require('https'); | ||
|
||
const AWS = require('aws-sdk'); | ||
const { Client } = require('pg'); | ||
|
||
const { default: CERT_DATA } = require('./rds-combined-ca-bundle.pem'); | ||
|
||
const { SECRET_NAME } = process.env; | ||
|
||
/** | ||
* Return a new HTTPS Agent with keep-alives enabled | ||
* | ||
* @return {Agent} | ||
* @see https://github.com/aws/aws-sdk-js/blob/v2.437.0/lib/http/node.js#L141 | ||
* @see https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-configuring-maxsockets.html | ||
*/ | ||
function getKeepAliveAgent() { | ||
const options = { | ||
keepAlive: true, | ||
rejectUnauthorized: true, // from aws-sdk | ||
maxSockets: 50, // from aws-sdk | ||
}; | ||
const agent = new https.Agent(options); | ||
agent.setMaxListeners(0); // from aws-sdk | ||
return agent; | ||
} | ||
const { SECRET_ARN, RESOURCE_ARN, DATABASE_NAME, SCHEMA_NAME } = process.env; | ||
|
||
AWS.config.logger = console; | ||
|
||
AWS.config.update({ | ||
httpOptions: { | ||
agent: getKeepAliveAgent(), | ||
}, | ||
}); | ||
|
||
/** | ||
* Get a secret from Secrets Manager | ||
* | ||
* @param {String} SecretId | ||
* @return {Promise<Object>} | ||
*/ | ||
async function getSecret(SecretId) { | ||
const secretsmanager = new AWS.SecretsManager(); | ||
|
||
const params = { | ||
SecretId, | ||
VersionStage: 'AWSCURRENT', | ||
}; | ||
|
||
let data; | ||
try { | ||
data = await secretsmanager.getSecretValue(params).promise(); | ||
if (data.SecretString) { | ||
data = JSON.parse(data.SecretString); | ||
} | ||
} catch (err) { | ||
console.error(`Unable to get secret ${SecretId}:`, err); | ||
throw err; | ||
} | ||
return data; | ||
} | ||
|
||
let SECRET_DATA; | ||
|
||
/** | ||
* Postgres Handler | ||
* PostgreSQL Handler | ||
* | ||
* @param {Object} event | ||
* @param {Object} context | ||
* @return {Promise} | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
exports.handler = async (event, context) => { | ||
if (!SECRET_DATA) { | ||
SECRET_DATA = await getSecret(SECRET_NAME); | ||
} | ||
const rds = new AWS.RDSDataService(); | ||
|
||
const config = { | ||
user: SECRET_DATA.username, | ||
password: SECRET_DATA.password, | ||
database: 'postgres', | ||
host: SECRET_DATA.host, | ||
port: SECRET_DATA.port, | ||
// this object will be passed to the TLSSocket constructor | ||
ssl: { | ||
rejectUnauthorized: true, | ||
ca: CERT_DATA, | ||
cert: CERT_DATA, | ||
}, | ||
statement_timeout: 5000, // milliseconds | ||
const params = { | ||
resourceArn: RESOURCE_ARN, | ||
secretArn: SECRET_ARN, | ||
sql: 'SELECT NOW()', | ||
database: DATABASE_NAME, | ||
schema: SCHEMA_NAME, | ||
}; | ||
|
||
const client = new Client(config); | ||
await client.connect(); | ||
|
||
const res = await client.query('SELECT NOW()'); | ||
const response = await rds.executeStatement(params).promise(); | ||
|
||
const [now] = res.rows || []; | ||
const { records = [] } = response; | ||
|
||
await client.end(); | ||
const [row] = records || []; | ||
|
||
return now; | ||
return row[0].stringValue; | ||
}; |
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
--- | ||
Resources: | ||
|
||
ExampleLambdaRole: | ||
Type: 'AWS::IAM::Role' | ||
Type: "AWS::IAM::Role" | ||
Properties: | ||
Path: '/${self:service}/${self:provider.stage}/' | ||
RoleName: '${self:service}-${self:provider.stage}-${self:provider.region}-ExampleLambdaRole' | ||
RoleName: "${self:service}-${self:provider.stage}-${self:provider.region}-ExampleLambdaRole" | ||
AssumeRolePolicyDocument: | ||
Statement: | ||
- Effect: Allow | ||
Principal: | ||
Service: 'lambda.amazonaws.com' | ||
Action: 'sts:AssumeRole' | ||
Service: "lambda.amazonaws.com" | ||
Action: "sts:AssumeRole" | ||
Policies: | ||
- PolicyName: ExampleLambdaPolicy | ||
PolicyDocument: | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- 'secretsmanager:GetSecretValue' | ||
Resource: | ||
'Fn::Join': | ||
- ':' | ||
- - 'arn:aws:secretsmanager' | ||
- Ref: 'AWS::Region' | ||
- Ref: 'AWS::AccountId' | ||
- 'secret:${self:custom.secretName}-*' | ||
Statement: | ||
- Effect: Allow | ||
Action: "secretsmanager:GetSecretValue" | ||
Resource: | ||
Ref: DBSecret | ||
- Effect: Allow | ||
Action: | ||
- "rds-data:BatchExecuteStatement" | ||
- "rds-data:BeginTransaction" | ||
- "rds-data:CommitTransaction" | ||
- "rds-data:ExecuteStatement" | ||
- "rds-data:RollbackTransaction" | ||
Resource: "*" | ||
ManagedPolicyArns: | ||
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole' | ||
- "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" |
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.