API secrets in Tencent Cloud CVM user data

Keep long-lived API keys out of instance user data.

Description

Give a Tencent Cloud CVM instance a role through cam_role_name when its application needs API access. Keys in user_data or user_data_raw can appear in Terraform state or initialization logs, even when Base64-encoded.

Potential impact

An exposed long-lived key can authorize Tencent Cloud operations within its permissions until it is disabled.

Remediation

Create a CAM role limited to the required operations. Remove static keys from the startup script and workload environment, then verify that the application can run those operations with the role’s temporary credentials. Revoke exposed keys.

Examples

The first example uses placeholder keys. The second attaches the existing cvm-access-role and retains a startup script without secrets. Supply the remaining instance settings separately.

Before

hcl
resource "tencentcloud_instance" "example" {
  instance_name = "cvm-postpaid"

  user_data = base64encode("#!/bin/sh\nexport TENCENTCLOUD_SECRET_ID=example-id; export TENCENTCLOUD_SECRET_KEY=example-key")
}

After

hcl
resource "tencentcloud_instance" "example" {
  instance_name = "cvm-postpaid"
  cam_role_name = "cvm-access-role"

  user_data = base64encode("#!/bin/sh\necho instance bootstrap")
}

References