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?

【AWS】TerraformでS3バケット作成 🔰

Last updated at Posted at 2025-02-26

1. はじめに 📝

本記事では、TerraformのインストールからS3バケット作成のみを実施します。
筆者はCloudFormationは扱い慣れていますが、Terraformは未経験です。
構文すら分かってない状態ですが、今後使っていくことになりそうなので、
まずはどんどん触れてみようと思います。

📌 参考は公式ドキュメント
👉What is Terraform?

2. 本記事の環境 💻

以下の環境で実施しました。

  • PC: M1 MacBook Air
  • OS: macOS Monterey 12.7.6
  • Terraform: v1.10.5
  • エディタ: Visual Studio Code (VSCode)

3. Terraformのインストール ⚙️

Macにインストール

MacではHomebrewを使って簡単にインストールできました。

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

以下コマンドでインストールできているか確認します。

terraform -v

Terraform v1.10.5
on darwin_arm64

その他の環境

WindowsやLinuxでのインストール方法については公式サイトに記載があります。
👉Install Terraform

4. Terraformの基本概念 📌

Terraformの設定ファイルや基本コマンドを確認します。

ファイル構成

以下のようなファイルを作成しました。

ディレクトリ
terraform-s3/
├── main.tf   # リソース定義
└── variables.tf   # 変数定義

基本コマンド

Terraformの基本的なコマンドを確認します。

コマンド
terraform init    # 初期化
terraform plan    # 実行プランの確認
terraform apply   # 変更の適用
terraform destroy # リソース削除

5. S3バケットの作成 🪣

ここからS3バケットを作成していきます。

variables.tf の作成

今回はS3バケット1つのみですが、Terraformの基本を学ぶために作成しました。

variables.tf
variable "bucket_name" {
  description = "S3バケット名"
  type        = string
  default     = "任意のS3バケット名"
}

main.tf の作成

続いてmain.tfを作成しました。

main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"  # AWSリージョン
}

resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name  # 変数を使用
}

適用手順

以下のコマンドを順番に実行しました。

コマンド
terraform init
terraform plan
terraform apply

ちゃんと出来てました☺️
CleanShot 2025-02-26 at 21.01.48@2x.png


TerraformでS3バケットを作成する方法を試してみました!
今後もたくさんリソースを作成して、早く使いこなせるように学習していこうと思います。
既存リソースのインポートもできる?みたいなので、いづれ試してみようと思います!

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?