Terraform入門 AWS S3を作成するサンプル
環境
Mac
aws cli
操作手順
1.aws cliをインストールする
$ brew install awscli
2.aws configureで認証情報を設定する
構成と認証情報ファイルの設定
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
バージョン確認
$ aws --version
$ aws --version
$ aws --version
3.tfenvをインストールする
$ brew install tfenv
$ tfenv --version
$ brew install tfenv
$ tfenv --version
$ brew install tfenv $ tfenv --version
terraform 1.5.1バージョンを指定してインストール
$ tfenv install 1.5.1
$ terraform --version
$ tfenv install 1.5.1
$ terraform --version
$ tfenv install 1.5.1 $ terraform --version
4.新規プロジェクトを作成する
# mkdir test-terraform-aws-s3
# cd test-terraform-aws-s3
main.tfを作成する
$ touch main.tf
サンプルコード
terraform {
// terraformのバージョンを指定
required_version = ">= 1.5.1"
// awsプロバイダーのバージョンを指定
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
}
// awsプロバイダーの設定
provider "aws" {
// regionを指定
region = "ap-northeast-1"
}
// s3バケットを作成
resource "aws_s3_bucket" "example" {
bucket = "example-bucket-test" // s3バケット名をユニークにする
}
terraform {
// terraformのバージョンを指定
required_version = ">= 1.5.1"
// awsプロバイダーのバージョンを指定
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
}
// awsプロバイダーの設定
provider "aws" {
// regionを指定
region = "ap-northeast-1"
}
// s3バケットを作成
resource "aws_s3_bucket" "example" {
bucket = "example-bucket-test" // s3バケット名をユニークにする
}
terraform { // terraformのバージョンを指定 required_version = ">= 1.5.1" // awsプロバイダーのバージョンを指定 required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } } // awsプロバイダーの設定 provider "aws" { // regionを指定 region = "ap-northeast-1" } // s3バケットを作成 resource "aws_s3_bucket" "example" { bucket = "example-bucket-test" // s3バケット名をユニークにする }
5.初期化を行う
$ terraform init
実行する前に確認
$ terraform plan
実行
$ terraform apply
参考資料
https://developer.hashicorp.com/terraform/tutorials/aws-get-started