Add your tags to Alien's resources
This extension adds your tags to the resources Alien's controllers create in your customer's environment. It is a set of interceptors, one per cloud, that add team and owner to each create call before Alien signs it.
alien init extensions/resource-tags tagsWhat you build:
- AWS interceptors for a query API (RDS) and a JSON API (DynamoDB), which carry tags in different places.
- The same tags as labels on Google Cloud and Kubernetes, and as tags on Azure.
- A unit test that runs an interceptor on a request, with no cloud.
Source: examples/extensions/resource-tags.
Where interceptors run
An interceptor runs inside a call one of Alien's controllers makes, before Alien signs it. It runs wherever those controllers run:
| Who drives | What the interceptors tag |
|---|---|
| The Alien manager, in a push deployment | Live resources the manager's controllers create |
| The Operator, in a pull deployment | Live resources the Operator's controllers create, and the Kubernetes objects it creates |
The setup CLI doesn't run interceptors, and refuses a release whose extensions ship them: set such a deployment up from its template, or let the manager or the Operator set it up.
A frozen resource that the CloudFormation or Terraform setup template creates goes through no Alien call. Tag it with a template modifier, as in the backups example.
In your stack
import * as alien from "@alienplatform/core"
import tags from "./tags/generated"
const db = new alien.Postgres("db").build()
export default new alien.Stack("my-app")
.extensions([tags])
.add(db, "live")
.build()AWS
Each cloud API takes tags in its own field, so each interceptor names the calls it tags and the field it writes. A query API such as RDS carries tags as numbered form keys:
use alien_extension_sdk::prelude::*;
use crate::TAGS;
/// Adds the tags to every Aurora cluster Alien creates.
pub struct RdsTags;
impl AwsInterceptor for RdsTags {
fn matches(&self, call: &AwsCall) -> bool {
call.service() == "rds" && call.operation() == "CreateDBCluster"
}
fn modify_request(&self, request: &mut AwsRequest) -> Result<()> {
for (key, value) in TAGS {
request.push_member("Tags.Tag", [("Key", *key), ("Value", *value)]);
}
Ok(())
}
}Alien already sends its own tags on CreateDBCluster, as Tags.Tag.1.Key and up. push_member adds each new member at the next free index, so Alien's tags stay. set, param, and remove read and write single form keys.
A JSON API such as DynamoDB carries tags in the request body. body_mut returns it as a serde_json::Value:
/// Adds the tags to every DynamoDB table Alien creates.
pub struct DynamoDbTags;
impl AwsInterceptor for DynamoDbTags {
fn matches(&self, call: &AwsCall) -> bool {
call.service() == "dynamodb" && call.operation() == "CreateTable"
}
fn modify_request(&self, request: &mut AwsRequest) -> Result<()> {
let body = request.body_mut()?;
let mut tags = body["Tags"].as_array().cloned().unwrap_or_default();
tags.extend(TAGS.iter().map(|(key, value)| json!({ "Key": key, "Value": value })));
body["Tags"] = Value::Array(tags);
Ok(())
}
}On a query API, body_mut returns an error, because the body is a form.
Google Cloud, Azure, and Kubernetes
The other clouds take a JSON body, so each interceptor writes a map:
/// Cloud SQL keeps labels under settings.userLabels.
pub struct CloudSqlLabels;
impl GcpInterceptor for CloudSqlLabels {
fn matches(&self, call: &GcpCall) -> bool {
call.service() == "cloudsql" && call.matches_route("POST", "projects/*/instances")
}
fn modify_request(&self, request: &mut GcpRequest) -> Result<()> {
let labels = &mut request.body_mut()["settings"]["userLabels"];
for (key, value) in TAGS {
labels[*key] = json!(value);
}
Ok(())
}
}The keys are lowercase letters, which every cloud and Kubernetes accept as a tag or label key. Google Cloud labels don't allow : or /.
Register the interceptors
use alien_extension_sdk::prelude::*;
mod aws;
mod azure;
mod gcp;
mod kubernetes;
/// The tags every interceptor adds.
pub const TAGS: &[(&str, &str)] = &[("team", "payments"), ("owner", "acme")];
fn register(ext: &mut Extension) {
ext.aws_interceptor(aws::RdsTags);
ext.aws_interceptor(aws::DynamoDbTags);
ext.gcp_interceptor(gcp::CloudSqlLabels);
ext.azure_interceptor(azure::AzureTags);
ext.kubernetes_interceptor(kubernetes::KubernetesLabels);
}
alien_extension_sdk::export!("tags", register);Test an interceptor
An interceptor is a function from a request to a request, so a unit test needs no cloud:
#[test]
fn adds_tags_after_alien_tags() {
let mut request = AwsRequest::new(
"rds",
"CreateDBCluster",
json!({ "DBClusterIdentifier": "db", "Tags.Tag.1.Key": "deployment", "Tags.Tag.1.Value": "stack" }),
);
RdsTags.modify_request(&mut request).unwrap();
assert_eq!(request.param("Tags.Tag.1.Key"), Some("deployment"));
assert_eq!(request.param("Tags.Tag.2.Key"), Some("team"));
assert_eq!(request.param("Tags.Tag.3.Value"), Some("acme"));
}alien extensions test runs it natively and again inside the WebAssembly module Alien loads.
Try it
cd tags
alien extensions test
alien extensions build
cd ..
alien releaseThe release pins the extension's digest, and every deployment on it loads the interceptors. After the manager's controller creates the live database on AWS, read its tags:
aws rds describe-db-clusters --db-cluster-identifier <cluster-id> --query 'DBClusters[0].TagList'The list holds Alien's tags and team and owner.
What it covers
The interceptors tag the calls they match, and nothing else:
- Listed calls only. Each API names tags in its own field, so add a
matchesarm and the right field for each create call you want tagged. - New resources only. A create-only match never reaches a resource that already exists. Resources created before the release keep the tags they have.
- Azure Resource Manager only. Key Vault, Service Bus, and Table Storage data calls are not intercepted.
- Controller calls only. Alien's reads outside its controllers, such as preflight checks, are not intercepted.
- Text bodies only. Calls with a binary or streamed body, such as S3
PutObject, are not offered to interceptors. If an interceptor would match one, the call fails with the extension's name.
If an interceptor returns an error, the call fails with the extension's name. Alien tries the step again only when the hook hit its time limit or its error says another try may pass.
Next
- Interceptors: the call and request views per cloud, and what an interceptor may change
- IAM database authentication: an interceptor and modifiers that set one value on every path
- Testing: test hooks natively and in the module
Longer backups
An extension with a CloudFormation and a Terraform modifier that raises Postgres backup retention to 30 on AWS, Google Cloud, and Azure.
Budget alert
A small custom resource that adds an AWS budget alert at initial setup, with a CloudFormation emitter, a Terraform emitter, and an importer.