-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Syncing Lambda IP ranges to RDS SG & running DB health check every min
- Loading branch information
1 parent
a53efb9
commit e97d101
Showing
8 changed files
with
82 additions
and
8 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
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 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,3 +1,4 @@ | ||
export * from './syncGithub' | ||
export * from './syncCalendar' | ||
export * from './syncNotion' | ||
export * from './syncNotion' | ||
export * from './runEveryMinute' |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { graphqlQuery } from '#src/utils'; | ||
import { EC2Client, DescribeSecurityGroupsCommand, RevokeSecurityGroupIngressCommand, AuthorizeSecurityGroupIngressCommand } from "@aws-sdk/client-ec2"; | ||
const ec2Client = new EC2Client(); | ||
|
||
export const runEveryMinute = async () => { | ||
// Sync IP Ranges | ||
const newIps = (await (await fetch(`https://ip-ranges.amazonaws.com/ip-ranges.json`)).json()).prefixes | ||
.filter(({ region, service }) => region === 'us-east-2' && service === 'EC2').map(obj => obj.ip_prefix); | ||
const existingIps = (await ec2Client.send(new DescribeSecurityGroupsCommand({ GroupIds: [process.env.DB_SECURITY_GROUP] }))) | ||
.SecurityGroups?.map(securityGroup => securityGroup?.IpPermissions?.map(ipPermission => ipPermission.IpRanges?.map(ipRange => ipRange.CidrIp)).flat()).flat(); | ||
const addedIps = newIps.filter(newIp => !existingIps.includes(newIp)); | ||
const removedIps = existingIps.filter(existingIp => !newIps.includes(existingIp)); | ||
if (removedIps.length > 0) { | ||
await ec2Client.send(new RevokeSecurityGroupIngressCommand({ | ||
GroupId: process.env.DB_SECURITY_GROUP, | ||
IpPermissions: removedIps.map(ip => ({ | ||
FromPort: 5432, | ||
IpProtocol: 'tcp', | ||
IpRanges: [{ | ||
CidrIp: ip, | ||
Description: 'Lambda' | ||
}], | ||
ToPort: 5432, | ||
})) | ||
})); | ||
} | ||
if (addedIps.length > 0) { | ||
await ec2Client.send(new AuthorizeSecurityGroupIngressCommand({ | ||
GroupId: process.env.DB_SECURITY_GROUP, | ||
IpPermissions: addedIps.map(ip => ({ | ||
FromPort: 5432, | ||
IpProtocol: 'tcp', | ||
IpRanges: [{ | ||
CidrIp: ip, | ||
Description: 'Lambda' | ||
}], | ||
ToPort: 5432, | ||
})) | ||
})); | ||
} | ||
|
||
// Perform health check | ||
const response = await graphqlQuery({ query: `{apps(limit:1){id}}` }); | ||
if (!response?.apps?.[0]?.id) { | ||
await fetch(process.env.SLACK_WEBHOOK, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
text: `*heythisischris* - Database connection failure detected` | ||
}) | ||
}) | ||
} | ||
return; | ||
} |
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 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { event, isStaging } from '.'; | ||
|
||
export const graphqlQuery = async ({ query, variables = {} }) => { | ||
const Authorization = event?.headers?.authorization; | ||
const response = (await (await fetch(`https://graphql${isStaging ? '-staging' : ''}.heythisischris.com/v1/graphql`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...Authorization ? { Authorization } : { 'x-hasura-admin-secret': process.env.DB_PASSWORD }, | ||
}, | ||
body: JSON.stringify({ query, variables }), | ||
})).json()); | ||
if (!response?.data) { | ||
console.log(JSON.stringify({ graphqlError: response })); | ||
} | ||
return response?.data; | ||
} |
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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isStaging = process.env.STAGING ? '-staging' : ''; |