COS Backend
The COS backend stores state in Tencent Cloud Object Storage (COS) with support for encryption and object tagging.Implementation
Location:/internal/backend/remote-state/cos/backend.go
Use Cases
- Managing Tencent Cloud infrastructure
- Teams using Tencent Cloud services
- China region compliance requirements
- Multi-cloud with Tencent Cloud component
Basic Configuration
Required Configuration
bucket
- Type: String
- Required: Yes
- Description: The name of the COS bucket
region
- Type: String
- Required: Yes
- Environment Variable:
TENCENTCLOUD_REGION - Description: The region of the COS bucket
ap-guangzhou- Guangzhouap-shanghai- Shanghaiap-beijing- Beijingap-chengdu- Chengduap-singapore- Singapore
Optional Configuration
prefix
- Type: String
- Optional: Yes
- Default:
"" - Description: The directory for saving the state file in the bucket
/ or ./
key
- Type: String
- Optional: Yes
- Default:
"terraform.tfstate" - Description: The path for saving the state file in the bucket
/
Authentication
The COS backend supports multiple authentication methods:1. Secret ID and Secret Key
TENCENTCLOUD_SECRET_IDTENCENTCLOUD_SECRET_KEY
2. Security Token (Temporary Credentials)
TENCENTCLOUD_SECURITY_TOKEN
3. CAM Role (CVM Instance Role)
TENCENTCLOUD_CAM_ROLE_NAME
Used when running Terraform from a CVM instance with an attached CAM role.
4. Shared Credentials File
~/.tccli
Environment Variables:
TENCENTCLOUD_PROFILETENCENTCLOUD_SHARED_CREDENTIALS_DIR
5. Assume Role
- role_arn (Required) - ARN of the CAM role to assume
- session_name (Required) - Session name for the role
- session_duration (Required) - Duration in seconds (0-43200, default: 7200)
- policy (Optional) - Policy to restrict permissions
- external_id (Optional) - External ID for cross-account access (2-128 characters)
TENCENTCLOUD_ASSUME_ROLE_ARNTENCENTCLOUD_ASSUME_ROLE_SESSION_NAMETENCENTCLOUD_ASSUME_ROLE_SESSION_DURATIONTENCENTCLOUD_ASSUME_ROLE_EXTERNAL_ID
Encryption
Server-Side Encryption
- Type: Boolean
- Optional: Yes
- Default:
true - Description: Enable server-side encryption of the state file
Access Control
ACL (Access Control List)
- Type: String
- Optional: Yes
- Default:
"private" - Valid Values:
private,public-read - Description: Object ACL to be applied to the state file
Custom Endpoints
endpoint
- Type: String
- Optional: Yes
- Environment Variable:
TENCENTCLOUD_ENDPOINT - Description: Custom endpoint for the COS API
http(s)://cos-internal.{Region}.tencentcos.cn
domain
- Type: String
- Optional: Yes
- Default:
"tencentcloudapi.com" - Environment Variable:
TENCENTCLOUD_DOMAIN - Description: The root domain of the API request
Acceleration
accelerate
- Type: Boolean
- Optional: Yes
- Default:
false - Description: Enable global acceleration for the COS bucket
{bucket}.cos.accelerate.myqcloud.com
Workspaces
The COS backend supports workspaces. State files are stored at:Configuration Options Summary
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
bucket | string | Yes | - | COS bucket name |
region | string | Yes | - | Tencent Cloud region |
prefix | string | No | "" | State file directory prefix |
key | string | No | terraform.tfstate | State file name |
secret_id | string | No | - | Secret ID |
secret_key | string | No | - | Secret key |
security_token | string | No | - | Security token |
cam_role_name | string | No | - | CAM role name |
profile | string | No | - | Profile name |
shared_credentials_dir | string | No | ~/.tccli | Credentials directory |
encrypt | bool | No | true | Enable encryption |
acl | string | No | private | Object ACL |
endpoint | string | No | - | COS endpoint |
domain | string | No | tencentcloudapi.com | API domain |
accelerate | bool | No | false | Enable acceleration |
Example: Production with Encryption and Assume Role
Example: Using CAM Role on CVM
Example: With Internal Endpoint and Acceleration
CAM Policy Requirements
Minimum permissions for the COS bucket:State Locking
The COS backend does not provide built-in state locking. For state locking, consider:- Using a different backend that supports locking
- Implementing external locking with Tencent Cloud services
- Using Terraform Cloud/Enterprise
Best Practices
- Enable encryption for production state files
- Use CAM roles when running on CVM instances
- Separate buckets for different environments
- Use internal endpoints when running within Tencent Cloud VPC
- Enable versioning on the COS bucket for state history
- Restrict bucket access with appropriate ACLs and CAM policies
- Use assume role for cross-account access
- Enable acceleration for globally distributed teams (but not with internal endpoints)
- Regular backups of state files
- Monitor bucket costs and access patterns