GithubHelp home page GithubHelp logo

hashicorp / learn-terraform-import Goto Github PK

View Code? Open in Web Editor NEW
53.0 258.0 48.0 40 KB

Learn how to import existing configuration into Terraform

Home Page: https://learn.hashicorp.com/tutorials/terraform/state-import

License: Mozilla Public License 2.0

HCL 100.00%
terraform hashicorp tutorial

learn-terraform-import's Introduction

Learn Terraform Import

Learn how to import existing resources under Terraform's management.

Follow along with the Learn Terraform Import tutorial.

Install prerequisites

  1. Terraform: https://www.terraform.io/downloads.html
  2. Docker: https://docs.docker.com/get-docker/

Create a docker container

  1. Run this docker command to create a container with the latest nginx image.

    docker run --name hashicorp-learn --detach --publish 8080:80 nginx:latest
  2. Verify container is running by running docker ps or visiting 0.0.0.0:8080 in your web browser.

    docker ps --filter "name=hashicorp-learn"

Import container resource

  1. Initialize your workspace by running terraform init.

  2. Add empty resource stub to docker.tf for the container.

    resource "docker_container" "web" { }
  3. Import the container into Terraform state.

    terraform import docker_container.web $(docker inspect -f {{.ID}} hashicorp-learn)
  4. Now the container is in your terraform configuration's state.

    terraform show
  5. Run terraform plan. Terraform shows errors for missing required arguments (image, name).

    terraform plan
  6. Generate configuration and save it in docker.tf, replacing the empty resource created earlier.

    terraform show -no-color > docker.tf
  7. Re-run terraform plan.

    terraform plan
  8. Terraform will show warnings and errors about a deprecated attribute (links), and several read-only attributes (ip_address, network_data, gateway, ip_prefix_length, id). Remove these attributes from docker.tf.

  9. Re-run terraform plan.

    terraform plan

    It should now execute successfully. The plan indicates that Terraform will update in place to add the attach, logs, must_run, and start attributes. Notice that the container resource will not be replaced.

  10. Apply the changes. Remember to confirm the run with a yes.

    terraform apply
  11. There are now several attributes in docker.tf that are unnecessary because they are the same as their default values. After removing these attributes, docker.tf will look something like the following.

    # docker_container.web:
    resource "docker_container" "web" {
       name  = "hashicorp-learn"
       image = "sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9"
    
       ports {
           external = 8080
           internal = 80
       }
    }
  12. Run terraform plan again to verify that removing these attributes did not change the configuration.

    terraform plan

Verify that your infrastructure still works as expected

$ docker ps --filter "name=hashicorp-learn"
You can revisit `0.0.0.0:8080` in your web browser to verify that it is
still up. Also note the "Status" - the container has been up and running
since it was created, so you know that it was not restarted when you
imported it into Terraform.

Create a Docker image resource

  1. Retrieve the image's tag name by running the following command, replacing the sha256 value shown with the one from docker.tf.

    docker image inspect sha256:602e111c06b6934013578ad80554a074049c59441d9bcd963cb4a7feccede7a -f {{.RepoTags}}
  2. Add the following configuration to your docker.tf file.

    resource "docker_image" "nginx" {
      name = "nginx:latest"
    }
  3. Run terraform apply to apply the changes. Remember to confirm the run with a yes.

    terraform apply
  4. Now that Terraform has created a resource for the image, refer to it in docker.tf like so:

    resource "docker_container" "web" {
      name  = "hashicorp-learn"
      image = docker_image.nginx.latest
    
    # File truncated...
  5. Verify that your configuration matches the current state.

    terraform apply

Manage the container with Terraform

  1. In your docker.tf file, change the container's external port from 8080 to 8081.

    resource "docker_container" "web" {
      name  = "hashicorp-learn"
      image = "sha256:602e111c06b6934013578ad80554a074049c59441d9bcd963cb4a7feccede7a5"
    
      ports {
        external = 8081
        internal = 80
      }
    }
  2. Apply the change. Remember to confirm the run with a yes.

    terraform apply
  3. Verify that the new container works by running docker ps or visiting 0.0.0.0:8081 in your web browser.

    docker ps --filter "name=hashicorp-learn"

Destroy resources

  1. Run terraform destroy to destroy the container. Remember to confirm destruction with a yes.

    terraform destroy
  2. Run docker ps to validate that the container was destroyed.

    docker ps --filter "name=hashicorp-learn"

learn-terraform-import's People

Contributors

brianmmcclain avatar duplo83 avatar franco-camba avatar hashicorp-copywrite[bot] avatar im2nguyen avatar judithpatudith avatar ritsok avatar rkoron007 avatar robin-norwood avatar swagatpanda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-terraform-import's Issues

step 3 meet followed issue

root@ip-192-168-0-59:~/learn-terraform-import# terraform import docker_container.web $(docker inspect -f {{.ID}} hashicorp-learn)

│ Error: Error initializing Docker client: protocol not available

│   with provider["registry.terraform.io/kreuzwerker/docker"],
│   on /root/learn-terraform-import/main.tf line 14, in provider "docker":
│   14: provider "docker" {



root@ip-192-168-0-59:~/learn-terraform-import# docker --version
Docker version 20.10.8, build 3967b7d
root@ip-192-168-0-59:~/learn-terraform-import# terraform --version
Terraform v1.0.6
on linux_amd64
+ provider registry.terraform.io/kreuzwerker/docker v2.11.0

Your version of Terraform is out of date! The latest version
is 1.0.8. You can update by downloading from https://www.terraform.io/downloads.html
root@ip-192-168-0-59:~/learn-terraform-import#

learn-terraform-import doesn't work with terraform 0.13

The terraform v0.13 needs explicit provider source locations.

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/docker versions matching "2.7.*"...

Error: Failed to install providers

Could not find required providers, but found possible alternatives:

  hashicorp/docker -> terraform-providers/docker

If these suggestions look correct, upgrade your configuration with the
following command:
    terraform 0.13upgrade .

Terraform init doesn't work for terraform versions > 0.x.x

Running terraform init after cloning the repository fails with terraform versions greater than 0.x.x

This is easily fixed by modifying the required_version in main.tf:

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.11"
    }
  }
- required_version = "~> 0.14"
+ required_version = ">=0.14"
}

But perhaps the source file or tutorial instructions should be updated to reflect this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.