【AWS CloudShell】Terraform実行環境の作成手順を紹介します

AWS全般

今回は、AWS CloudShellでTerraform実行環境を作成し、terraform planを実行するまでの手順をご紹介します。最近、業務でAWS CloudShellを使う機会があったのですが、とても便利でした。
以前、Cloud9からTerraformを実行したことがあるので、その時に使ったファイルを使用して、AWS CloudShellで実行してみたいと思います。

AWS CloudShellでTerraformを実行する手順

前提条件

・Terraformのtfファイルは過去の記事で作成したものを使用する

AWS CloudShellの開始

1.AWSにログインしてAWS CloudShellを検索する
2.「Welcome to AWS CloudShell」のポップアップを「Close」ボタンで閉じる
3.AWS CloudShellが起動するのを待つ

Terraform実行環境の準備

Terraformの最新バージョンをダウンロード

$ wget https://releases.hashicorp.com/terraform/1.1.9/terraform_1.1.9_linux_amd64.zip

ダウンロードしたファイルの展開

$ sudo unzip terraform_1.1.9_linux_amd64.zip -d /usr/local/bin/

Archive: terraform_1.1.9_linux_amd64.zip
inflating: /usr/local/bin/terraform

Terraformのバージョン確認

$ terraform --version
Terraform v1.1.9
on linux_amd64

Terraform実行ファイルの準備

Terraformの実行ファイルをcloneする

$ git clone <リポジトリ名>

main.tfファイルのあるディレクトリに移動する

$ cd <ディレクトリ名>

Terraformを初期化する

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.71.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Terraformの実行

今回はTerraform planまで実施しました

$ terraform plan


Plan: 10 to add, 0 to change, 0 to destroy.

まとめ

AWS CloudShellでは、Cloud9よりも手軽にTerraformの実行環境を作成することができました。
個人の検証や、メンバーで実行環境を共有する必要がなければ、AWS CloudShellからTerraformを実行する方が便利だと思いました。今後の記事では、Terraformで色々なAWSリソースの作成に挑戦していこうと考えております。

タイトルとURLをコピーしました