실제 리소스로 부터 테라폼 코드를 만들어보자.
컨피그 작성 -- PLAN------APPLY
존재하는 인프라--IMPORT----컨피그 작성 -- PLAN------APPLY
1
docker run --name hashicorp-learn --detach --publish 8080:80 nginx:latest
2
docker ps
1
git clone https://github.com/hashicorp/learn-terraform-import.git
2
cd learn-terraform-import
Docker provider.
Docker container
3
terraform init
Error: Failed to query available provider packages then run this command:
terraform init -upgrade
4
provider "docker" {
# host = "npipe:////.//pipe//docker_engine"
}
5
docker.tf file
resource "docker_container" "web" {}
6
docker ps
7
terraform import docker_container.web $(docker inspect -f {{.ID}} hashicorp-learn)
terraform show
1
terraform plan
2
terraform show -no-color > docker.tf
3
terraform plan
4
Remove all of these attributes, keeping only the required attributes:
image
,
name
, and
ports
resource "docker_container" "web" {
image = "sha256:87a94228f133e2da99cb16d653cd1373c5b4e8689956386c1c12b60a20421a02"
name = "hashicorp-learn"
ports {
external = 8080
internal = 80
ip = "0.0.0.0"
protocol = "tcp"
}
}
5
terraform plan
6
terraform apply
1
replacing
<IMAGE-ID>
with the image ID from
:
docker image inspect <IMAGE-ID> -f {{.RepoTags}}
2
Add
resource "docker_image" "nginx" {
name = "nginx:latest"
}
3
terraform apply
4
Change
docker_container.web
resource "docker_container" "web" {
image = docker_image.nginx.latest
name = "hashicorp-learn"
ports {
external = 8080
internal = 80
ip = "0.0.0.0"
protocol = "tcp"
}
}
5
terraform apply
1
8080 to 8081:
resource "docker_container" "web" {
name = "hashicorp-learn"
image = docker_image.nginx.latest
ports {
external = 8081
internal = 80
ip = "0.0.0.0"
protocol = "tcp"
}
}
2
terraform apply
3
docker ps
1
terraform destroy
2
docker ps --filter "name=hashicorp-learn"
1
What is Mutable vs. Immutable Infrastructure?
https://www.hashicorp.com/resources/what-is-mutable-vs-immutable-infrastructure
2
테라포머
https://github.com/GoogleCloudPlatform/terraformer
3
커뮤니티
https://www.terraform.io/community
4
모듈 샘플
https://github.com/GoogleCloudPlatform/terraform-google-examples
5
튜트리얼
https://developer.hashicorp.com/terraform/tutorials
6
마켓 플레이스
https://brunch.co.kr/@topasvga/2419
감사합니다.