[A-00199] Terraform入門(AWS)
aws上でterraformを使用して色々作ってみたいと思います。
aws cliにシークレット情報を記録しておきます。下記のコマンドで登録できます。
aws configure基本的に下記のコマンド作業します。各章に記載するのは冗長なのでここに記載しておきます。
terraform init
terraform plan
terraform apply
terraform destroy・EC2を作成してみる
vpc,subnet内にec2を作成します。
アーキテクチャは下記の通り

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.53.0"
    }
  }
}
provider "aws" {
  region = "ap-northeast-1"
}
resource "aws_vpc" "examplevpc" {
  cidr_block = "10.0.0.0/16"
}resource "aws_vpc" "example_vpc" {
  cidr_block = "172.16.0.0/16"
  tags = {
    Name = "Main"
  }
}
resource "aws_subnet" "example_subnet" {
  vpc_id            = aws_vpc.example_vpc.id
  cidr_block        = "172.16.10.0/24"
  availability_zone = "ap-northeast-1a"
  tags = {
    Name = "Main"
  }
}resource "aws_network_interface" "foo" {
  subnet_id   = aws_subnet.example_subnet.id
  private_ips = ["172.16.10.100"]
  tags = {
    Name = "primary_network_interface"
  }
}
resource "aws_instance" "foo" {
  ami           = "ami-097df8c5fa681defb"
  instance_type = "t2.micro"
  network_interface {
    network_interface_id = aws_network_interface.foo.id
    device_index = 0
  }
  credit_specification {
    cpu_credits = "unlimited"
  }
  tags = {
    Name = "Main"
  }
}・Appendix
参考文献はこちら
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
https://stackoverflow.com/questions/69860280/how-to-properly-reset-terraform-default-tfstate
https://docs.aws.amazon.com/ja_jp/signin/latest/userguide/command-line-sign-in.html
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/finding-an-ami.html
https://aws.amazon.com/jp/ec2/instance-types
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
コメントを残す