4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC2にterraformをインストールしてみる

Last updated at Posted at 2020-07-11

EC2(AmazonLinux2、x86_64)にterraformをインストールする。

##① EC2インスタンス起動
下記内容でインスタンスを立ち上げる。詳細は割愛。

  • AmazonLinux2
  • t2.micro
  • パブリックサブネット
  • AdministratorAccessポリシーを付与したロールをアタッチ
  • セキュリティーグループ
      インバウンド:SSHのみ許可
      アウトバウンド:すべて許可

##② SSH接続
SSHクライアントでEC2インスタンスに接続する。
今回はTeratermを使用。

##③ [任意] AWS CLI の最新化
今回のインスタンスはAmazon Linux 2なのでAWS CLIが最初から入っている。
ただしバージョンが古い場合があるので、ver 1.xx なら 2.xx に上げておくほうが無難。

aws --version を打って 1.xx なら下記手順でバージョンアップしておく。
https://qiita.com/simis/items/46ffe04c346b9fdf7830

##④ Terraformインストール
公式のインストールマニュアルはここ。
https://learn.hashicorp.com/terraform/getting-started/install.html

書いてあるとおりにやってみる。流れは以下のとおり。

・パッケージダウンロード
・パッケージ解凍
・PATH通し

ちなみにパッケージと言っても中身はterraformというバイナリファイル1つだけ。
よってインストールはバイナリファイルにPATHを通すか、
既にPATHの通っているディレクトリにバイナリファイルを移せばよい。
(もちろん ln -s でソフトリンクを張るでもOK)。

今回は後者(既にPATHが通っているディレクトリにバイナリファイルを移す)でやってみる。

###■パッケージダウンロード

提供されているバージョンはこちら。
https://releases.hashicorp.com/terraform/

今回は0.12.28にしてみる(2020/07/11時点の最新バージョン)。

ダウンロード
$ wget https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
--2020-07-11 08:52:09--  https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.109.183, 2a04:4e42:1a::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.109.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28425934 (27M) [application/zip]
Saving to: ‘terraform_0.12.28_linux_amd64.zip’

100%[=================================================================================================>] 28,425,934  74.3MB/s   in 0.4s

2020-07-11 08:52:10 (74.3 MB/s) - ‘terraform_0.12.28_linux_amd64.zip’ saved [28425934/28425934]

$

###■パッケージ解凍

解凍
$ unzip terraform_0.12.28_linux_amd64.zip
Archive:  terraform_0.12.28_linux_amd64.zip
  inflating: terraform
$

terraformというバイナリが解凍されたはず。

###■PATH通し
まずは現状のPATHを確認。

PATH確認
$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
$

どこにバイナリファイルを移動させてもいいのだけど、、
今回は最初に出てきた /usr/local/bin に移すことにした。

バイナリファイル移動(PATH通し)
$ sudo mv terraform /usr/local/bin
$
$
$ ls -l /usr/local/bin/terraform
-rwxr-xr-x 1 ec2-user ec2-user 69818039 Jun 25 13:11 /usr/local/bin/terraform
$

###■動作確認
バイナリファイルが実行できることを確認する。
helpオプションを指定してヘルプが表示されることを確認してみる。

動作確認
$ terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management
$

後片付け

不要ファイル(ダウンロードファイル)を削除する。

不要ファイル削除
$ rm terraform_0.12.28_linux_amd64.zip
$
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?