Skip to content

Commit

Permalink
L2 construct for serverless aurora implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
vahdet committed Jan 1, 2021
1 parent 52231a5 commit 58d348c
Show file tree
Hide file tree
Showing 5 changed files with 780 additions and 748 deletions.
3 changes: 1 addition & 2 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// RDS
export const auroraMajorVersion = '5.6'
export const auroraFullVersion = '5.6.10a'
// export const auroraFullVersion = '5.6.10a'
32 changes: 13 additions & 19 deletions lib/iyiye-native-cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import { CognitoNestedStack } from './nestedStack/cognito'
import { DataNestedStack } from './nestedStack/data'
import { PipelineNestedStack } from './nestedStack/pipeline'
import { StorageNestedStack } from './nestedStack/storage'
import { auroraFullVersion} from './constants'

export class IyiyeNativeCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props)

const rdsDatabaseName = `iyiye_${process.env.ENVIRONMENT}_db`
const rdsDbIngredientTableName = 'Ingredients'
const rdsDbIngredientTableName = 'Ingredients'
const rdsDbKitTableName = 'Kits'
const rdsDbKitIngredientTableName = 'KitIngredients'

Expand All @@ -29,29 +28,24 @@ export class IyiyeNativeCdkStack extends Stack {
})

// Nested stacks
const ec2Stack = new Ec2NestedStack(this, 'Ec2NestedStack', {
})
const ec2Stack = new Ec2NestedStack(this, 'Ec2NestedStack', {})

const dataStack = new DataNestedStack(this, 'DataNestedStack', {
rdsSubnetIds: ec2Stack.vpc.selectSubnets({
rdsVpc: ec2Stack.vpc,
rdsSubnets: ec2Stack.vpc.selectSubnets({
subnetType: SubnetType.PRIVATE
}).subnetIds,
rdsVpcSecurityGroupIds: [ec2Stack.rdsSecurityGroup.securityGroupId],
}),
rdsVpcSecurityGroups: [ec2Stack.rdsSecurityGroup],
rdsDbClusterIdentifier: 'iyiye-rds-cluster-1',
rdsDatabaseName,
auroraFullVersion,
shoppingCartTable: `iyiye_${process.env.ENVIRONMENT}_shopping_cart`
})

const storageStack = new StorageNestedStack(
this,
'StorageNestedStack',
{
pipelineArtifactStoreBucketName: 'iyiye-pipeline-articat-store',
metaFilesBucketName: 'iyiye-meta-files',
userFilesBucketName: 'iyiye-user-files'
}
)
const storageStack = new StorageNestedStack(this, 'StorageNestedStack', {
pipelineArtifactStoreBucketName: 'iyiye-pipeline-articat-store',
metaFilesBucketName: 'iyiye-meta-files',
userFilesBucketName: 'iyiye-user-files'
})

const cognitoStack = new CognitoNestedStack(this, 'CognitoNestedStack', {
userPoolName: 'iyiye-up',
Expand All @@ -74,7 +68,7 @@ export class IyiyeNativeCdkStack extends Stack {
getCognitoUserFunctionRepoName: 'get-cognito-user-function',
rdsBootstrapFunctionRepoName: 'rds-bootstrap-function',
rdsDbName: rdsDatabaseName,
rdsDbClusterArn: `arn:aws:rds:${this.region}:${this.account}:cluster:${dataStack.databaseCluster.ref}`,
rdsDbClusterArn: `arn:aws:rds:${this.region}:${this.account}:cluster:${dataStack.databaseCluster.clusterArn}`,
rdsDbCredentialsSecretArn: dataStack.dbSecret.secretArn,
rdsDbIngredientTableName,
rdsDbKitTableName,
Expand All @@ -86,7 +80,7 @@ export class IyiyeNativeCdkStack extends Stack {
cognitoUserPoolId: cognitoStack.userPool.userPoolId,
getCognitoUserFunctionArn: `arn:aws:lambda:${this.region}:${this.account}:function:iyiye-${process.env.ENVIRONMENT}-get-cognito-user`,
rdsDbName: rdsDatabaseName,
rdsDbClusterArn: `arn:aws:rds:${this.region}:${this.account}:cluster:${dataStack.databaseCluster.ref}`,
rdsDbClusterArn: `arn:aws:rds:${this.region}:${this.account}:cluster:${dataStack.databaseCluster.clusterArn}`,
rdsDbCredentialsSecretArn: dataStack.dbSecret.secretArn,
rdsDbIngredientTableName,
rdsDbKitTableName,
Expand Down
52 changes: 23 additions & 29 deletions lib/nestedStack/data.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { Table as DynamoDbTable, AttributeType } from '@aws-cdk/aws-dynamodb'
import { IVpc, SecurityGroup, SelectedSubnets } from '@aws-cdk/aws-ec2'
import {
CfnDBCluster as RdsCfnDBCluster,
CfnDBSubnetGroup,
DatabaseSecret
ServerlessCluster as RdsDBCluster,
Credentials,
DatabaseSecret,
DatabaseClusterEngine
} from '@aws-cdk/aws-rds'
import {
Construct,
Duration,
NestedStack,
NestedStackProps,
Tags
} from '@aws-cdk/core'

interface DataNestedStackProps extends NestedStackProps {
rdsSubnetIds: Array<string>
rdsVpcSecurityGroupIds: Array<string>
auroraMajorVersion?: string
auroraFullVersion: string
rdsVpc: IVpc
rdsSubnets: SelectedSubnets
rdsVpcSecurityGroups: Array<SecurityGroup>
rdsDbClusterIdentifier: string
rdsDatabaseName: string
shoppingCartTable: string
Expand All @@ -26,7 +28,7 @@ export class DataNestedStack extends NestedStack {
// productTable: DynamoDbTable
readonly dbSecret: DatabaseSecret
readonly shoppingCartTable: DynamoDbTable
readonly databaseCluster: RdsCfnDBCluster
readonly databaseCluster: RdsDBCluster

constructor(scope: Construct, id: string, props: DataNestedStackProps) {
super(scope, id, props)
Expand All @@ -35,29 +37,21 @@ export class DataNestedStack extends NestedStack {
username: 'root'
})

this.databaseCluster = new RdsCfnDBCluster(this, 'RdsDatabase', {
dbClusterIdentifier: props.rdsDbClusterIdentifier,
databaseName: props.rdsDatabaseName,
engine: 'aurora',
engineMode: 'serverless',
engineVersion: props.auroraFullVersion,
this.databaseCluster = new RdsDBCluster(this, 'RdsDatabase', {
engine: DatabaseClusterEngine.AURORA,
vpc: props.rdsVpc,
clusterIdentifier: props.rdsDbClusterIdentifier,
defaultDatabaseName: props.rdsDatabaseName,
deletionProtection: false,
enableHttpEndpoint: true,
backupRetentionPeriod: 7,
masterUsername: this.dbSecret.secretValueFromJson('username').toString(),
masterUserPassword: this.dbSecret
.secretValueFromJson('password')
.toString(),
vpcSecurityGroupIds: props.rdsVpcSecurityGroupIds,
dbSubnetGroupName: new CfnDBSubnetGroup(this, 'RdsSubnetGroup', {
dbSubnetGroupDescription: 'RdsSubnetGroup',
subnetIds: props.rdsSubnetIds
}).ref,
scalingConfiguration: {
autoPause: true,
enableDataApi: true,
backupRetention: Duration.days(7),
credentials: Credentials.fromSecret(this.dbSecret),
securityGroups: props.rdsVpcSecurityGroups,
vpcSubnets: props.rdsSubnets,
scaling: {
autoPause: Duration.minutes(5),
minCapacity: 1,
maxCapacity: 2,
secondsUntilAutoPause: 300
maxCapacity: 2
}
})

Expand Down
Loading

0 comments on commit 58d348c

Please sign in to comment.