Configuring Static Website Hosting
Application Scenario
OBS allows static websites to be hosted on buckets and supports index page, error page display, and page redirection. You can upload the content files of the static website to your bucket on OBS and configure a read permission to anonymous users for these files, and then configure the static website hosting mode for your bucket to host your static websites on OBS.
Related Resources
Procedure
- Create an OBS bucket and configure static website hosting.
- Create the main.tf file, enter the following information, and save the file:
resource "huaweicloud_obs_bucket" "mywebsite" { bucket = "mywebsite" website { index_document = "index.html" error_document = "error.html" } }
- Run terraform init to initialize the environment.
- Run terraform plan to view resources.
- After you confirm that the resource information is correct, run terraform apply to start OBS bucket creation.
- Run terraform show to view the created OBS bucket.
- Create the main.tf file, enter the following information, and save the file:
- Configure a bucket policy to allow anonymous users to access objects in the bucket.
Add the following information to the main.tf file:
# Grant the Read-Only permission to anonymous users. resource "huaweicloud_obs_bucket_policy" "policy" { bucket = huaweicloud_obs_bucket.mywebsite.bucket policy = <<POLICY { "Statement": [ { "Sid": "AddPerm", "Effect": "Allow", "Principal": {"ID": "*"}, "Action": ["GetObject"], "Resource": "mywebsite/*" } ] } POLICY }
- Upload static website files.
- Edit the index.html and error.html files in the current directory.
- Add the following information to the main.tf file and upload the files to the OBS bucket:
# put index.html resource "huaweicloud_obs_bucket_object" "index" { bucket = huaweicloud_obs_bucket.mywebsite.bucket key = "index.html" source = "index.html" } # put error.html resource "huaweicloud_obs_bucket_object" "error" { bucket = huaweicloud_obs_bucket.mywebsite.bucket key = "error.html" source = "error.html" }
- Run terraform plan to view resources.
- After you confirm that the resource information is correct, run terraform apply to start file uploading.
- Verify the configuration.
Use a browser to access https://mywebsite.obs-website.eu-west-101.myhuaweicloud.com, that is, to access index.html. mywebsite indicates the OBS bucket name, and eu-west-101 indicates the region to which the bucket belongs.
Resource Name |
Parameter |
Description |
|
---|---|---|---|
huaweicloud_obs_bucket |
bucket |
(Mandatory) OBS bucket name. An OBS bucket name:
|
|
website |
index_document |
(Mandatory) The index page that is returned when you access a static website, that is, the homepage. |
|
error_document |
(Optional) The 404 error page that is returned when an incorrect static website path is accessed. |
||
routing_rules |
(Optional) Rule for redirecting the static website. |
||
huaweicloud_obs_bucket_policy |
bucket |
(Mandatory) Bucket name. |
|
policy_format |
(Optional) Policy format. The value can be obs or s3. The default value is obs. |
||
policy |
(Mandatory) Policy content. For details, see Policy Format. |
||
huaweicloud_obs_bucket_object |
bucket |
(Mandatory) Bucket name. |
|
key |
(Mandatory) Object name. |
||
source |
(Optional) Path to the source file of the object. |
Follow-up Operation
You can bind a user-defined domain name to the access domain name of an OBS bucket so that you can access files stored in OBS through the user-defined domain name. With the domain name management of OBS, you can also use CDN for service acceleration. For details, see Using a User-Defined Domain Name to Host a Static Website.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.