今回はステージング環境と本番環境でそれぞれS3バケット一つの作成を例とします。具体的な操作手順は以下の通りです。
まず、ステージング環境と本番環境用workspaceを作成
- ステージング環境のワークスペースを作成
$ terraform workspace new staging
Created and switched to workspace "staging"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
Created and switched to workspace "staging"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
- 本番環境用ワークスペースを作成
$ terraform workspace new prod
Created and switched to workspace "prod"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
Created and switched to workspace "prod"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
- ワークスペース一覧を確認
$ terraform workspace list
default
* prod
staging
default
* prod
staging
- ステージング環境を作業中にします。
$ terraform workspace select staging
Switched to workspace "staging".
Switched to workspace "staging".
ステージングと本番用awsアカウント情報を設定
- aws認証情報ファイル(~/.aws/credentials)の中、以下のようにステージングと本番用アクセスキーを設定します。
$ vi ~/.aws/credentials
[stg]
aws_access_key_id = xxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxx
region = ap-northeast-1
[prod]
aws_access_key_id = xxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxx
region = ap-northeast-1
[stg]
aws_access_key_id = xxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxx
region = ap-northeast-1
[prod]
aws_access_key_id = xxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxx
region = ap-northeast-1
- main.tfというファイルを作成し、以下のようにアカウントを切り替えします。
$ vi main.tf
provider "aws" {
region = "ap-northeast-1"
profile = "${terraform.workspace == "staging"? "stg" : "prod"}"
}
provider "aws" {
region = "ap-northeast-1"
profile = "${terraform.workspace == "staging"? "stg" : "prod"}"
}
S3バケットを作成します
- ステージング環境と本番環境用変数を作成します。
$ vi staging.tfvars
bucket_name = "stg-workspace-test"
$ vi prod.tfvars
bucket_name = "workspace-test"
bucket_name = "stg-workspace-test"
$ vi prod.tfvars
bucket_name = "workspace-test"
- バケットを作成するため、s3.tfというファイルを作成します。
$ vi s3.tf
variable "bucket_name" {}
resource "aws_s3_bucket" "example" {
bucket = "${var.bucket_name}"
acl = "private"
}
variable "bucket_name" {}
resource "aws_s3_bucket" "example" {
bucket = "${var.bucket_name}"
acl = "private"
}
まず、ステージング環境で作成します。
- 初期化します。
$ terraform init
Initializing provider plugins...
Initializing provider plugins...
- 実行します
$ terraform workspace select staging && terraform apply -var-file=staging.tfvars
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:
+ aws_s3_bucket.example
id: <computed>
acceleration_status: <computed>
acl: "private"
arn: <computed>
bucket: "stg-workspace-test"
bucket_domain_name: <computed>
bucket_regional_domain_name: <computed>
force_destroy: "false"
hosted_zone_id: <computed>
region: <computed>
request_payer: <computed>
versioning.#: <computed>
website_domain: <computed>
website_endpoint: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions in workspace "staging"?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
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:
+ aws_s3_bucket.example
id: <computed>
acceleration_status: <computed>
acl: "private"
arn: <computed>
bucket: "stg-workspace-test"
bucket_domain_name: <computed>
bucket_regional_domain_name: <computed>
force_destroy: "false"
hosted_zone_id: <computed>
region: <computed>
request_payer: <computed>
versioning.#: <computed>
website_domain: <computed>
website_endpoint: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions in workspace "staging"?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
本番環境で実行します。
ステージング環境で似てようで、以下のコマンドを使って本番でも実行します。
$ terraform workspace select prod && terraform apply -var-file=prod.tfvars
0 件のコメント:
コメントを投稿