Docs

Keep Postgres backups longer

This extension raises your Postgres database's backup retention to 30 days. It is two template modifiers, one for CloudFormation and one for Terraform, so every setup template your customers get carries the longer retention.

alien init extensions/postgres-backup-retention backups

What you build:

  • A CloudFormation modifier that sets BackupRetentionPeriod to 30 on every Aurora cluster.
  • A Terraform modifier that sets the same retention in the AWS, Google Cloud, and Azure modules.
  • A stack that lists the extension, and a template you can read before any customer runs it.

Source: examples/extensions/postgres-backup-retention.

What Alien emits

The modifiers change one value Alien already writes for a frozen Postgres. Postgres has no config field for it, so a modifier is how you change it:

CloudTemplateResourceAlien's valueAfter the extension
AWSCloudFormationAWS::RDS::DBClusterBackupRetentionPeriod: 730
AWSTerraformaws_rds_clusterbackup_retention_period = 730
Google CloudTerraformgoogle_sql_database_instancebackup_configuration with no retention, so Cloud SQL's default appliesretained_backups = 30
AzureTerraformazurerm_postgresql_flexible_serverbackup_retention_days = 730

The Kubernetes setup templates for EKS, GKE, and AKS create the database with the same resources, so the modifiers cover pull deployments on those clusters too.

In your stack

Import the TypeScript that alien extensions build writes, and list the extension in .extensions():

alien.ts
import * as alien from "@alienplatform/core"
import backups from "./backups/generated"

const db = new alien.Postgres("db").build()

export default new alien.Stack("my-app")
  .extensions([backups])
  .add(db, "frozen")
  .build()

The database is frozen, so the customer's admin creates it at initial setup from the setup template.

The CloudFormation modifier

A template modifier gets the template Alien generated and returns a changed one. This one finds every Aurora cluster and raises its retention:

backups/hooks/src/lib.rs
use alien_extension_sdk::prelude::*;

/// Keeps every RDS cluster's backups for 30 days.
struct CloudFormation;

impl CloudFormationModifier for CloudFormation {
    fn modify(&self, mut template: CfTemplate) -> Result<CfTemplate> {
        for cluster in template.resources_of_type_mut("AWS::RDS::DBCluster") {
            cluster.set_property("BackupRetentionPeriod", 30);
        }
        Ok(template)
    }
}

The Terraform modifier

A Terraform module holds one cloud's resources, so one modifier handles all three. It edits Alien's own files in place and leaves every other line as Alien wrote it:

backups/hooks/src/lib.rs
/// The same retention in the Terraform module, on each cloud.
struct Terraform;

impl TerraformModifier for Terraform {
    fn modify(&self, mut module: TfModule) -> Result<TfModule> {
        for cluster in module.resources_of_type_mut("aws_rds_cluster") {
            cluster.set_attribute("backup_retention_period", 30);
        }
        for instance in module.resources_of_type_mut("google_sql_database_instance") {
            instance
                .block_mut("settings")?
                .block_mut("backup_configuration")?
                .add_block("backup_retention_settings")
                .set_attribute("retained_backups", 30);
        }
        for server in module.resources_of_type_mut("azurerm_postgresql_flexible_server") {
            server.set_attribute("backup_retention_days", 30);
        }
        Ok(module)
    }
}
  • set_attribute adds or changes an attribute on the resource.
  • block_mut opens a nested block Alien emitted, and fails if it isn't there. add_block adds a new nested block inside it.
  • Cloud SQL counts retention in backups, not days. It takes one automated backup a day, so 30 backups cover about 30 days. Point-in-time recovery keeps its own log window, at most 7 days on the Enterprise edition Alien uses.

Register the modifiers

register lists what the extension adds, and export! names it backups:

backups/hooks/src/lib.rs
fn register(ext: &mut Extension) {
    ext.cloudformation_modifier(CloudFormation);
    ext.terraform_modifier(Terraform);
}

alien_extension_sdk::export!("backups", register);

The modifiers run when Alien builds the setup template, in alien render and in your setup packages. They never run in your customer's cloud.

Try it

Test and build

cd backups
alien extensions test
alien extensions build

alien extensions test runs the crate's tests natively and inside the WebAssembly module Alien loads. alien extensions build writes generated/index.ts, the TypeScript your stack imports.

Read the templates

From your stack's directory, render each template and find the retention:

alien render --format cloudformation --stack alien.ts --json \
  | jq '.Resources[] | select(.Type == "AWS::RDS::DBCluster") | .Properties.BackupRetentionPeriod'

alien render --format terraform --target gcp --stack alien.ts | grep -A 2 backup_retention_settings
alien render --format terraform --target azure --stack alien.ts | grep backup_retention_days

The first prints 30. alien render also lists which extension changed which resource.

Release

alien release

The release pins the extension's digest. Setup templates built from it keep backups for 30 days.

Existing and live databases

The modifiers change the setup template, so they reach a database only when a template creates or updates it:

Next

On this page