Deploy Bucket Static Website Hosting Configuration

Application Scenario

Object Storage Service (OBS) is a highly available, highly reliable, high-performance, secure, and low-cost object storage service provided by Huawei Cloud. OBS provides massive, secure, highly reliable, and low-cost data storage capabilities, supporting multiple storage types, including standard storage, infrequent access storage, archive storage, etc., meeting storage requirements for different business scenarios.

OBS bucket static website hosting is an important feature of the OBS service, used to configure OBS buckets as static website hosting services. Through static website hosting, enterprises can store static website files (HTML, CSS, JavaScript, images, etc.) in OBS buckets and access them directly through OBS-provided website endpoints. Static website hosting supports custom home pages and error pages, provides complete website access control policies, providing enterprises with simple, economical, and reliable static website hosting solutions. This best practice will introduce how to use Terraform to automatically deploy OBS bucket static website hosting configuration, including KMS key creation, OBS bucket configuration, website configuration, and access policy settings.

This best practice involves the following main resources and data sources:

Resources

Resource/Data Source Dependencies

huaweicloud_kms_key.test
    └── huaweicloud_obs_bucket.test
        ├── huaweicloud_obs_bucket_policy.test
        └── huaweicloud_obs_bucket_object.test

Operation Steps

1. Script Preparation

Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.

2. Create KMS Key

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a KMS key resource:

Parameter Description:

  • key_alias: Key alias, assigned by referencing the input variable key_alias

  • key_usage: Key usage, assigned by referencing the input variable key_usage

  • count: Conditional creation, creates when encryption is enabled and key ID is not specified

3. Define Local Variables

Add the following script to the TF file (e.g., main.tf) to define local variables for handling website configuration:

Parameter Description:

  • website_configurations: Website configuration map, must contain index and error page configurations

  • website_page_names: Extract file names for home page and error page

  • index_page: Home page configuration

  • error_page: Error page configuration

4. Create OBS Bucket

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an OBS bucket resource:

Parameter Description:

  • bucket: Bucket name, assigned by referencing the input variable bucket_name

  • storage_class: Storage class, assigned by referencing the input variable bucket_storage_class

  • acl: Access control list, assigned by referencing the input variable bucket_acl

  • encryption: Whether encryption is enabled, assigned by referencing the input variable bucket_encryption

  • sse_algorithm: Server-side encryption algorithm, uses kms algorithm when encryption is enabled

  • kms_key_id: KMS key ID, prioritizes using input variable, uses created KMS key if empty

  • force_destroy: Force destroy, assigned by referencing the input variable bucket_force_destroy

  • tags: Tags, merge bucket tags and website page file name tags

  • website: Website configuration, set home page and error page documents

  • provisioner: Local executor, used to create and delete website page files

5. Create OBS Bucket Policy

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an OBS bucket policy resource:

Parameter Description:

  • bucket: Bucket ID, assigned by referencing the OBS bucket resource (huaweicloud_obs_bucket.test) ID

  • policy: Bucket policy, allows all users to perform GetObject operations on bucket objects

6. Create OBS Bucket Object

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an OBS bucket object resource:

Parameter Description:

  • bucket: Bucket ID, assigned by referencing the OBS bucket resource (huaweicloud_obs_bucket.test) ID

  • key: Object key name, using website page file name

  • source: Source file path, using locally created website page file

7. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input during each execution.

Create a terraform.tfvars file in the working directory with the following example content:

Usage:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform apply, Terraform will automatically read the variable values from this file

In addition to using terraform.tfvars file, variable values can also be set in the following ways:

  1. Command line parameters: terraform apply -var="bucket_name=my-bucket" -var="key_alias=my-key"

  2. Environment variables: export TF_VAR_bucket_name=my-bucket

  3. Custom named variable files: terraform apply -var-file="custom.tfvars"

Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.

8. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create resources:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming the resource plan is correct, run terraform apply to start creating static website hosting configuration

  4. Run terraform show to view the created static website hosting configuration

Reference Information

Last updated