简体   繁体   中英

How to set global tags in Pulumi Azure Native

In a stack specific settings file (ie Pulumi.dev.yaml ), if location is set (ie azure-native:location ) then resource group location is set automatically and location for resources is derived from resource group location. Now I am trying to apply common tag for all resources ie CreatedBy: Pulumi . Is there any way to set common/global tags, similar to azure-native:location in settings file ( Pulumi.dev.yaml ) ?

Expected: both location and tags will be set from Pulumi.dev.yaml

config:
  azure-native:location: japaneast
  azure-native:tags:
    CreatedBy: Pulumi
var mainRgArgs = config.RequireObject<JsonElement>(KEY_RESOURCE_GROUP_ARGS);
var mainRgName = mainRgArgs.GetProperty(RESOURCE_GROUP_NAME).GetString()!;

var mainRg = new ResourceGroup(RESOURCE_GROUP_MAIN, new ResourceGroupArgs
{
    ResourceGroupName = mainRgName
    //Location = 
    //Tags = 
});

It isn't possible to set the tags automatically, because tags aren't a required API property.

The reason location is a provider argument is because every resource requires a location when it's created. That isn't true for tags.

However, it is possible to automatically add tags to resources that are taggable (which isn't all resources) using a transformation

Transformations allow you to inject properties into every resource, regardless of whether you've set that value on your resource explicitly. You will however have to set a list of taggable resources, because not every Azure resource is taggable.

A function which will register tags on resources will look something like this:

export function registerAutoTags(autoTags: Record<string, string>): void {
    pulumi.runtime.registerStackTransformation((args) => {
        if (isTaggable(args.type)) {
            args.props["tags"] = { ...args.props["tags"], ...autoTags };
            return { props: args.props, opts: args.opts };
        }
        return undefined;
    });
}

and then you can use those tags by calling the function:

registerAutoTags({
    "user:Project": pulumi.getProject(),
    "user:Stack": pulumi.getStack(),
    "user:Cost Center": config.require("costCenter"),
});

There's more information on this (albeit for AWS, not Azure) here . You can find a list of Azure resources that are support tags here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM