Created
July 31, 2022 05:44
-
-
Save leegilmorecode/945f85f6456edddc92c406a2a2bb0a47 to your computer and use it in GitHub Desktop.
Basic example of a CDK custom resource to create a Postgres table
This file contains hidden or 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
import * as AWS from "aws-sdk"; | |
import { | |
CdkCustomResourceEvent, | |
CdkCustomResourceHandler, | |
CdkCustomResourceResponse, | |
} from "aws-lambda"; | |
import { Client } from "pg"; | |
import { v4 as uuid } from "uuid"; | |
const options = { | |
user: process.env.DB_USER, | |
host: process.env.DB_HOST, | |
password: "", | |
ssl: { | |
rejectUnauthorized: false, | |
}, | |
connectionTimeoutMillis: 1000, | |
}; | |
const signer = new AWS.RDS.Signer({ | |
region: process.env.REGION, | |
hostname: process.env.DB_HOST, | |
port: Number(process.env.DB_PORT), | |
username: process.env.DB_USER, | |
}); | |
const physicalResourceId = "OnlineDatabaseTable"; | |
let response: CdkCustomResourceResponse; | |
async function createTable(): Promise<void> { | |
const token = signer.getAuthToken({ | |
username: process.env.DB_USER, | |
}); | |
options.password = token; | |
const client: Client = new Client(options); | |
await client.connect(); | |
await client.query( | |
"CREATE TABLE IF NOT EXISTS online (username varchar(45) NOT NULL, PRIMARY KEY (username));" | |
); | |
console.log(`Table created successfully`); | |
} | |
export const createTableHandler: CdkCustomResourceHandler = async ( | |
event: CdkCustomResourceEvent | |
): Promise<CdkCustomResourceResponse> => { | |
try { | |
const correlationId = uuid(); | |
const method = "create-table.handler"; | |
const prefix = `${correlationId} - ${method}`; | |
console.log(`${prefix} - started`); | |
switch (event.RequestType) { | |
case "Create": | |
await createTable(); | |
response = { | |
Status: "SUCCESS", | |
Reason: "", | |
LogicalResourceId: event.LogicalResourceId, | |
PhysicalResourceId: physicalResourceId, | |
RequestId: event.RequestId, | |
StackId: event.StackId, | |
}; | |
break; | |
case "Update": | |
await createTable(); | |
response = { | |
Status: "SUCCESS", | |
Reason: "", | |
LogicalResourceId: event.LogicalResourceId, | |
PhysicalResourceId: physicalResourceId, | |
RequestId: event.RequestId, | |
StackId: event.StackId, | |
}; | |
break; | |
case "Delete": | |
// we do nothing as the table will be removed | |
response = { | |
Status: "SUCCESS", | |
Reason: "", | |
LogicalResourceId: event.LogicalResourceId, | |
PhysicalResourceId: physicalResourceId, | |
RequestId: event.RequestId, | |
StackId: event.StackId, | |
}; | |
break; | |
default: | |
throw new Error(`${prefix} - event request type not found`); | |
} | |
console.log(`${prefix} - response: ${JSON.stringify(response)}`); | |
return response; | |
} catch (error) { | |
console.error(error); | |
return { | |
Status: "FAILED", | |
Reason: JSON.stringify(error), | |
LogicalResourceId: event.LogicalResourceId, | |
PhysicalResourceId: physicalResourceId, | |
RequestId: event.RequestId, | |
StackId: event.StackId, | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment