Managing Certificates Using AppViewX Terraform Provider

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool that allows you to define, provision, and manage infrastructure using human-readable configuration files.

Terraform configurations can be versioned, reused, and shared, enabling teams to manage infrastructure consistently throughout its lifecycle.

Terraform can manage both cloud and on-premises resources, including low-level infrastructure such as compute, storage, and networking, as well as higher-level resources such as DNS records and SaaS services.

How does Terraform work?

Terraform manages resources through APIs exposed by cloud platforms and other services.

Terraform uses providers to communicate with these platforms and services. A provider acts as an interface between Terraform and the target system's API, allowing Terraform to create, read, update, and delete resources.

There are thousands of providers available through the Terraform Registry, including providers maintained by HashiCorp, third-party organizations, and the community.

AppViewX Terraform Provider is a third-party Terraform provider that enables Terraform to interact with AppViewX and manage supported AppViewX resources through Terraform configurations.

Terraform Core Workflow

The core Terraform workflow consists of three stages:

  • Write: Define the desired infrastructure in Terraform configuration files. You can define resources across multiple cloud providers and services. For example, you can configure an application to run on virtual machines within a Virtual Private Cloud (VPC), along with security groups and a load balancer.
  • Plan: Terraform creates an execution plan that shows the changes it intends to make such as which resources will be created, updated, or destroyed based on the current infrastructure state and your Terraform configuration.
  • Apply: After reviewing the plan, Terraform executes the proposed changes in the appropriate order, taking resource dependencies into account.

What does the AppViewX Terraform Provider do?

  • Creates certificates using the provided certificate/CSR parameters through AppViewX.
  • Downloads certificates in supported formats, along with the certificate chain/trust store certificates when required.
  • Downloads private keys from AppViewX in either password-protected format or plain .pem format, based on the configured options.
  • Optionally stores certificate and private key content in the Terraform state file when store_certificate_in_state is set to true. When enabled, the certificate and private key content can be consumed by downstream Terraform resources without requiring the certificate and key to be stored as local files.

Where is the AppViewX Terraform Provider hosted?

  • The latest version of the AppViewX Terraform Provider is 1.1.2, hosted in the HashiCorp Terraform Registry.
  • The open-source GitHub repository is synced with the Terraform Registry.

Supported authentication models

  • AppViewX username and password credentials
  • AppViewX service account credentials

Supported APIs and details

The following APIs and operations are used by the AppViewX Terraform Provider to create and download certificates:
  • Authenticate using username and password credentials and obtain a session ID for subsequent API calls.
  • Authenticate using service account credentials and obtain an access token for subsequent API calls.
  • Create a certificate using the CSR and certificate parameters defined in the Terraform configuration (.tf file).
  • Certificate creation uses the AppViewX Visual Workflow process. Disabling the approval level in the applicable policy is recommended to allow automated certificate creation.
  • Download the end-entity certificate in supported formats such as .pem, .crt, .cer, .der, .p12, .pfx, and .jks.
    • Download trusted certificates along with .p12, .pfx, and .jks certificate formats when required.
  • Download the private key in either password-protected format or plain .pem format.
  • Convert a password-protected private key to an unencrypted private key using OpenSSL:
    openssl pkey -in <password-protected-private-key-file> \ 
      -out <plain-private-key-file> \ 
      -passin pass:<private-key-password>

How to use the AppViewX Terraform Provider

  • Install and configure Terraform on the local machine where you intend to use the provider.
  • Configure the AppViewX Terraform provider in your Terraform configuration.
  • Create a .tf configuration file with the required certificate parameters.
  • Initialize the Terraform working directory:
    terraform init
  • Validate the Terraform configuration:
    terraform validate
  • Review the execution plan:
    terraform plan
  • Apply the Terraform configuration:
    terraform apply

    The terraform apply command executes the planned changes and triggers the certificate creation process.

Provider .tf file parameters
# Field name Field type Mandatory Description Sample
1 appviewx_username String No The username of the AppViewX user for signing in and performing API calls. test_user
2 appviewx_password String No The password of the AppViewX user for signing in and performing API calls. password
3 appviewx_client_id String No The client ID of the AppViewX service account for signing in and performing API calls. appviewx client id
4 appviewx_client_secret String No The client secret of the AppViewX service account for signing in and performing API calls. appviewx client secret
Note: Provide either the username and password, or the client ID and client secret.
5 appviewx_environment_is_https Boolean Yes Specifies whether the AppViewX API URL uses HTTPS. true
6 appviewx_environment_ip String Yes The FQDN or IP address of the AppViewX instance for making API calls. appviewx.example.com
7 appviewx_environment_port String Yes The port number used to construct the AppViewX API URL. 31443
8 appviewx_tfvars_file_path String No The absolute path to the .tfvars file where a rotated client secret is automatically written. When this parameter is configured, the provider updates the file with the new client secret after rotation, and the current operation continues without requiring a re-run. If this parameter is not configured, the provider displays the new credentials in the console output and provides instructions for updating them manually. /home/user/Terraform-Demo/terraform-dev.auto.tfvars
appviewx_create_certificate resource .tf file parameters
# Field name Field type Mandatory Description Possible values / Examples
1 common_name String No The common name (CN) CSR parameter. sample.test.com
2 dns_names List of String No A list of DNS subject alternative names (SANs) for the CSR parameter. ["sample.test.com", "sample12.test.com"]
3 hash_function String No The hash function CSR parameter. Possible values: SHA256, SHA160, SHA224, SHA512, SHA384
Note: Values may vary depending on the CA.
4 key_type String No The key type CSR parameter. Possible values: RSA, DSA, EC
Note: Values may vary depending on the CA.
5 bit_length String No The bit length CSR parameter. Possible values: 1024, 2048, 3072, 4096, 7680, 8192
Note: Values may vary depending on the CA.
6 certificate_authority String No The name of the certificate authority to use for certificate enrollment. Microsoft Enterprise
7 ca_setting_name String No The CA setting name configured in AppViewX. msca
8 certificate_type String No The certificate type for enrollment. SSL Certificate
9 custom_fields Object No AppViewX certificate attributes that appear under the Certificate Attributes column in the certificate inventory. {"request_by": "terraform"}
10 vendor_specific_fields Object No CA-specific values that are passed under Vendor Specific fields during certificate enrollment for the selected CA. {"templateName": "WebServer"}
11 validity_days Integer No The certificate validity period in days. Possible values: 1 to 365
12 validity_unit String No The unit of validity for the certificate. Possible values: days, months, years
13 validity_unit_value Integer No The numeric value of the certificate validity period, used with validity_unit. 12
14 certificate_group_name String No The certificate group in the AppViewX certificate inventory under which the certificate is placed. test_group1
15 is_sync Boolean No
  • When set to true, the certificate is created and downloaded in a single API call (synchronous mode).
  • When set to false, certificate creation is triggered asynchronously. The provider returns a resource ID that can be used to download the certificate after it is issued.
true
16 certificate_download_path String No The path where the certificate file is downloaded. Optional when store_certificate_in_state is set to true. Possible scenarios:
  • /home/sample/testcert
  • /home/sample/
  • testcert
Note: Ensure you provide a valid path for downloading the certificate and private key.
17 certificate_download_format String No The format in which the certificate is downloaded. Possible values: PEM, CRT, DER, CER, P12, PFX, JKS
18 certificate_download_password String Yes, for P12, PFX, and JKS formats. The password used to protect the certificate when downloading in P12, PFX, or JKS format. password
19 certificate_chain_required Boolean No When set to true, the trust store certificates are included with the end certificate during download.
Note: This parameter applies only to P12, PFX, and JKS certificate formats.
true
20 key_download_path String No The path where the private key file is downloaded. Optional when store_certificate_in_state is set to true. Possible scenarios:
  • /home/sample/testcert
  • /home/sample/
  • testcert
21 key_download_password String No The password used to download the private key. password
22 download_password_protected_key Boolean No
  • When set to true, the private key is downloaded in encrypted format.
  • When set to false, OpenSSL is used to convert the key to plain format before saving it to the specified path.
false
23 store_certificate_in_state Boolean No When set to true, the certificate content and private key content are stored in the Terraform state file after a successful download. This enables downstream Terraform resources to reference the certificate and key without requiring file-based downloads. The certificate_download_path and key_download_path parameters are optional when this is enabled.
CAUTION: Enabling this option stores sensitive certificate and private key content in the Terraform state file. Secure your state backend by using encryption and access controls. Do not commit state files to version control.
true
appviewx_download_certificate resource .tf file parameters
# Field name Field type Mandatory Description Sample
1 resource_id String No The resource ID returned by the appviewx_create_certificate resource. Used to download the certificate after it is issued. 6128381dq231sw1ww
Note: When is_sync=false, use this resource together with appviewx_create_certificate as shown:

 ....
 ....
}
resource "time_sleep" "wait" {
           depends_on = [appviewx_create_certificate.createcert]
           create_duration = "10s"
}
resource "appviewx_download_certificate" "downloadcert"{
    depends_on = [time_sleep.wait]
    resource_id=appviewx_create_certificate.createcert.resource_id
            .....
}
2 common_name String No The common name of the certificate to download from AppViewX. test.sample.com
3 serial_number String No The serial number of the certificate to download from AppViewX. 12:QQ:23:12:34:45:45:23
Note: Provide either the username and password, or the client ID and client secret.
4 certificate_download_path String No The path where the certificate file is downloaded. Optional when store_certificate_in_state is set to true. Possible scenarios:
  • /home/sample/testcert
  • /home/sample/
  • testcert
5 certificate_download_format String No The format in which the certificate is downloaded.

Possible values: PEM, CRT, DER, CER, P12, PFX, JKS

PEM
6 certificate_download_password String Yes, for P12, PFX, and JKS formats. The password used to protect the certificate when downloading in P12, PFX, or JKS format. password
7 certificate_chain_required Boolean No When set to true, the trust store certificates are included with the end certificate during download.
Note: This parameter applies only to P12, PFX, and JKS certificate formats.
true
8 key_download_path String No The path where the private key file is downloaded. Optional when store_certificate_in_state is set to true. Possible scenarios:
  • /home/sample/testcert
  • /home/sample/
  • testcert
9 key_download_password String No The password used to download the private key. password
10 download_password_protected_key Boolean No

When set to true, the private key is downloaded in encrypted format.

When set to false, OpenSSL is used to convert the key to plain format before saving it to the specified path.

false
11 store_certificate_in_state Boolean No When set to true, the certificate content and private key content are stored in the Terraform state file after a successful download. The certificate_download_path and key_download_path parameters are optional when this is enabled.
CAUTION: Enabling this option stores sensitive certificate and private key content in the Terraform state file. Secure your state backend by using encryption and access controls. Do not commit state files to version control.
true

Store certificate and private key content in Terraform state

The AppViewX Terraform Provider can store certificate and private key content directly in the Terraform state file. This removes the requirement to write these artifacts to a local file path, and enables downstream Terraform resources to reference the certificate and key programmatically.

To enable this behavior, set store_certificate_in_state = true in the appviewx_download_certificate resource.

When this parameter is enabled:
  • The certificate content is stored in the certificate_content output attribute in the Terraform state.
  • The private key content, if downloaded, is stored in the key_content output attribute in the Terraform state.
  • The certificate_download_path and key_download_path parameters are optional. Files are not written to disk unless you provide these paths.
CAUTION: Enabling store_certificate_in_state stores sensitive certificate and private key content in the Terraform state file. If you use a remote state backend such as Amazon S3, Google Cloud Storage, or Terraform Cloud, ensure the backend is encrypted and access-controlled. Do not commit state files to version control.

Example: Download certificate and store in state

resource "appviewx_download_certificate" "downloadcert" {
  common_name                       = "example.certplus.in"
  serial_number                     = "6F:48:A5:01:6B:20:C8:DD:C3:71:C7:32:BA:A7:2A:1D"
  certificate_download_format       = "PEM"
  certificate_chain_required        = true
  key_download_password             = "AppViewX@123"
  download_password_protected_key   = false
  store_certificate_in_state        = true
}

Automate service account client secret rotation

The AppViewX Terraform Provider can automatically rotate the service account client secret when it expires and write the new secret back to a .tfvars file. This removes the need for manual intervention when a client secret expires during Terraform operations.

To enable automatic rotation with file persistence, configure the appviewx_tfvars_file_path parameter in the provider block.

When the provider detects that the client secret has expired, it:

  1. Requests a new client secret from AppViewX.
  2. Writes the new client secret to the specified .tfvars file, updating the appviewx_client_secret value.
  3. Continues the current Terraform operation using the new secret without requiring a re-run.

If appviewx_tfvars_file_path is not configured, the provider still rotates the secret automatically, but displays the new client ID and secret in the console output with instructions for persisting them. The provider then retries the current operation using the new secret.

The console output resembles the following:
codeblock_rotation_console_output
=== NEW CLIENT SECRET REGENERATED ===
Client ID:     <client-id-value>
Client Secret: <new-client-secret-value>

Please configure one of the following:
1. Add appviewx_tfvars_file_path to your provider block
2. Update appviewx_client_secret in your .tf file
3. Update APPVIEWX_TERRAFORM_CLIENT_SECRET environment variable

Then re-run: terraform apply

Set up variables for secure credential handling

Declare sensitive variables in a variables.tf file so that the client ID and secret are never hardcoded in your .tf files.

variables.tf

variable "appviewx_client_id" {
  description = "AppViewX client ID"
  type        = string
  sensitive   = true
}

variable "appviewx_client_secret" {
  description = "AppViewX client secret"
  type        = string
  sensitive   = true
}

Provider block referencing variables

provider "appviewx" {
  appviewx_client_id            = var.appviewx_client_id
  appviewx_client_secret        = var.appviewx_client_secret
  appviewx_environment_is_https = true
  appviewx_environment_ip       = "<AppViewX-FQDN-or-IP>"
  appviewx_environment_port     = "31443"
  appviewx_tfvars_file_path     = "/home/user/Terraform-Demo/terraform-dev.auto.tfvars"
}

terraform-dev.auto.tfvars (auto-updated on rotation)

appviewx_client_id     = "<your-client-id>"
appviewx_client_secret = "<your-client-secret>"
Note: The .tfvars file that contains your client secret should not be committed to version control. Add it to your .gitignore file. Terraform automatically loads files named *.auto.tfvars from the working directory.

Sample provider and resource .tf files

Sample version.tf file
terraform {
  required_providers {
    appviewx = {
      source  = "AppViewX/appviewx"
      version = "1.1.2"
    }
  }
}
  1. Create the certificate in sync mode, which creates and downloads the certificate in a single appviewx_create_certificate resource.
    
    resource "appviewx_create_certificate" "createcert"{
        common_name="sample.test.com"
        hash_function="SHA256"
        key_type="RSA"
        bit_length="2048"
        certificate_authority="Microsoft Enterprise"
        ca_setting_name="msca"
        #certificate_type="SSL Certificate"
        #dns_names=["test.com","test12.com"]
        custom_fields={"request_by":"terraform"}
        vendor_specific_fields={"templateName":"WebServer"}
        validity_unit=365
        validity_unit_value="years"
        certificate_group_name="cert_group"
    
        is_sync=true
        certificate_download_path="/home/sample/test/"
        certificate_download_format="P12"
        certificate_download_password="test"
        certificate_chain_required=true
    
        key_download_path="/test/testcert123"
        key_download_password="password"
        download_password_protected_key=false
    }
    
  2. Create a certificate in async mode, where certificate creation is triggered first, followed by a delayed certificate download using the appviewx_download_certificate resource.
    
    resource "appviewx_create_certificate" "createcert"{
        common_name="test.sample.com"
        hash_function="SHA256"
        key_type="RSA"
        bit_length="2048"
        certificate_authority="Microsoft Enterprise"
        ca_setting_name="msca"
        #certificate_type="SSL Certificate"
        #dns_names=["test.com","test123.com"]
        custom_fields={"request_by":"terraform"}
        vendor_specific_fields={"templateName":"WebServer"}
        validity_unit=365
        validity_unit_value="years"
        certificate_group_name="cert_group"
        is_sync=false
    }
    
    resource "time_sleep" "wait" {
      depends_on      = [appviewx_create_certificate.createcert]
      create_duration = "10s"
    }
    
    resource "appviewx_download_certificate" "downloadcert"{
        depends_on                    = [time_sleep.wait]
        resource_id                   = appviewx_create_certificate.createcert.resource_id
        certificate_download_path     = "/Downloads/testcert123"
        certificate_download_format   = "P12"
        certificate_download_password = "test"
        certificate_chain_required    = true
        key_download_path             = "/test/testcert123"
        key_download_password         = "password"
        download_password_protected_key = false
    }
    
  3. Create a certificate in async mode with deferred download. Certificate creation is triggered first, and you can download the certificate at any time using the appviewx_download_certificate resource, identified by common name and serial number or by resource ID.
    
    resource "appviewx_create_certificate" "createcert"{
        common_name="test.domain.com"
        hash_function="SHA256"
        key_type="RSA"
        bit_length="2048"
        certificate_authority="Microsoft Enterprise"
        ca_setting_name="msca"
        #certificate_type="SSL Certificate"
        #dns_names=["test.com","test123.com"]
        custom_fields={"request_by":"terraform"}
        vendor_specific_fields={"templateName":"WebServer"}
        validity_unit="years"
        validity_unit_value=2
        certificate_group_name="cert_group"
        is_sync=false
    }
    
    resource "appviewx_download_certificate" "downloadcert"{
        resource_id                     = "564647657564gfdfx3w"
        common_name                     = "test.domain.com"
        serial_number                   = "21:23:123..."
        certificate_download_path       = "testcert"
        certificate_download_format     = "P12"
        certificate_download_password   = "password"
        certificate_chain_required      = true
        key_download_path               = "/test/testcert123"
        key_download_password           = "password"
        download_password_protected_key = false
    }
    
  4. Download a certificate and store the content in Terraform state.
    
    resource "appviewx_download_certificate" "downloadcert" {
        common_name                     = "example.certplus.in"
        serial_number                   = "6F:48:A5:01:6B:20:C8:DD:C3:71:C7:32:BA:A7:2A:1D"
        certificate_download_format     = "PEM"
        certificate_chain_required      = true
        key_download_password           = "AppViewX@123"
        download_password_protected_key = false
        store_certificate_in_state      = true
    }
    

Troubleshooting

  1. Verify that the provider version is correct.
  2. Confirm that the fields in the .tf files use the exact field names and types.
  3. Review the console output after running the terraform apply command.
  4. Set the logging level by running export TF_LOG=DEBUG or export TF_LOG=TRACE.
  5. For authentication errors: Verify the AppViewX user account role and permissions in the AppViewX interface to confirm the user has access to create and download certificates.
  6. For internal server errors: Verify the input parameters and review the AppViewX server logs for failures.
  7. For expired client secret errors: The provider returns a clear error message when the service account client secret has expired. To resolve this, configure appviewx_tfvars_file_path in the provider block to enable automatic secret rotation, or manually update the client secret by following the instructions in the console output. For more information, see Automate service account client secret rotation.
  8. For invalid file path errors: The provider returns a clear error message when a path specified in certificate_download_path or key_download_path does not exist or is inaccessible. Verify that the path exists and that Terraform has write permission to it.

Enhancements implemented

  1. The group name is included in the payload when creating the certificate.
  2. Option to download the certificate and private key separately.
  3. Support for service account credentials alongside username and password.
  4. Support for pulling updates directly from the AppViewX Terraform registry.
  5. Improved logging to display AppViewX error messages in Terraform output.
  6. Support for specifying certificate validity in days, months, and years.
  7. Support for storing certificate and private key content in the Terraform state file using the store_certificate_in_state parameter.
  8. Support for automated service account client secret rotation. When appviewx_tfvars_file_path is configured, the provider writes the new client secret to the specified .tfvars file after rotation.
  9. Improved error messages for expired client secrets and invalid certificate or private key file paths. The provider now returns descriptive error messages instead of terminating unexpectedly.

Points to remember

  • Authenticate using either username and password, or client ID and client secret.
  • Downloading the private key is optional, but the AppViewX user account must have permission to download private keys.
  • Certificate download fields are required when is_sync=true and within the appviewx_download_certificate resource.
  • The certificate_download_password field is required only for PFX, P12, and JKS formats.
  • The certificate_chain_required field applies only to CRT, CER, CERT, PEM, and DER formats.
  • If you are using an earlier version of the provider, update your resource names to appviewx_create_certificate and appviewx_download_certificate. All other fields remain unchanged.
  • When store_certificate_in_state is enabled, certificate and private key content are present in the Terraform state file. Secure your state backend appropriately.
  • The appviewx_tfvars_file_path parameter must point to a writable file. Terraform must have write permission to this path for automatic client secret rotation to work.
  • Exclude the .tfvars file used for secret rotation from version control.