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 backupsWhat you build:
- A CloudFormation modifier that sets
BackupRetentionPeriodto 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:
| Cloud | Template | Resource | Alien's value | After the extension |
|---|---|---|---|---|
| AWS | CloudFormation | AWS::RDS::DBCluster | BackupRetentionPeriod: 7 | 30 |
| AWS | Terraform | aws_rds_cluster | backup_retention_period = 7 | 30 |
| Google Cloud | Terraform | google_sql_database_instance | backup_configuration with no retention, so Cloud SQL's default applies | retained_backups = 30 |
| Azure | Terraform | azurerm_postgresql_flexible_server | backup_retention_days = 7 | 30 |
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():
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:
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:
/// 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_attributeadds or changes an attribute on the resource.block_mutopens a nested block Alien emitted, and fails if it isn't there.add_blockadds 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:
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 buildalien 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_daysThe first prints 30. alien render also lists which extension changed which resource.
Release
alien releaseThe 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:
- A deployment set up before this release gets the longer retention when its admin reruns initial setup with the new template.
- A live database is created by Alien's controllers, not by a template. It needs an interceptor on the create call, as the IAM database authentication example shows.
Next
- Template modifiers: the CloudFormation, Terraform, and Helm APIs, and what a modifier may change
- Your own tags: an interceptor for resources Alien's controllers create
- IAM database authentication: modifiers and interceptors that set one value on every path
IAM database authentication
An extension with template modifiers and interceptors that turn on IAM authentication for the Postgres database on AWS, Google Cloud, and Azure.
Your own tags
An extension with interceptors that add your tags to the resources Alien's controllers create on AWS, Google Cloud, Azure, and Kubernetes.