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
- 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 applyThe terraform apply command executes the planned changes and triggers the certificate creation process.
| # | 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 |
| # | 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 |
|
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:
|
|
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:
|
| 21 | key_download_password | String | No | The password used to download the private key. | password |
| 22 | download_password_protected_key | Boolean | No |
|
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 |
| # | 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:
|
|||||
| 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:
|
| 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:
|
| 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 When set to |
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.
- The certificate content is stored in the
certificate_contentoutput attribute in the Terraform state. - The private key content, if downloaded, is stored in the
key_contentoutput attribute in the Terraform state. - The
certificate_download_pathandkey_download_pathparameters are optional. Files are not written to disk unless you provide these paths.
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:
- Requests a new client secret from AppViewX.
- Writes the new client secret to the specified .tfvars file, updating the
appviewx_client_secretvalue. - 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.
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 applySet 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>"
Sample provider and resource .tf files
terraform {
required_providers {
appviewx = {
source = "AppViewX/appviewx"
version = "1.1.2"
}
}
}
- Create the certificate in sync mode, which creates and downloads the
certificate in a single
appviewx_create_certificateresource.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 } - Create a certificate in async mode, where certificate creation is triggered
first, followed by a delayed certificate download using the
appviewx_download_certificateresource.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 } - 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_certificateresource, 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 } - 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
- Verify that the provider version is correct.
- Confirm that the fields in the
.tffiles use the exact field names and types. - Review the console output after running the
terraform applycommand. - Set the logging level by running
export TF_LOG=DEBUGorexport TF_LOG=TRACE. - 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.
- For internal server errors: Verify the input parameters and review the AppViewX server logs for failures.
- 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_pathin 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. - For invalid file path errors: The provider returns a clear error message
when a path specified in
certificate_download_pathorkey_download_pathdoes not exist or is inaccessible. Verify that the path exists and that Terraform has write permission to it.
Enhancements implemented
- The group name is included in the payload when creating the certificate.
- Option to download the certificate and private key separately.
- Support for service account credentials alongside username and password.
- Support for pulling updates directly from the AppViewX Terraform registry.
- Improved logging to display AppViewX error messages in Terraform output.
- Support for specifying certificate validity in days, months, and years.
- Support for storing certificate and private key content in the Terraform state
file using the
store_certificate_in_stateparameter. - Support for automated service account client secret rotation. When
appviewx_tfvars_file_pathis configured, the provider writes the new client secret to the specified .tfvars file after rotation. - 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=trueand within theappviewx_download_certificateresource. - The
certificate_download_passwordfield is required only for PFX, P12, and JKS formats. - The
certificate_chain_requiredfield 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_certificateandappviewx_download_certificate. All other fields remain unchanged. - When
store_certificate_in_stateis enabled, certificate and private key content are present in the Terraform state file. Secure your state backend appropriately. - The
appviewx_tfvars_file_pathparameter 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.
