LoginSignup
1
1

More than 1 year has passed since last update.

Terraformの概要とAWS EC2作成の定義のやり方について

Posted at

今回は、Terraformを学んでみました。
Terraformとは、AWS、GCP、Azureなどのクラウドリソースを、
コードで定義して、構築できるオープンソースです。

AWSでいう、Cloud Formationに似ているものですが、
AzureやGCPも汎用的に使えるということで、
Dockerや、Kubernetesと同様に広く愛されている技術です。

最低限の使い方ということで、
試しに、AWS EC2のt2マイクロを作成するための、
定義ファイルをmain.tfという名前で作ってみました。

事前準備

まずは、最初の準備として、
・Terraformがインストールされていること
・AWS CLIがインストールされていること

yusuke@mbp terraform % terraform --version
Terraform v0.15.4
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.26.0

Your version of Terraform is out of date! The latest version
is 1.2.8. You can update by downloading from https://www.terraform.io/downloads.html
yusuke@mbp terraform % aws --version
aws-cli/2.7.20 Python/3.9.11 Darwin/20.4.0 exe/x86_64 prompt/off

また、AWS CLIの初期設定や、IAM設定も、
今回は触れませんが必要です。

.tfファイルにEC2起動の定義を記載する。

.tfファイルはTerraformを表しています。
provider "aws" {
  profile = "terraform"
  region = "ap-northeast-1"
}

resource "aws_instance" "test" {
  ami = "ami-0ecb2a61303230c9d"
  instance_type = "t2.micro"

  tags = {
    Name = "test"
  }
} 

EC2の作成だけなら、やることはこれだけです。
実際使用するときは、サブネットや冗長化、インターネットゲートウェイなど、もっと大量に記載するものはありますね。

今回は、これだけで、
別の記事で、実際の使い方や、以下のコマンドについて触れていきます。

terraform init
terraform plan
terraform apply
1
1
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
1
1