Last active
June 7, 2020 13:36
-
-
Save metaskills/e17361340a9149436f2893196b1b9633 to your computer and use it in GitHub Desktop.
Aurora Serverless IaC Example Using CDK & TypeScript
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
// Run `mkdir aurora && cd aurora && cdk init sample-app --language=typescript` | |
// and replace the `lib/aurora-stack.ts` with this file. May need to remove non-needed | |
// packages from package.json and add others like "@aws-cdk/aws-rds" and | |
// "@aws-cdk/aws-secretsmanager" | |
// | |
// Running assumes you pass in the following environment variables or hard code values. | |
// | |
// * ASCLUSTER_MASTER_USER | |
// * ASCLUSTER_MASTER_PASS | |
// * SUBNET_GROUP_NAME | |
// | |
// For an example that also creates a VPC, Subnet Group, & User: https://git.io/JexsN | |
// | |
import { CfnOutput, Construct, Stack, StackProps } from "@aws-cdk/core"; | |
import { CfnDBCluster, CfnDBClusterParameterGroup } from "@aws-cdk/aws-rds"; | |
import { CfnSecret } from "@aws-cdk/aws-secretsmanager"; | |
const MASTER_USER = process.env.ASCLUSTER_MASTER_USER; | |
const MASTER_PASS = process.env.ASCLUSTER_MASTER_PASS; | |
const SUBNET_GROUP_NAME = process.env.SUBNET_GROUP_NAME; | |
export class AuroraStack extends Stack { | |
constructor(scope: Construct, id: string, props?: StackProps) { | |
super(scope, id, props); | |
// RDS PARAMETER GROUP | |
const dbParamGroup = new CfnDBClusterParameterGroup( | |
this, | |
"ParameterGroup", | |
{ | |
family: "aurora5.6", | |
description: "My Aurora Serverless Database Parameters.", | |
parameters: { | |
innodb_large_prefix: "1", | |
innodb_file_per_table: "1", | |
innodb_file_format: "Barracuda", | |
character_set_client: "utf8mb4", | |
character_set_connection: "utf8mb4", | |
character_set_database: "utf8mb4", | |
character_set_results: "utf8mb4", | |
character_set_server: "utf8mb4", | |
collation_server: "utf8mb4_unicode_ci", | |
collation_connection: "utf8mb4_unicode_ci" | |
} | |
} | |
); | |
// AURORA SERVERLESS CLUSTERS | |
const aurora = new CfnDBCluster(this, "AuroraServerless", { | |
databaseName: "mydatabase", | |
dbClusterIdentifier: "mydatabase", | |
engine: "aurora", | |
engineMode: "serverless", | |
masterUsername: MASTER_USER, | |
masterUserPassword: MASTER_PASS, | |
enableHttpEndpoint: true, | |
port: 3306, | |
dbSubnetGroupName: SUBNET_GROUP_NAME, | |
dbClusterParameterGroupName: dbParamGroup.ref, | |
scalingConfiguration: { | |
autoPause: true, | |
minCapacity: 1, | |
maxCapacity: 4, | |
secondsUntilAutoPause: 10800 // 3 hours. | |
} | |
}); | |
aurora.addDependsOn(dbParamGroup); | |
new CfnOutput(this, "MyAuroraResourceArn", { | |
value: `arn:aws:rds:${this.region}:${this.account}:cluster:mydatabase` | |
}); | |
// SECRETS | |
const secret = new CfnSecret(this, "Secret", { | |
secretString: JSON.stringify({ | |
username: MASTER_USER, | |
password: MASTER_PASS, | |
engine: "mysql", | |
host: aurora.attrEndpointAddress, | |
port: aurora.attrEndpointPort, | |
dbClusterIdentifier: "mydatabase" | |
}), | |
description: "My Aurora Master Credentials." | |
}); | |
secret.addDependsOn(aurora); | |
new CfnOutput(this, "MySecretArn", { | |
value: secret.ref | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment