Hashicorp(하시코프)에서 Go언어로 개발한 오픈소스 IaC 도구로, 인프라 서버를 구축/운영할 수 있게 도와주는 오픈소스 소프트웨어이다
테라폼은 API를 통해 클라우드는 온프레미스에 리소스를 생성하고 관리한다.
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gp
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
# terraform -version
on linux_amd6
-- 자동완성 활성화
terraform -install-autocomplete
# terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.22.0
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.
# vi main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:1.22.0"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
# terrafrom validate
Success! The configuration is valid.
# terraform plan
docker_image.nginx: Refreshing state... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_container.nginx: Refreshing state... [id=41a476861cf301835c65c615dff188d15466f025f4327bd41fb24955a8469da9]
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:
# docker_container.nginx will be created
+ resource "docker_container" "nginx" {
`
`
`
(중략)
`
`
`
Plan: 1 to add, 0 to change, 0 to destroy.
# 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:
# docker_container.nginx will be created
+ resource "docker_container" "nginx" {
`
`
`
(중략)
`
`
`
Plan: 2 to add, 0 to change, 0 to destroy.
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
docker_image.nginx: Creating...
Message from syslogd@user at Jul 13 15:00:11 ...
docker_image.nginx: Still creating... [44s elapsed]
docker_image.nginx: Creation complete after 44s [id=sha256:88e754207f36628c574c1a0cbcde92147eff93b32c497831c75a9d3d2cd0d0a3nginx:1.22.0]
docker_container.nginx: Creating...
docker_container.nginx: Creation complete after 5s [id=3f1978b2cea5ea36dc464a0c9476f5509c7708f0a1553b674c49a6b82d87370d]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
# terraform destroy
docker_image.nginx: Refreshing state... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_container.nginx: Refreshing state... [id=41a476861cf301835c65c615dff188d15466f025f4327bd41fb24955a8469da9]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# docker_image.nginx will be destroyed
- resource "docker_image" "nginx" {
- id = "sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0" -> null
- keep_locally = false -> null
- latest = "sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35" -> null
- name = "nginx:1.22.0" -> null
- repo_digest = "nginx@sha256:d4fe835ca959ff06f3f0d6fff47dc9d480d21179cefea413f07e1be6b1de16c3" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
docker_image.nginx: Destroying... [id=sha256:b3c5c59017fbb3fdcebe18b977da09d1e448218354d73ecd036283ddb81b3c35nginx:1.22.0]
docker_image.nginx: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.