{"id":22060595,"url":"https://github.com/hashicorp-education/learn-terraform-import","last_synced_at":"2025-07-24T01:31:48.854Z","repository":{"id":42467430,"uuid":"264026859","full_name":"hashicorp-education/learn-terraform-import","owner":"hashicorp-education","description":"Learn how to import existing configuration into Terraform","archived":false,"fork":false,"pushed_at":"2024-05-03T19:48:05.000Z","size":40,"stargazers_count":58,"open_issues_count":0,"forks_count":53,"subscribers_count":248,"default_branch":"master","last_synced_at":"2025-04-06T03:31:48.829Z","etag":null,"topics":["hashicorp","terraform","tutorial"],"latest_commit_sha":null,"homepage":"https://learn.hashicorp.com/tutorials/terraform/state-import","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp-education.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-14T21:15:50.000Z","updated_at":"2025-02-25T15:04:03.000Z","dependencies_parsed_at":"2024-05-03T20:26:05.106Z","dependency_job_id":"f564f252-417d-4645-baa5-e6553701ca86","html_url":"https://github.com/hashicorp-education/learn-terraform-import","commit_stats":{"total_commits":32,"total_committers":11,"mean_commits":2.909090909090909,"dds":0.5,"last_synced_commit":"1fdb24efa9f0bbedb4b011f0081372b7edb92c22"},"previous_names":["hashicorp-education/learn-terraform-import","hashicorp/learn-terraform-import"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hashicorp-education/learn-terraform-import","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp-education%2Flearn-terraform-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp-education%2Flearn-terraform-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp-education%2Flearn-terraform-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp-education%2Flearn-terraform-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp-education","download_url":"https://codeload.github.com/hashicorp-education/learn-terraform-import/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp-education%2Flearn-terraform-import/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266152026,"owners_count":23884464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hashicorp","terraform","tutorial"],"created_at":"2024-11-30T18:01:10.445Z","updated_at":"2025-07-24T01:31:48.841Z","avatar_url":"https://github.com/hashicorp-education.png","language":"HCL","funding_links":[],"categories":["HCL"],"sub_categories":[],"readme":"## Learn Terraform Import\n\nLearn how to import existing resources under Terraform's management.\n\nFollow along with the [Learn Terraform Import tutorial](https://developer.hashicorp.com/terraform/tutorials/state/state-import).\n\n### Install prerequisites\n\n1. Terraform: https://www.terraform.io/downloads.html\n1. Docker: https://docs.docker.com/get-docker/\n\n### Create a docker container\n\n1. Run this docker command to create a container with the latest nginx image.\n\n    ```shell\n    docker run --name hashicorp-learn --detach --publish 8080:80 nginx:latest\n    ```\n\n1. Verify container is running by running `docker ps` or visiting `0.0.0.0:8080`\n    in your web browser.\n\n    ```shell\n    docker ps --filter \"name=hashicorp-learn\"\n    ```\n\n### Import container resource\n\n1. Initialize your workspace by running `terraform init`.\n\n1. Add empty resource stub to `docker.tf` for the container.\n\n    ```hcl\n    resource \"docker_container\" \"web\" { }\n    ```\n\n1. Import the container into Terraform state.\n\n    ```shell\n    terraform import docker_container.web $(docker inspect -f {{.ID}} hashicorp-learn)\n    ```\n\n1. Now the container is in your terraform configuration's state.\n\n    ```shell\n    terraform show\n    ```\n\n1. Run `terraform plan`. Terraform shows errors for missing required arguments\n    (`image`, `name`).\n\n    ```shell\n    terraform plan\n    ```\n\n1. Generate configuration and save it in `docker.tf`, replacing the empty\n    resource created earlier.\n\n    ```shell\n    terraform show -no-color \u003e docker.tf\n    ```\n\n1. Re-run `terraform plan`.\n    ```shell\n    terraform plan\n    ```\n\n1. Terraform will show warnings and errors about a deprecated attribute\n    (`links`), and several read-only attributes (`ip_address`, `network_data`,\n    `gateway`, `ip_prefix_length`, `id`). Remove these attributes from `docker.tf`.\n\n1. Re-run `terraform plan`.\n\n    ```shell\n    terraform plan\n    ```\n\n    It should now execute successfully. The plan indicates that Terraform will\n    update in place to add the `attach`, `logs`, `must_run`, and `start`\n    attributes. Notice that the container resource will not be replaced.\n\n1. Apply the changes. Remember to confirm the run with a `yes`.\n\n    ```shell\n    terraform apply\n    ```\n\n1. There are now several attributes in `docker.tf` that are unnecessary because\n    they are the same as their default values. After removing these attributes,\n    `docker.tf` will look something like the following.\n\n    ```hcl\n    # docker_container.web:\n    resource \"docker_container\" \"web\" {\n       name  = \"hashicorp-learn\"\n       image = \"sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9\"\n\n       ports {\n           external = 8080\n           internal = 80\n       }\n    }\n    ```\n\n1. Run `terraform plan` again to verify that removing these attributes did not\n    change the configuration.\n\n    ```shell\n    terraform plan\n    ```\n\n### Verify that your infrastructure still works as expected\n\n```shell\n$ docker ps --filter \"name=hashicorp-learn\"\n```\n\n    You can revisit `0.0.0.0:8080` in your web browser to verify that it is\n    still up. Also note the \"Status\" - the container has been up and running\n    since it was created, so you know that it was not restarted when you\n    imported it into Terraform.\n\n### Create a Docker image resource\n\n1. Retrieve the image's tag name by running the following command, replacing the\n    sha256 value shown with the one from `docker.tf`.\n\n    ```shell\n    docker image inspect sha256:602e111c06b6934013578ad80554a074049c59441d9bcd963cb4a7feccede7a -f {{.RepoTags}}\n    ```\n\n1. Add the following configuration to your docker.tf file.\n\n    ```hcl\n    resource \"docker_image\" \"nginx\" {\n      name = \"nginx:latest\"\n    }\n    ```\n\n1. Run `terraform apply` to apply the changes. Remember to confirm the run with\n    a `yes`.\n\n    ```shell\n    terraform apply\n    ```\n\n1. Now that Terraform has created a resource for the image, refer to it in\n    `docker.tf` like so:\n\n    ```hcl\n    resource \"docker_container\" \"web\" {\n      name  = \"hashicorp-learn\"\n      image = docker_image.nginx.latest\n\n    # File truncated...\n    ```\n\n1. Verify that your configuration matches the current state.\n\n    ```shell\n    terraform apply\n    ```\n\n### Manage the container with Terraform\n\n1. In your `docker.tf` file, change the container's external port from `8080` to\n    `8081`.\n\n    ```hcl\n    resource \"docker_container\" \"web\" {\n      name  = \"hashicorp-learn\"\n      image = \"sha256:602e111c06b6934013578ad80554a074049c59441d9bcd963cb4a7feccede7a5\"\n\n      ports {\n        external = 8081\n        internal = 80\n      }\n    }\n    ```\n\n1. Apply the change. Remember to confirm the run with a `yes`.\n\n    ```shell\n    terraform apply\n    ```\n\n1. Verify that the new container works by running `docker ps` or visiting\n    `0.0.0.0:8081` in your web browser.\n\n    ```shell\n    docker ps --filter \"name=hashicorp-learn\"\n    ```\n\n### Destroy resources\n\n1. Run `terraform destroy` to destroy the container. Remember to confirm\n    destruction with a `yes`.\n\n    ```shell\n    terraform destroy\n    ```\n\n1. Run `docker ps` to validate that the container was destroyed.\n\n    ```shell\n    docker ps --filter \"name=hashicorp-learn\"\n    ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp-education%2Flearn-terraform-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp-education%2Flearn-terraform-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp-education%2Flearn-terraform-import/lists"}