使用terraform-provider-s3 操作minio

时间:2023-03-09 14:26:44
使用terraform-provider-s3 操作minio

尽管默认官方提供了s3 的操作,但是对于开源minio 无法支持,更多的是aws 的s3,社区提供了一个通用
s3 操作的provider(基于minio 的sdk)

环境准备

  • docker-compose 文件
version: "3"
services:
s3:
image: minio/minio
command: server /export
ports:
- "9000:9000"
volumes:
- ./data:/export
- ./config:/root/.minio
environment:
- "MINIO_ACCESS_KEY=dalongdemo"
- "MINIO_SECRET_KEY=dalongdemo"
  • s3 terraform 操作
    添加了依赖处理以及一个简单静态web 页面部署
provider "s3" {
s3_server = "localhost:9000"
s3_access_key = "dalongdemo"
s3_secret_key = "dalongdemo"
s3_api_signature = "v4"
s3_ssl = false
s3_debug = true
} resource "s3_bucket" "bucket_create" {
bucket = "s3page"
} resource "s3_file" "upload_index_page" {
bucket = "s3page"
name = "index.html"
file_path = "./files/index.html"
content_type = "text.html"
debug = true
depends_on = ["s3_bucket.bucket_create"]
} resource "s3_file" "upload_user_js" {
bucket = "s3page"
name = "user.js"
file_path = "./files/user.js"
content_type = "application/javascript"
debug = true
depends_on = ["s3_bucket.bucket_create"] }

部署

  • 插件准备
我测试环境是mac,使用go get 安装provider,然后copy plugin 到tf 定义文件目录,注意需要创建目录
mkdir -p ./.terraform/plugins/darwin_amd64/
./.terraform/plugins/darwin_amd64/
  • init
terraform init
  • 查看plan
terraform plan

效果

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create Terraform will perform the following actions: + s3_bucket.bucket_create
id: <computed>
bucket: "s3page"
debug: "false" + s3_file.upload_index_page
id: <computed>
bucket: "s3page"
content_type: "text.html"
debug: "true"
file_path: "./files/index.html"
name: "index.html" + s3_file.upload_user_js
id: <computed>
bucket: "s3page"
content_type: "application/javascript"
debug: "true"
file_path: "./files/user.js"
name: "user.js" Plan: 3 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
  • apply
terraform apply

效果

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create Terraform will perform the following actions: + s3_bucket.bucket_create
id: <computed>
bucket: "s3page"
debug: "false" + s3_file.upload_index_page
id: <computed>
bucket: "s3page"
content_type: "text.html"
debug: "true"
file_path: "./files/index.html"
name: "index.html" + s3_file.upload_user_js
id: <computed>
bucket: "s3page"
content_type: "application/javascript"
debug: "true"
file_path: "./files/user.js"
name: "user.js" Plan: 3 to add, 0 to change, 0 to destroy. Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve. Enter a value: yes s3_bucket.bucket_create: Creating...
bucket: "" => "s3page"
debug: "" => "false"
s3_bucket.bucket_create: Creation complete after 0s
s3_file.upload_index_page: Creating...
bucket: "" => "s3page"
content_type: "" => "text.html"
debug: "" => "true"
file_path: "" => "./files/index.html"
name: "" => "index.html"
s3_file.upload_user_js: Creating...
bucket: "" => "s3page"
content_type: "" => "application/javascript"
debug: "" => "true"
file_path: "" => "./files/user.js"
name: "" => "user.js"
s3_file.upload_index_page: Creation complete after 0s
s3_file.upload_user_js: Creation complete after 0s Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  • minio 界面
    使用terraform-provider-s3 操作minio
  • 设置浏览器访问
    使用terraform-provider-s3 操作minio
  • 直接访问
    使用terraform-provider-s3 操作minio

说明

terraform-provider-s3 功能还算不错,使用起来也比较简单,但是功能还不是很多,比如设置策略,配置通知。。。。,文件夹数据复制,
桶数据复制,但是实现起来还是相对简单的,可以参考provider 开发

参考资料

https://github.com/negronjl/terraform-provider-s3
https://github.com/rongfengliang/terraform-minio-s3-provider-demo