# How to Configure Terraform for NIPA Object Storage

## วิธีการตั้งค่า Terraform กับ NIPA Object Storage ด้วย Backend "s3"

การตั้งค่า Terraform เพื่อใช้งานกับ NIPA Object Storage โดยใช้ backend ประเภท "s3" สามารถทำได้ตามขั้นตอนดังนี้

***

### 1. ติดตั้ง Terraform

* ดาวน์โหลด Terraform เวอร์ชันล่าสุดจาก [เว็บไซต์ Terraform](https://www.terraform.io/downloads.html)
* ติดตั้ง Terraform ตามคำแนะนำที่เหมาะสมกับระบบปฏิบัติการของคุณ

***

### 2. สร้าง Bucket ใน NIPA Object Storage

* เข้าสู่ระบบ [NIPA Cloud Space](https://space.nipa.cloud/)
* [สร้าง Object Storage Sub-Users](https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/create-an-object-storage-sub-user)
* [สร้าง Object Storage Bucket](https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/create-an-object-storage-bucket)
* ทดสอบเชื่อม Bucket ด้วยโปรแกรม [Cyberduck (UI)](https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/access-s3-bucket-with-cyberduck) หรือ ด้วย [s3cmd (CLI)](https://app.gitbook.com/o/b4jKLRni8sRoSESA9D6d/s/q2BYmWTMiaonrEL8QBP2/~/changes/483/storage/object-storage-s3/access-s3-bucket-with-s3cmd) ![](https://t9003005656.p.clickup-attachments.com/t9003005656/667d72f3-c4ff-499f-97b9-443ecfe96041/image.png)

***

### 3. สร้างไฟล์ `main.tf`

สร้างไฟล์ `main.tf` เพื่อกำหนดการตั้งค่า backend และ resource ที่ต้องการ โดยตัวอย่างการตั้งค่า backend สำหรับ NIPA Object Storage มีดังนี้:

```yaml
terraform {
  # เพิ่มส่วนนี้เข้ามาเพื่อเชื่อมต่อกับ S3 NIPA
  backend "s3" {
    bucket         = "ชื่อ-bucket"
    key            = "path/ของ/terraform.tfstate" e.g. 
    region         = "NCP-TH"
    endpoint       = "https://s3-bkk.nipa.cloud"
    # เพื่อความปลอดภัยไม่เก็บ Access Key และ Secret Key ไว้ในไฟล์ .tf โดยตรง
    access_key     = "access-key-ของคุณ"
    secret_key     = "secret-key-ของคุณ"
    # --- ค่าสำคัญสำหรับ S3-Compatible Storage ---
    skip_credentials_validation = true
    skip_metadata_api_check     = true
    skip_region_validation      = true
    force_path_style            = true
    skip_requesting_account_id  = true
    skip_s3_checksum            = true
     
  } //backend
} //terraform

#  provider ที่ต้องการจะใช้
provider "openstack" {
  ...
}
# resource ที่ต้องการสร้าง
resource "openstack_networking_port_v2" "create_myport" {
  ...
}
```

**คำอธิบาย:**

* `bucket`: ชื่อ bucket ที่สร้างไว้ใน NIPA Object Storage
* `key`: ไฟล์ที่ใช้เก็บสถานะ (state) ของ Terraform ใน Bucket ![](https://t9003005656.p.clickup-attachments.com/t9003005656/2df8faab-74bf-437c-b332-5dbc62ccfc6f/image.png)
* `region`: ชื่อ region (สามารถกำหนดเป็นค่าใดก็ได้ เช่น `default`)
* `endpoint`: URL สำหรับเชื่อมต่อกับ NIPA Object Storage
* `access_key` และ `secret_key`: Credentials สำหรับการเข้าถึง Object Storage หรือ Sub-Users ของ nipa cloud space **คำอธิบาย:** การกำหนดค่า S3-Compatible Storage มีวัตถุประสงค์เพื่อปรับการทำงานของ S3 backend ให้สามารถใช้งานร่วมกับ Object Storage ที่ไม่ใช่ของ AWS ได้ โดยปกติแล้ว S3 backend ถูกออกแบบมาเพื่อใช้งานกับ AWS โดยเฉพาะ จึงมีขั้นตอนการตรวจสอบหลายอย่างที่เฉพาะเจาะจงสำหรับ AWS เท่านั้น อย่างไรก็ตาม เมื่อใช้งานกับผู้ให้บริการรายอื่น เช่น Nipa Cloud จำเป็นต้องใช้ flag เหล่านี้เพื่อแจ้งให้ Terraform ข้ามการตรวจสอบเฉพาะของ AWS เหล่านี้ไป
* `skip_credentials_validation = true` บอก Terraform ว่า "ไม่ต้องพยายามนำ Access Key นี้ไปตรวจสอบกับระบบยืนยันตัวตนของ AWS (STS)"
* `skip_metadata_api_check = true` บอกว่า "ไม่ต้องเช็คหา Metadata service ของเครื่อง EC2" เพราะเราไม่ได้รันอยู่บน AWS
* `skip_region_validation = true` บอกว่า "ไม่ต้องตรวจสอบว่า Region ที่ระบุ (เช่น NCP-TH) เป็น Region ที่ถูกต้องของ AWS หรือไม่"
* `force_path_style = true` บังคับให้ Terraform เข้าถึง Bucket แบบ `endpoint/bucket` แทนที่จะเป็น `bucket.endpoint` ซึ่งเป็นวิธีที่ S3-compatible storage ส่วนใหญ่ใช้
* `skip_requesting_account_id = true` บอกว่า "ไม่ต้องถามหา AWS Account ID" เพราะผู้ให้บริการรายอื่นไม่มีสิ่งนี้
* `skip_s3_checksum = true` บอกให้ข้ามการตรวจสอบความสมบูรณ์ของไฟล์ ซึ่งมีประโยชน์เมื่อ ETag ที่ผู้ให้บริการส่งกลับมาไม่ตรงกับมาตรฐานของ AWS

***

### 5. รันคำสั่ง Terraform

1. **Initialize Terraform**: คำสั่งนี้จะทำการตั้งค่า backend และดาวน์โหลด provider ที่จำเป็น

```bash
terraform init
```

2. **ตรวจสอบแผนการทำงาน**: ใช้เพื่อตรวจสอบว่า Terraform จะทำอะไรบ้าง

```bash
terraform plan
```

3. **Apply การตั้งค่า**: ใช้เพื่อดำเนินการสร้าง resource ตามที่กำหนดไว้ในไฟล์ `main.tf`

```bash
terraform apply
```

***

## know issue

#### ปัญหา Error: `XAmzContentSHA256Mismatch` (Version terraform มีผล)

<https://github.com/hashicorp/terraform/issues/36704> หนึ่งในข้อผิดพลาดที่ผู้ใช้ Terraform กับ S3-compatible storage มักจะเจอคือ XAmzContentSHA256Mismatch ซึ่งเกิดจากความไม่เข้ากันของวิธีการคำนวณ "ลายเซ็นดิจิทัล" (Checksum) ระหว่าง Terraform และ S3 provider

ในอดีต การแก้ปัญหานี้มักจะต้อง Downgrade Terraform ไปใช้เวอร์ชันเก่า แต่สำหรับ Terraform v1.12.2 ขึ้นไป มีวิธีแก้ปัญหาที่ทันสมัยและดีกว่า นั่นคือการตั้งค่า Environment Variable เพื่อให้ Terraform ยืดหยุ่นในการตรวจสอบ Checksum มากขึ้น **ตั้งค่า Environment Variable ต่อไปนี้ใน Shell ของคุณก่อนรันคำสั่ง terraform init**

```bash
export AWS_RESPONSE_CHECKSUM_VALIDATION=when_required
export AWS_REQUEST_CHECKSUM_CALCULATION=when_required
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-epc.gitbook.io/ncs-documents/terraform/how-to-configure-terraform-for-nipa-object-storage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
