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?

Terraform の自動実行スクリプト(Bash スクリプト)

Last updated at Posted at 2025-03-31

めんどくさがり御用達のスクリプトです

#!/bin/bash

# 色の定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# エラーハンドリング
set -e

# 並列処理の制限
PARALLELISM=100  

# 削除モードの確認
if [[ "$1" == "destroy" ]]; then
    DESTROY_MODE=true
else
    DESTROY_MODE=false
fi

# スクリプトの実行開始
echo -e "${BLUE} Terraform 自動実行スクリプト開始${NC}"

# Terraform Format
echo -e "${YELLOW} Terraform Format...${NC}"
terraform fmt -recursive
echo -e "${GREEN} Terraform Format 完了${NC}"

# Terraform Validate
echo -e "${YELLOW} Terraform Validate...${NC}"
terraform validate
echo -e "${GREEN} Terraform Validate 成功${NC}"

# Terraform Plan / Destroy Plan (tfplan を常に使用)
if [[ "$DESTROY_MODE" == "true" ]]; then
    echo -e "${YELLOW}  Terraform Destroy Plan...${NC}"
    terraform plan -destroy -out=tfplan -parallelism=$PARALLELISM -input=false
    echo -e "${GREEN} Terraform Destroy Plan 成功${NC}"
else
    echo -e "${YELLOW} Terraform Plan...${NC}"
    terraform plan -out=tfplan -parallelism=$PARALLELISM -input=false
    echo -e "${GREEN} Terraform Plan 成功${NC}"
fi

# Terraform Apply / Destroy (tfplan を適用)
if [[ "$DESTROY_MODE" == "true" ]]; then
    echo -e "${YELLOW}  Terraform Destroy 実行中 (tfplan 使用)...${NC}"
    terraform apply -auto-approve -parallelism=$PARALLELISM tfplan
    echo -e "${GREEN} Terraform Destroy 完了${NC}"
else
    echo -e "${YELLOW} Terraform Apply 実行中 (tfplan 使用)...${NC}"
    terraform apply -auto-approve -parallelism=$PARALLELISM tfplan
    echo -e "${GREEN} Terraform Apply 完了${NC}"
fi

# スクリプトの完了メッセージ
echo -e "${BLUE} Terraform の自動実行が完了しました!${NC}"

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?