0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

localstackにterraformでs3 bucketを作成する

Posted at

はじめに

本記事では、localstackにterraformでs3 bucketを作成する方法を紹介します。

環境情報

コンポーネント バージョン 備考
PC Intel® NUC Kit NUC11PAHi7 SSD: 2GiB
Memory: 64GiB
OS Ubuntu 24.04
Docker 26.1.3
LocalStack 4.4.0
awscli 2.27.17
awscli-local 0.22.0
terraform v1.12.0

手順

以下の記事で紹介した環境がセットアップされていることを前提とします。
https://qiita.com/ykmchd/items/a6f8834a5787cc7fbe85

provider.tfを作成する

以下のprovider.tfを作成します。

provider.tf
terraform {
  required_version = "~> 1.12.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5"
    }
  }
}

provider "aws" {
  access_key        = "test"
  secret_key        = "test"
  region            = "us-east-1"
  s3_use_path_style = true

  endpoints {
    s3 = "http://localhost:4566"
  }
}

ポイントはs3_use_path_style = trueを設定することです。
デフォルトのバーチャルホスト形式でs3 bucektを表現します。
しかし、localstackではバーチャルホスト形式が使えないため、このオプションを指定する必要があります。
https://registry.terraform.io/providers/hashicorp/aws/5.39.1/docs#s3_use_path_style-1

s3.tfを作成する

以下のs3.tfを作成します。

s3.tf
resource "aws_s3_bucket" "s3-localstack" {
  bucket = "s3-localstack"
}

s3-localstackという名前のs3 bucketを作成します。

terraformを実行する

以下のコマンドを実行します。

terraform init
terraform plan
terraform apply

s3 bucketを作成できたか確認する

awslocalコマンドでs3 bucketが作成できたか確認します。

❯ awslocal s3 ls           
2025-05-18 23:12:40 s3-localstack

最後に

localstackにterraformでs3を作成する方法を紹介しました。
terraformの練習をするときに、実際のAWSを使うと料金が心配です。
しかし、localstackだと使い勝手は違いますが、料金を気にせず試せるのが良いですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?