github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/init.mdx (about)

     1  ---
     2  page_title: 'Command: init'
     3  description: >-
     4    The terraform init command initializes a working directory containing
     5    configuration files and installs plugins for required providers.
     6  ---
     7  
     8  # Command: init
     9  
    10  > **Hands-on:** Try the [Terraform: Get Started](https://learn.hashicorp.com/collections/terraform/aws-get-started?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) collection on HashiCorp Learn.
    11  
    12  The `terraform init` command is used to initialize a working directory
    13  containing Terraform configuration files. This is the first command that should
    14  be run after writing a new Terraform configuration or cloning an existing one
    15  from version control. It is safe to run this command multiple times.
    16  
    17  ## Usage
    18  
    19  Usage: `terraform init [options]`
    20  
    21  This command performs several different initialization steps in order to
    22  prepare the current working directory for use with Terraform. More details on
    23  these are in the sections below, but in most cases it is not necessary to worry
    24  about these individual steps.
    25  
    26  This command is always safe to run multiple times, to bring the working
    27  directory up to date with changes in the configuration. Though subsequent runs
    28  may give errors, this command will never delete your existing configuration or
    29  state.
    30  
    31  ## General Options
    32  
    33  The following options apply to all of (or several of) the initialization steps:
    34  
    35  * `-input=true` Ask for input if necessary. If false, will error if
    36    input was required.
    37  
    38  * `-lock=false` Disable locking of state files during state-related operations.
    39  
    40  * `-lock-timeout=<duration>` Override the time Terraform will wait to acquire
    41    a state lock. The default is `0s` (zero seconds), which causes immediate
    42    failure if the lock is already held by another process.
    43  
    44  * `-no-color` Disable color codes in the command output.
    45  
    46  * `-upgrade` Opt to upgrade modules and plugins as part of their respective
    47    installation steps. See the sections below for more details.
    48  
    49  ## Copy a Source Module
    50  
    51  By default, `terraform init` assumes that the working directory already
    52  contains a configuration and will attempt to initialize that configuration.
    53  
    54  Optionally, init can be run against an empty directory with the
    55  `-from-module=MODULE-SOURCE` option, in which case the given module will be
    56  copied into the target directory before any other initialization steps are
    57  run.
    58  
    59  This special mode of operation supports two use-cases:
    60  
    61  * Given a version control source, it can serve as a shorthand for checking out
    62    a configuration from version control and then initializing the working directory
    63    for it.
    64  
    65  * If the source refers to an _example_ configuration, it can be copied into
    66    a local directory to be used as a basis for a new configuration.
    67  
    68  For routine use it is recommended to check out configuration from version
    69  control separately, using the version control system's own commands. This way
    70  it is possible to pass extra flags to the version control system when necessary,
    71  and to perform other preparation steps (such as configuration generation, or
    72  activating credentials) before running `terraform init`.
    73  
    74  ## Backend Initialization
    75  
    76  During init, the root configuration directory is consulted for
    77  [backend configuration](/language/settings/backends/configuration) and the chosen backend
    78  is initialized using the given configuration settings.
    79  
    80  Re-running init with an already-initialized backend will update the working
    81  directory to use the new backend settings. Either `-reconfigure` or
    82  `-migrate-state` must be supplied to update the backend configuration.
    83  
    84  The `-migrate-state` option will attempt to copy existing state to the new
    85  backend, and depending on what changed, may result in interactive prompts to
    86  confirm migration of workspace states.  The `-force-copy` option suppresses
    87  these prompts and answers "yes" to the migration questions. This implies
    88  `-migrate-state`.
    89  
    90  The `-reconfigure` option disregards any existing configuration, preventing
    91  migration of any existing state.
    92  
    93  To skip backend configuration, use `-backend=false`. Note that some other init
    94  steps require an initialized backend, so it is recommended to use this flag only
    95  when the working directory was already previously initialized for a particular
    96  backend.
    97  
    98  The `-backend-config=...` option can be used for
    99  [partial backend configuration](/language/settings/backends/configuration#partial-configuration),
   100  in situations where the backend settings are dynamic or sensitive and so cannot
   101  be statically specified in the configuration file.
   102  
   103  ## Child Module Installation
   104  
   105  During init, the configuration is searched for `module` blocks, and the source
   106  code for referenced [modules](/language/modules/develop) is retrieved from the locations
   107  given in their `source` arguments.
   108  
   109  Re-running init with modules already installed will install the sources for
   110  any modules that were added to configuration since the last init, but will not
   111  change any already-installed modules. Use `-upgrade` to override this behavior,
   112  updating all modules to the latest available source code.
   113  
   114  To skip child module installation, use `-get=false`. Note that some other init
   115  steps can complete only when the module tree is complete, so it's recommended
   116  to use this flag only when the working directory was already previously
   117  initialized with its child modules.
   118  
   119  ## Plugin Installation
   120  
   121  Most Terraform providers are published separately from Terraform as plugins.
   122  During init, Terraform searches the configuration for both direct and indirect
   123  references to providers and attempts to install the plugins for those providers.
   124  
   125  For providers that are published in either
   126  [the public Terraform Registry](https://registry.terraform.io/) or in a
   127  third-party provider registry, `terraform init` will automatically find,
   128  download, and install the necessary provider plugins. If you cannot or do not
   129  wish to install providers from their origin registries, you can customize how
   130  Terraform installs providers using
   131  [the provider installation settings in the CLI configuration](/cli/config/config-file#provider-installation).
   132  
   133  For more information about specifying which providers are required for each
   134  of your modules, see [Provider Requirements](/language/providers/requirements).
   135  
   136  After successful installation, Terraform writes information about the selected
   137  providers to [the dependency lock file](/language/files/dependency-lock).
   138  You should commit this file to your version control system to ensure that
   139  when you run `terraform init` again in future Terraform will select exactly
   140  the same provider versions. Use the `-upgrade` option if you want Terraform
   141  to ignore the dependency lock file and consider installing newer versions.
   142  
   143  You can modify `terraform init`'s plugin behavior with the following options:
   144  
   145  * `-upgrade` Upgrade all previously-selected plugins to the newest version
   146    that complies with the configuration's version constraints. This will
   147    cause Terraform to ignore any selections recorded in the dependency lock
   148    file, and to take the newest available version matching the configured
   149    version constraints.
   150  * `-get-plugins=false` — Skip plugin installation.
   151  
   152    -> Note: Since Terraform 0.13, this option has been superseded by the
   153    [`provider_installation`](/cli/config/config-file#provider-installation) and
   154    [`plugin_cache_dir`](/cli/config/config-file#plugin_cache_dir) settings.
   155    It should not be used in Terraform versions 0.13+, and this option
   156    was removed in Terraform 0.15.
   157  * `-plugin-dir=PATH` — Force plugin installation to read plugins _only_ from
   158    the specified directory, as if it had been configured as a `filesystem_mirror`
   159    in the CLI configuration. If you intend to routinely use a particular
   160    filesystem mirror then we recommend
   161    [configuring Terraform's installation methods globally](/cli/config/config-file#provider-installation).
   162    You can use `-plugin-dir` as a one-time override for exceptional situations,
   163    such as if you are testing a local build of a provider plugin you are
   164    currently developing.
   165  * `-lockfile=MODE` Set a dependency lockfile mode.
   166  
   167  The valid values for the lockfile mode are as follows:
   168  
   169  * readonly: suppress the lockfile changes, but verify checksums against the
   170    information already recorded. It conflicts with the `-upgrade` flag. If you
   171    update the lockfile with third-party dependency management tools, it would be
   172    useful to control when it changes explicitly.
   173  
   174  ## Running `terraform init` in automation
   175  
   176  For teams that use Terraform as a key part of a change management and
   177  deployment pipeline, it can be desirable to orchestrate Terraform runs in some
   178  sort of automation in order to ensure consistency between runs, and provide
   179  other interesting features such as integration with version control hooks.
   180  
   181  There are some special concerns when running `init` in such an environment,
   182  including optionally making plugins available locally to avoid repeated
   183  re-installation. For more information, see
   184  the [Running Terraform in Automation](https://learn.hashicorp.com/tutorials/terraform/automate-terraform?in=terraform/automation&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn.
   185  
   186  ## Passing a Different Configuration Directory
   187  
   188  Terraform v0.13 and earlier also accepted a directory path in place of the
   189  plan file argument to `terraform apply`, in which case Terraform would use
   190  that directory as the root module instead of the current working directory.
   191  
   192  That usage is still supported in Terraform v0.14, but is now deprecated and we
   193  plan to remove it in Terraform v0.15. If your workflow relies on overriding
   194  the root module directory, use
   195  [the `-chdir` global option](/cli/commands/#switching-working-directory-with-chdir)
   196  instead, which works across all commands and makes Terraform consistently look
   197  in the given directory for all files it would normally read or write in the
   198  current working directory.
   199  
   200  If your previous use of this legacy pattern was also relying on Terraform
   201  writing the `.terraform` subdirectory into the current working directory even
   202  though the root module directory was overridden, use
   203  [the `TF_DATA_DIR` environment variable](/cli/config/environment-variables#tf_data_dir)
   204  to direct Terraform to write the `.terraform` directory to a location other
   205  than the current working directory.