Package: cdk-rds-mysql-backup-lambda
RDSMySQLBackupLambdaTheRDSMySQLBackupLambdais an AWS CDK Construct that provides an automated solution for backing up RDS MySQL databases to an S3 bucket using a Lambda function. It is designed for developers who need a flexible and cost-effective method to back up their RDS MySQL databases outside of the default RDS backup capabilities.Table of ContentsFeaturesHow It WorksInstallationUsageDocumentationContributingPull Request GuidelinesLicenseFeaturesAutomated backups of RDS MySQL databases.Backup scheduling using a cron expression.Backups stored securely in an S3 bucket with encryption.Customizable Lambda function name, timeout, and S3 bucket name.Built-in VPC and security group configurations for secure access to RDS instances.How It WorksThe RDSMySQLBackupLambda AWS CDK Construct automates the process of backing up RDS MySQL databases to S3, leveraging AWS Lambda and other AWS services. Here’s an overview of its operation:1. Lambda Function:A Lambda function is the core component that performs the database backup. It's triggered based on the specified schedule (defaulting to daily at 00:00 UTC).The function connects to the specified RDS MySQL database instance using the provided credentials and performs a mysqldump.2. VPC and Subnet Configuration:The Lambda function is deployed within the same VPC and subnet group as the RDS instance. This ensures a secure and direct connection to the RDS instance, as typically, RDS instances are placed in private subnets without direct internet access.3. Security Group Settings:The security group of the Lambda function is configured to allow outbound connections to the RDS instance on the specified port (default 3306 for MySQL).The RDS instance's security group is updated to allow inbound connections from the Lambda function's security group.4. S3 Bucket for Backup Storage:An S3 bucket is created and configured with server-side encryption (SSE-S3) for secure storage of the backup files.5. S3 VPC Endpoint:To address scenarios where the RDS instance and Lambda function reside in a subnet without internet access, the construct utilizes an S3 VPC endpoint.This endpoint provides private connectivity to S3, allowing the Lambda function to upload backup files to the S3 bucket without needing internet access.6. Backup Process:When triggered, the Lambda function initiates a backup of the specified database, generating a dump file.The dump file is then securely uploaded to the S3 bucket via the S3 VPC endpoint.InstallationTo installRDSMySQLBackupLambdaconstruct library using npm, run the following command:npmicdk-rds-mysql-backup-lambdaUsageTo initialize theRDSMySQLBackupLambdaconstruct you can use the following code:import*ascdkfrom'aws-cdk-lib';import*asec2from'aws-cdk-lib/aws-ec2';import*aseventsfrom'aws-cdk-lib/aws-events';import{Construct}from'constructs';import{RDSMySQLBackupLambda}from'cdk-rds-mysql-backup-lambda';exportclassTestStackextendscdk.Stack{constructor(scope:Construct,id:string,props:TestStackProps){super(scope,id,props);//GetVPCandSubnetswheretheRDSinstanceislocatedconstvpc=ec2.Vpc.fromVpcAttributes(this,'TestVpc',{vpcId:props.vpcId,availabilityZones:['eu-central-1a','eu-central-1b','eu-central-1c'],publicSubnetIds:props.vpcSubnetIds,});constvpcSubnets=vpc.selectSubnets({subnetType:ec2.SubnetType.PUBLIC,});constmysqlBackup=newRDSMySQLBackupLambda(this,'RDSMySQLBackupLambda',{rdsVpc:vpc,rdsVpcSubnets:vpcSubnets,rdsInstanceName:'rdsInstanceName',rdsInstancePort:3306,//optionalrdsInstanceEndpointAddress:'rdsInstanceEndpointAddress',rdsSecurityGroupId:'rdsSecurityGroupId',dbName:'db_name',dbUser:'user',dbPassword:'pass',lambdaFunctionName:'RDSMySQLBackupLambdaFunction',//optionallambdaFunctionTimeout:cdk.Duration.minutes(15),//optionals3BucketName:'rds-mysql-backups',//optionalbackupSchedule:events.Schedule.cron({hour:'0',minute:'0'}),//optional});//exportedpropertiesconsole.log(mysqlBackup.s3Bucket);//S3bucketwherethebackupsarestoredconsole.log(mysqlBackup.lambdaFunction);//LambdafunctionthatbacksuptheRDSMySQLdatabase}}DocumentationTo initialize theRDSMySQLBackupLambdaconstruct you can use the following props:/***PropertiesfortheRDSMySQLBackupLambdaconstruct.*/exportinterfaceRDSMySQLBackupLambdaPropsextendsResourceProps{/***VPCusedbytheRDSinstance.*/readonlyrdsVpc:ec2.IVpc;/***VPCsubnetgroupusedbytheRDSinstance.*/readonlyrdsVpcSubnets:ec2.SelectedSubnets;/***NameofRDSinstancetobackup.*/readonlyrdsInstanceName:string;/***EndpointaddressoftheRDSinstance.*/readonlyrdsInstanceEndpointAddress:string;/***PortoftheRDSinstance.*/readonlyrdsInstancePort?:number;/***SecuritygroupIDfortheRDSinstance.*/readonlyrdsSecurityGroupId:string;/***UsertoconnecttotheRDSinstance.*/readonlydbUser:string;/***PasswordtoconnecttotheRDSinstance.*/readonlydbPassword:string;/***Nameofthedatabasetobackup.*/readonlydbName:string;/***Nameofthelambdafunctionthatwillbecreated.**@default-[db-instance-identifier]-rds-backup-lambda*/readonlylambdaFunctionName?:string;/***Timeoutvalueforthebackuplambdafunction.**@default-5minutes*/readonlylambdaTimeout?:Duration;/***NameoftheS3buckettostoretheRDSbackups.**@default-[db-instance-identifier]-rds-backup*/readonlys3BucketName?:string;/***Scheduleforthelambdafunctiontorun.**@default-Everydayat00:00UTC*/readonlyscheduleRule?:events.Schedule;}ContributingWe welcome contributions! Please reviewcode of conductandcontributing guideso that you can understand what actions will and will not be tolerated.Pull Request GuidelinesThemainbranch is just a snapshot of the latest stable release. All development should be done in development branches.Do not submit PRs against themainbranch.Work in thesrcfolder andDO NOTcheckindistin the commits.It's OK to have multiple small commits as you work on the PRIf adding a new feature add accompanying test case.If fixing bug,Add accompanying test case if applicable.Provide a detailed description of the bug in the PR.If you are resolving an opened issue add issue number in your PR title.LicenseRDSMySQLBackupLambdaisMIT licensed.