diff --git a/terraform_imports/.terraform.lock.hcl b/terraform_imports/.terraform.lock.hcl new file mode 100644 index 0000000..cecb4d5 --- /dev/null +++ b/terraform_imports/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/kreuzwerker/docker" { + version = "3.0.2" + constraints = "~> 3.0.2" + hashes = [ + "h1:os8pBi4rbtFJJtzNWlcGhOVsz5V9UPJvo+L0wNQFYE8=", + "zh:15b0a2b2b563d8d40f62f83057d91acb02cd0096f207488d8b4298a59203d64f", + "zh:23d919de139f7cd5ebfd2ff1b94e6d9913f0977fcfc2ca02e1573be53e269f95", + "zh:38081b3fe317c7e9555b2aaad325ad3fa516a886d2dfa8605ae6a809c1072138", + "zh:4a9c5065b178082f79ad8160243369c185214d874ff5048556d48d3edd03c4da", + "zh:5438ef6afe057945f28bce43d76c4401254073de01a774760169ac1058830ac2", + "zh:60b7fadc287166e5c9873dfe53a7976d98244979e0ab66428ea0dea1ebf33e06", + "zh:61c5ec1cb94e4c4a4fb1e4a24576d5f39a955f09afb17dab982de62b70a9bdd1", + "zh:a38fe9016ace5f911ab00c88e64b156ebbbbfb72a51a44da3c13d442cd214710", + "zh:c2c4d2b1fd9ebb291c57f524b3bf9d0994ff3e815c0cd9c9bcb87166dc687005", + "zh:d567bb8ce483ab2cf0602e07eae57027a1a53994aba470fa76095912a505533d", + "zh:e83bf05ab6a19dd8c43547ce9a8a511f8c331a124d11ac64687c764ab9d5a792", + "zh:e90c934b5cd65516fbcc454c89a150bfa726e7cf1fe749790c7480bbeb19d387", + "zh:f05f167d2eaf913045d8e7b88c13757e3cf595dd5cd333057fdafc7c4b7fed62", + "zh:fcc9c1cea5ce85e8bcb593862e699a881bd36dffd29e2e367f82d15368659c3d", + ] +} diff --git a/terraform_imports/docker.tf b/terraform_imports/docker.tf new file mode 100644 index 0000000..6cf6652 --- /dev/null +++ b/terraform_imports/docker.tf @@ -0,0 +1,19 @@ +# Terraform configuration +# Configuration-driven import relies on the import block, which has two required arguments: + +# - resource_type: The type of resource to import, in this case, docker_container. +# - resource_name: The name of the resource to import, in this case, my_container. + +import { + id = "305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215" + to = docker_container.web +} + +# generating configuration with the cli is better than manually defining the resources by using +# terraform plan -generate-config-out=generated.tf flag. + +# The import block is used to import existing resources into Terraform state. The id argument specifies the ID of the resource to import, and the to argument specifies the name of the resource in the Terraform configuration. + +resource "docker_image" "nginx" { + name = "nginx:latest" +} \ No newline at end of file diff --git a/terraform_imports/generated.tf b/terraform_imports/generated.tf new file mode 100644 index 0000000..8c1bb09 --- /dev/null +++ b/terraform_imports/generated.tf @@ -0,0 +1,26 @@ +# __generated__ by Terraform +# Please review these resources and move them into your main configuration files. + +# __generated__ by Terraform from "305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215" +resource "docker_container" "web" { + env = [] + image = docker_image.nginx.image_id + name = "hashicorp-import" + ports { + external = 8081 + internal = 80 + ip = "0.0.0.0" + protocol = "tcp" + } +} + +# its possible to bring some resources under terraform management by using the terraform import command. this is often the case for resources defined by a single unique ID or Tag, such as Docker images + +# terraform import docker_container.web 305226eb8001ec8363cf1c2500b63566651362a7dbd45ada90b7163e33d8d215 + +# In the above generated.tf file, the docker_container.web resource specifies the SHA256 hash ID of the image used to create the container. This is how Docker stores the image ID internally, so the import operation loaded the image ID directly into your state. However, identifying the image by its tag or name would make your configuration easier to understand. +# but we can make it easily readable by using the image name instead of the sha256 hash. To do this, we can use the docker_image data source to look up the image by its name and tag. The data source will return the SHA256 hash ID of the image, which we can then use in our docker_container resource. +# we retrieve it with the following command: +# docker image inspect -f {{.RepoTags}} `docker inspect --format="{{.Image}}" hashicorp-import` + +# include it into the resources block : name: "nginx:latest" \ No newline at end of file diff --git a/terraform_imports/main.tf b/terraform_imports/main.tf new file mode 100644 index 0000000..5dd1c90 --- /dev/null +++ b/terraform_imports/main.tf @@ -0,0 +1,3 @@ +provider "docker" { + +} diff --git a/terraform_imports/terraform.tf b/terraform_imports/terraform.tf new file mode 100644 index 0000000..5906035 --- /dev/null +++ b/terraform_imports/terraform.tf @@ -0,0 +1,19 @@ +terraform { + /* Uncomment this block to use Terraform Cloud for this tutorial + cloud { + organization = "organization-name" + workspaces { + name = "learn-terraform-import" + } + } + */ + + required_providers { + docker = { + source = "kreuzwerker/docker" + version = "~> 3.0.2" + } + } + + required_version = "~> 1.5" +}