Skip to main content

Templates

The imagefactory_template resource is the core of your image automation. It defines the base OS, the components to apply, and the distribution settings for your images.

Basic Example

data "imagefactory_distribution" "ubuntu" {
name = "Ubuntu Server 18.04 LTS"
cloud_provider = "AWS"
}

data "imagefactory_system_component" "hardening" {
name = "Hardening level 1"
}

resource "imagefactory_template" "web_server" {
name = "ubuntu-web-server"
cloud_provider = "AWS"
distribution_id = data.imagefactory_distribution.ubuntu.id

config {
aws {
region = "eu-west-1"
}
build_components {
id = data.imagefactory_system_component.hardening.id
}
build_components {
id = imagefactory_custom_component.nginx.id
}
notifications {
type = "SNS"
uri = "arn:aws:sns:eu-west-1:123456789012:ImageBuildTopic"
}
tags {
key = "Environment"
value = "Production"
}
tags {
key = "Team"
value = "Platform"
}
cloud_account_ids = [imagefactory_aws_account.main.id]
}
}
note

All components, notifications, tags, and distribution targets are defined inside the config block, not at the top level of the resource.

Argument Reference

Top-Level Arguments

ArgumentTypeRequiredDescription
nameStringRequiredUnique name for the template.
cloud_providerStringRequiredTarget cloud: AWS, AZURE, GCP, EXOSCALE.
distribution_idStringRequiredID of the base OS distribution. Use the imagefactory_distribution data source to look it up.
descriptionStringOptionalA description of the template.
configBlockOptionalTemplate configuration — see below.

Config Block

All of the following are nested inside the config block:

ArgumentTypeDescription
awsBlockAWS-specific settings.
azureBlockAzure-specific settings.
gcpBlockGCP-specific settings.
exoscaleBlockExoscale-specific settings.
build_componentsBlock ListComponents to run during the build phase. Each block has an id field.
test_componentsBlock ListComponents to run during the test phase. Each block has an id field.
notificationsBlock ListNotification targets. Each block has type (SNS or WEB_HOOK) and uri.
tagsBlock ListImage tags. Each block has key and value.
cloud_account_idsList of StringIDs of cloud accounts to distribute the image to.
disable_cyclical_rebuildsBooleanDisable automatic rebuilds when the source image updates or security patches are available. Defaults to false.
scopeStringSet to "CHINA" for AWS China or Azure China templates.

AWS Config (config.aws)

ArgumentTypeDescription
regionStringTarget AWS region (e.g., eu-west-1).
ebs_volume_typeStringEBS volume type for the primary block device: gp2 or gp3. Defaults to the source image type.
kms_key_idStringAWS KMS key ID/ARN to encrypt the destination snapshot. The ImageFactoryMasterRole must have permission to use this key.
custom_image_nameStringCustom name for the resulting AMI.
additional_ebs_volumesBlock ListUp to 10 additional EBS volumes (see below).

additional_ebs_volumes block:

ArgumentTypeDescription
device_nameStringRequired. Linux: /dev/sd[b-z], Windows: xvd[b-z].
sizeNumberRequired. Volume size between 1 and 10 GB.
volume_typeStringOptional. gp2 or gp3. Defaults to gp2.

Azure Config (config.azure)

ArgumentTypeDescription
replica_regionsList of StringRegions to replicate the gallery image to.
vm_image_definitionBlockAzure Compute Gallery image definition (requires name, offer, sku).
exclude_from_latestBooleanExclude this version from the latest tag in the gallery.
eol_date_optionBooleanSet end-of-life date. Defaults to true.
trusted_launchBooleanEnable Trusted Launch for the image.
create_managed_imageBooleanCreate a legacy managed image in addition to the gallery image.
disable_vhd_cleanupBooleanKeep temporary VHD files after build. Defaults to false.
additional_data_disksBlock ListUp to 10 additional data disks. Each block has a size field (1–10 GB).
additional_signaturesBlock ListAdditional UEFI keys for boot loader validation. Each block has a variable_name field pointing to a customer variable storing the key.

GCP Config (config.gcp)

ArgumentTypeDescription
custom_image_nameStringCustom name for the GCP image. Must start with a lowercase letter, contain only lowercase letters, digits, and dashes, and be 3–45 characters long.

Exoscale Config (config.exoscale)

ArgumentTypeDescription
zoneStringRequired. Target Exoscale zone (e.g., de-fra-1).

Advanced Examples

Azure Template with VM Image Definition

resource "imagefactory_template" "azure_app" {
name = "azure-app-server"
cloud_provider = "AZURE"
distribution_id = data.imagefactory_distribution.ubuntu_azure.id

config {
azure {
exclude_from_latest = true
eol_date_option = true
replica_regions = ["westeurope", "northeurope"]
vm_image_definition {
name = "my-app-image"
offer = "ubuntu-22_04-lts"
sku = "v1"
}
}
cloud_account_ids = [imagefactory_azure_subscription.main.id]
notifications {
type = "WEB_HOOK"
uri = "https://hooks.slack.com/services/..."
}
tags {
key = "Environment"
value = "Production"
}
}
}

Azure Template with Additional Data Disks

resource "imagefactory_template" "azure_with_disks" {
name = "azure-with-data-disks"
cloud_provider = "AZURE"
distribution_id = data.imagefactory_distribution.ubuntu_azure.id

config {
azure {
additional_data_disks {
size = 1
}
}
}
}

Azure Template with UEFI Signatures

resource "imagefactory_variable" "uefi_key" {
name = "UEFI_KEY"
value = "MIIDQTCCAimgAwIBAgIQ..."
}

resource "imagefactory_template" "azure_signed" {
name = "azure-signed-image"
cloud_provider = "AZURE"
distribution_id = data.imagefactory_distribution.ubuntu_azure.id

config {
azure {
trusted_launch = true
additional_signatures {
variable_name = imagefactory_variable.uefi_key.name
}
}
}
}

AWS Template with KMS Encryption and EBS Volumes

resource "imagefactory_template" "aws_encrypted" {
name = "aws-encrypted-image"
cloud_provider = "AWS"
distribution_id = data.imagefactory_distribution.ubuntu.id

config {
aws {
region = "eu-west-1"
ebs_volume_type = "gp3"
kms_key_id = "arn:aws:kms:eu-west-1:123456789012:key/example-key-id"
additional_ebs_volumes {
size = 1
device_name = "/dev/sdb"
}
}
build_components {
id = data.imagefactory_system_component.hardening.id
}
}
}

Template with Notifications

resource "imagefactory_template" "notified" {
name = "notified-template"
cloud_provider = "AWS"
distribution_id = data.imagefactory_distribution.amazon_linux.id

config {
aws {
region = "us-east-1"
}
notifications {
type = "WEB_HOOK"
uri = "https://hooks.slack.com/services/..."
}
notifications {
type = "SNS"
uri = "arn:aws:sns:us-east-1:123456789012:my-topic"
}
}
}

Exoscale Template

data "imagefactory_distribution" "ubuntu22_exoscale" {
name = "Ubuntu Server 22.04 LTS"
cloud_provider = "EXOSCALE"
}

resource "imagefactory_template" "exoscale" {
name = "ubuntu-2204-exoscale"
cloud_provider = "EXOSCALE"
distribution_id = data.imagefactory_distribution.ubuntu22_exoscale.id

config {
exoscale {
zone = "de-fra-1"
}
notifications {
type = "WEB_HOOK"
uri = "https://hooks.example.com/imagefactory"
}
}
}

GCP Template

data "imagefactory_distribution" "ubuntu22_gcp" {
name = "Ubuntu Server 22.04 LTS"
cloud_provider = "GCP"
}

resource "imagefactory_template" "gcp" {
name = "ubuntu-2204-gcp"
cloud_provider = "GCP"
distribution_id = data.imagefactory_distribution.ubuntu22_gcp.id

config {
gcp {
custom_image_name = "ubuntu-22-04-prod-1-3"
}
notifications {
type = "WEB_HOOK"
uri = "https://hooks.example.com/imagefactory"
}
}
}

AWS China Template

resource "imagefactory_template" "aws_china" {
name = "ubuntu-china"
cloud_provider = "AWS"
distribution_id = data.imagefactory_distribution.ubuntu.id

config {
scope = "CHINA"
}
}

Import

terraform import imagefactory_template.main <TEMPLATE_ID>