terraform initコマンド,terraform applyコマンドでEC2を作成する
新しいディレクトリhogeを作成しカレントディレクトリに移動します。
mkdir hoge cd hoge touch main.tf
main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "app_server" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
terraform init
terraform initコマンドでディレクトリを初期化します。
Terraform has been successfully initialized!
こんな表示が出たら初期化成功です。
.terraformフォルダ、.terraform.lock.hclというロックファイルが作成されます。
terraform apply
terraform applyコマンドでmain.tfを適用します。
適用されるとterraform.tfstateファイルが作成されます。
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.app_server will be created
+ resource "aws_instance" "app_server" {
+ ami = "ami-830c94e3"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ disable_api_stop = (known after apply)
+ disable_api_termination = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
...
known after applyはデプロイしたら決まる値です。
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
1つのリソースが適用されています。これでEC2作成完了です。
マネージメントコンソールから確認します。
EC2インスタンスが作成されていることが確認できます。

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^



コメント