0
0

More than 3 years have passed since last update.

公式チュートリアルに沿ってTerraformでEC2インスタンスを立ててみるメモ

Last updated at Posted at 2020-03-13

メモ

  • EC2インスタンスを一台作成する
  • 参考 記載のURLの通りに実行したメモ

インスタンスの構築

# 作業用ディレクトリ作成
% mkdir terraform-test2
% cd terraform-test2

# configuration codeファイル(日本語訳不明)作成
% touch example.tf

# デプロイするサービスプロバイダの種類(AWS)やリソースの情報を記載する(AMI IDは環境によって有効な値に読み替えること)
% vim example.tf
provider "aws" {
  profile    = "default"
  region     = "ap-northeast-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"
}

# 初期化コマンド
# AWSとの連携に必要なファイル類をダウンロードするらしい
% terraform init

# フォーマット & バリデーション
# ファイルのフォーマッティングをしてくれる。インデントとか
% terraform fmt

# バリデーション
% terraform validate

# リソース作成
# `yes` と入力を求められるプロンプトが表示される
% terraform apply
...

# リソース確認
%  terraform show

インスタンスのタイプ変更

# t2.micro -> t2.large に変更
% vim example.tf
provider "aws" {
  profile    = "default"
  region     = "ap-northeast-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.large"
}

# 変更実行
# 勝手にインスタンス止めて、起動させてくれる。すごい。
% terraform apply

インスタンスの削除

% terraform destroy

参考

Build Infrastructure | Terraform - HashiCorp Learn https://learn.hashicorp.com/terraform/getting-started/build

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