logo

Database

Need

Implementation of safeguards to prevent accidental or unauthorized deletion of databases in Amazon Web Services RDS.

Context

• Usage of AWS CloudFormation for Infrastructure as Code (IaC)

• Usage of AWS SDK for interacting with Amazon Web Services

Description

1. Non compliant code

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyDBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      AllocatedStorage: 20
      DBInstanceClass: db.t2.micro
      EngineVersion: 5.7...

This CloudFormation template creates an AWS RDS instance. However, it lacks safeguards against accidental or unauthorized deletion. Specifically: - `DeletionProtection` is not enabled, which means the database can be deleted without any restrictions. - `SkipFinalSnapshot` is set to `true`, which prevents a final snapshot from being created when the DB instance is deleted. This leads to permanent loss of data if deleted.

2. Steps

• Enable `DeletionProtection` in the RDS instance configuration.

• Ensure `SkipFinalSnapshot` is set to `false` to retain a snapshot.

• Enable backups by setting a `BackupRetentionPeriod`.

• Apply proper IAM access controls and enable monitoring via CloudTrail.

3. Secure code example

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyDBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      AllocatedStorage: 20
      DBInstanceClass: db.t2.micro
      EngineVersion: 5.7...

This improved CloudFormation template addresses the risks by: 1. Setting `DeletionProtection` to `true`, preventing unintended deletion. 2. Setting `SkipFinalSnapshot` to `false`, ensuring a snapshot is taken before deletion. 3. Defining `BackupRetentionPeriod` to `7` to allow recovery to a point in time within the last 7 days. These changes help mitigate accidental or malicious deletion and ensure data recovery options are in place.