In this article, we explore the concept of remote state in Terraform, explaining its benefits for team collaboration and enhanced infrastructure security. Previously, we discussed Terraform state files with a focus on local state stored alongside your configuration files. Local state files map your Terraform configuration to real-world infrastructure and track metadata, such as dependencies, which ensures that resources are created and deleted in the correct order. Although local state files can improve performance by disabling state refreshes in large organizations with multiple cloud providers, they pose risks when collaborating in teams. Consider the following directory listing that shows a typical configuration with a local state file:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Local state files are stored on the client machine and should not be used when working in a team environment. Additionally, avoid storing them in version control systems like GitHub as they may contain sensitive infrastructure details.


Configuring Remote State with AWS S3
Let’s review how to configure remote state using AWS S3 as a backend. To achieve this, you will need an existing S3 bucket for storing the Terraform state and, optionally, a DynamoDB table for state locking. (Creating these resources is outside the scope of this article. For further details, check out the Terraform Basics Training Course, which includes practical labs.) The S3 backend configuration requires:- a bucket name (an existing S3 bucket),
- a key (the S3 object path to store the state file),
- the region where the bucket is located,
- and optionally, a DynamoDB table for state locking.
terraform apply without initializing the new backend will result in an error. This is because Terraform must be initialized with the new backend configuration.
To initialize the remote backend, run the following command:
Pre-existing state was found while migrating the previous “local” backend to the newly configured “s3” backend. No existing state was found in the newly configured “s3” backend. Do you want to copy this state to the new “s3” backend? Enter “yes” to copy and “no” to start with an empty state.Type “yes” to copy the existing state file to the S3 bucket. Once initialization is complete, you can safely remove the local state file from your directory. Subsequent runs of
terraform plan or terraform apply will automatically manage state locking, download the state from the S3 bucket, and upload the updated state after operations conclude. For example:
