반응형
우분투 22.04에서 Terraform을 설치하는 방법
Terraform 설치
필요한 패키지 업데이트 및 설치
sudo apt update && sudo apt install -y gnupg software-properties-common
HashiCorp GPG 키 추가
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
HashiCorp 리포지토리 추가
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
패키지 목록 업데이트
sudo apt update
Terraform 설치
sudo apt install -y terraform
설치 확인
terraform -help
Usage: terraform [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
All other commands:
console Try Terraform expressions at an interactive command prompt
fmt Reformat your configuration in the standard style
force-unlock Release a stuck lock on the current workspace
get Install or upgrade remote Terraform modules
graph Generate a Graphviz graph of the steps in an operation
import Associate existing infrastructure with a Terraform resource
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
metadata Metadata related commands
modules Show all declared modules in a working directory
output Show output values from your root module
providers Show the providers required for this configuration
refresh Update the state to match remote systems
show Show the current state or a saved plan
state Advanced state management
taint Mark a resource instance as not fully functional
test Execute integration tests for Terraform modules
untaint Remove the 'tainted' state from a resource instance
version Show the current Terraform version
workspace Workspace management
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this help output, or the help for a specified subcommand.
-version An alias for the "version" subcommand.
terraform -version
Terraform v1.12.2
on linux_amd64
728x90
Enable tab completion
touch ~/.bashrc
terraform -install-autocomplete
$ tail -n1 .bashrc
complete -C /usr/bin/terraform terraform
AWS Provider 등록
작업 디렉토리 생성
mkdir aws
cd aws
AWS CLI 및 자격 증명 설정
aws configure list
- AWS 액세스 키의 값을 출력
aws configure get aws_access_key_id
- AWS 비밀 액세스 키의 값을 출력
aws configure get aws_secret_access_key
Terraform 구성 파일 작성
- required_providers : Terraform에서 사용할 AWS Provider를 정의합니다.
vim terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.92"
}
}
required_version = ">= 1.2"
}
- provider "aws" : AWS 리전을 설정하고, AWS 자격 증명을 기본적으로 ~/.aws/credentials에서 가져옵니다.
vim main.tf
provider "aws" {
region = "ap-northeast-1"
profile = "default"
}
data "aws_vpcs" "all" {}
output "vpc_ids" {
value = data.aws_vpcs.all.ids
}
provider 다운로드 및 초기화
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.92"...
- Installing hashicorp/aws v5.100.0...
- Installed hashicorp/aws v5.100.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
구성 확인
terraform validate
Success! The configuration is valid.
실행 계획 확인
terraform plan
data.aws_vpcs.all: Reading...
data.aws_vpcs.all: Read complete after 0s [id=ap-northeast-1]
Changes to Outputs:
+ vpc_ids = [
+ "vpc-1xeab277a",
]
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
─────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
실제 인프라 적용
terraform apply
data.aws_vpcs.all: Reading...
data.aws_vpcs.all: Read complete after 0s [id=ap-northeast-1]
Changes to Outputs:
+ vpc_ids = [
+ "vpc-1xeab277a",
]
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
vpc_ids = tolist([
"vpc-1xeab277a",
])
참고URL
- Terraform Documentation : Install Terraform
- Terraform Documentation : Create infrastructure
728x90
반응형
'리눅스' 카테고리의 다른 글
AVX란? (2) | 2025.07.18 |
---|---|
PostgreSQL 데이터 초기화 (0) | 2025.07.18 |
도커 컨테이너로 Kafka, ZooKeeper 그리고 Kafdrop을 구성하는 방법 (1) | 2025.07.16 |
PostgreSQL 로그 설정 및 system_stats 확장 활성화하는 방법 (0) | 2025.07.15 |
CentOS 7에서 PostgreSQL을 설치하는 방법 (0) | 2025.07.15 |