{Terraform} S3/OCI

https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/objectstorage_bucket

https://qiita.com/billtench/items/10f85d9098bd1bf1f3b6


echo test > file01.txt
echo test > file02.txt


cat <<-'EOF' > variables.tf

locals {
  tenancy_ocid = "ocid1.tenancy.oc1..000000000000000000000000000000000000000000000000000000000000"

}

variable "compartment_name" {
  description = "compartment_name"
  type = string
  default = "cmp01"
}

variable "bucket_name" {
  description = "bucket_name"
  type = string
  default = "bucket01"
}

EOF

 

cat <<-'EOF' > main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    oci = {
       source  = "hashicorp/oci"
       version = "= 5.23.0"
    }
  }
}

provider "oci" {
  tenancy_ocid = local.tenancy_ocid
  user_ocid = "ocid1.user.oc1..000000000000000000000000000000000000000000000000000000000000" 
  private_key_path = "~/.oci/oci_api_key.pem"
  fingerprint = "45:ed:22:e6:cc:fd:63:97:12:9d:62:7a:90:12:65:7a"
  region = "us-ashburn-1"
}


resource "oci_identity_compartment" "cmp01" {
    # Required
    compartment_id = local.tenancy_ocid
    description = var.compartment_name
    name = var.compartment_name
    
    enable_delete = true
}


data "oci_objectstorage_namespace" "ns01" {
    compartment_id = local.tenancy_ocid
}

resource "oci_objectstorage_bucket" "bucket01" {
    #Required
    compartment_id = oci_identity_compartment.cmp01.id
    name = var.bucket_name
    namespace = data.oci_objectstorage_namespace.ns01.namespace

    #Optional
    access_type = "NoPublicAccess"
    auto_tiering = "Disabled"
    object_events_enabled = false
    storage_tier = "Standard"
    versioning = "Disabled"
    
}


resource "oci_objectstorage_object" "file01" {
    #Required
    bucket = var.bucket_name
    content = file("${path.module}/file01.txt")
    namespace = data.oci_objectstorage_namespace.ns01.namespace
    object = "file01"
}

resource "oci_objectstorage_object" "file02" {
    #Required
    bucket = var.bucket_name
    content = file("${path.module}/file02.txt")
    namespace = data.oci_objectstorage_namespace.ns01.namespace
    object = "file02"
}

EOF

 

cat <<-'EOF' > outputs.tf

output "cmp01_id" {
  value = oci_identity_compartment.cmp01.id
  description = "cmp01.id"
}

output "ns01_namespace" {
  value = data.oci_objectstorage_namespace.ns01.namespace
  description = "ns01.namespace"
}

output "bucket01" {
  value = oci_objectstorage_bucket.bucket01
  description = "bucket01"
}

output "file01" {
  value = oci_objectstorage_object.file01
  description = "file01"
}

output "file02" {
  value = oci_objectstorage_object.file02
  description = "file02"
}

EOF

 


terraform init
terraform fmt
terraform -version

export TF_VAR_compartment_name=cmp01
export TF_VAR_bucket_name=bucket01


terraform plan

terraform apply -auto-approve


バケット作成とオブジェクトアップロードを同時に行うと下記エラー。2回実行する必要がある
Error: 404-BucketNotFound, Either the bucket named 'bucket01' does not exist in the namespace '111111111111' or you are not authorized to access it


terraform destroy -auto-approve

★削除時も下記エラーがタイミングにより発生。エラーとなった場合、2回実行する必要がある
Error: 409-BucketNotEmpty, Bucket named 'bucket01' is not empty. Delete all object versions first.


oci os object get \
--bucket-name bucket01 \
--name file01 \
--file - 

oci os object get \
--bucket-name bucket01 \
--name file02 \
--file -