Skip to content

Commit

Permalink
Enable DynamoDB stream on repo activity table
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed May 17, 2024
1 parent cf2e72d commit 3e28ec4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
21 changes: 19 additions & 2 deletions src/cdk/stacks/StorageStack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Aws, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib'
import { Construct } from 'constructs'
import { AttributeType, TableV2 } from 'aws-cdk-lib/aws-dynamodb'
import { AttributeType, StreamViewType, TableV2 } from 'aws-cdk-lib/aws-dynamodb'
import { saveInSSMViaCloudFormation } from '../support/ssm'
import { AllStacksProps } from '../config/allStacksProps'
import { BlockPublicAccess, Bucket, IBucket, ObjectOwnership } from 'aws-cdk-lib/aws-s3'
import { SSM_PARAM_NAMES, SsmParamName, ssmTableNamePath } from '../../multipleContexts/ssmParams'
import {
SSM_PARAM_NAMES,
SsmParamName,
ssmTableNamePath,
ssmTableStreamPath
} from '../../multipleContexts/ssmParams'
import { CICADA_TABLE_IDS, CicadaTableId, tableConfigurations } from '../../multipleContexts/dynamoDBTables'
import { CfnDatabase } from 'aws-cdk-lib/aws-glue'
import { CfnWorkGroup } from 'aws-cdk-lib/aws-athena'
Expand Down Expand Up @@ -48,6 +53,11 @@ function defineTable(scope: Construct, props: AllStacksProps, tableId: CicadaTab
name: 'PK',
type: AttributeType.STRING
},
...(config.stream
? {
dynamoStream: StreamViewType.NEW_IMAGE
}
: {}),
...(config.hasSortKey
? {
sortKey: {
Expand Down Expand Up @@ -76,6 +86,13 @@ function defineTable(scope: Construct, props: AllStacksProps, tableId: CicadaTab
})

saveInSSMViaCloudFormation(scope, props, ssmTableNamePath(tableId), table.tableName)

if (config.stream) {
if (tableId !== 'github-repo-activity')
throw new Error(`Streaming config not currently setup for table ${tableId}`)
if (!table.tableStreamArn) throw new Error(`Stream has not been configured for table ${tableId}`)
saveInSSMViaCloudFormation(scope, props, ssmTableStreamPath(tableId), table.tableStreamArn)
}
}

function defineBucket(
Expand Down
30 changes: 19 additions & 11 deletions src/multipleContexts/dynamoDBTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Used by CDK for deployment and application code for configuration

// If adding here, also add to SsmTableNameParamName type

export const CICADA_TABLE_IDS = [
'github-installations',
'github-users',
Expand All @@ -15,40 +16,47 @@ export const CICADA_TABLE_IDS = [
export type CicadaTableId = (typeof CICADA_TABLE_IDS)[number]

interface CicadaTableConfig {
hasSortKey: boolean
hasGSI1: boolean
readonly hasSortKey: boolean
readonly hasGSI1: boolean
readonly stream: boolean
}

const allFalseConfig: CicadaTableConfig = {
hasGSI1: false,
hasSortKey: false,
stream: false
}

export const tableConfigurations: Record<CicadaTableId, CicadaTableConfig> = {
'github-installations': {
hasSortKey: false,
hasGSI1: false
},
'github-users': {
hasSortKey: false,
hasGSI1: false
},
'github-installations': allFalseConfig,
'github-users': allFalseConfig,
'github-account-memberships': {
...allFalseConfig,
hasSortKey: true,
hasGSI1: true
},
'github-repositories': {
...allFalseConfig,
hasSortKey: true,
hasGSI1: false
},
'github-repo-activity': {
hasSortKey: true,
hasGSI1: true
hasGSI1: true,
stream: true
},
'github-latest-workflow-runs': {
...allFalseConfig,
hasSortKey: true,
hasGSI1: true
},
'github-latest-pushes-per-ref': {
...allFalseConfig,
hasSortKey: true,
hasGSI1: true
},
'web-push-subscriptions': {
...allFalseConfig,
hasSortKey: true,
hasGSI1: false
}
Expand Down
5 changes: 5 additions & 0 deletions src/multipleContexts/ssmParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export function ssmTableNamePath(id: CicadaTableId): SsmParamName {
return `resources/table/${id}`
}

// For now, only table with a stream
export function ssmTableStreamPath(id: 'github-repo-activity'): SsmParamName {
return `resources/tableStreamArn/${id}`
}

export function createFullParameterName({ appName }: { appName: string }, key: string) {
return `/${appName}/${key}`
}

0 comments on commit 3e28ec4

Please sign in to comment.