terraform

terraform 인스턴스 생성

짱구는굿 2023. 11. 21. 14:50

https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html

apt remove awscli
apt install -y curl
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

..... --version


aws configure
aws sts get-caller-identity


sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
apt-get update && apt-get install terraform


touch ~/.bashrc
terraform -install-autocomplete
재부팅 

nano ~/.terraformrc 
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache" 
mkdir -p ~/.terraform.d/plugin-cache

 

 

최신 버전의 AWS CLI 설치 또는 업데이트 - AWS Command Line Interface

이전 버전에서 업데이트하는 경우 unzip 명령을 실행하면 기존 파일을 덮어쓸지 묻는 메시지가 표시됩니다. 스크립트 자동화와 같은 경우에 이러한 프롬프트를 건너뛰려면 unzip에 대한 -u 업데이

docs.aws.amazon.com

 

provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_instance" "ubuntu" {   
  ami           = "ami-09eb4311cbaecf89d" 
  instance_type = "t2.micro"

  tags = {
    Name = "itbank-ubuntu"
  }
}

 

nano main.tf
terraform init -> plan -> apply

 

 

인스턴스가 생성이 되었다
만약 실패를 한다면
aws ec2 create-default-subnet --availability-zone ap-northeast-2a -> zone을 생성을 해주자

 

provider "aws" {
  region = "ap-northeast-2"
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "ubuntu" {
  ami           = data.aws_ami.ubuntu.image_id
  instance_type = "t2.micro"

  tags = {
    Name = "itbank-ubuntu"
  }
}

 

main.tf -> 전에 있던 내용 지우고 붙여넣기
terraform init -> plan -> apply

 

 

전에 있던건 삭제 후 다시 설치를 해주었다

 

 

aws으로 확인