github.com/hugorut/terraform@v1.1.3/website/docs/language/settings/backends/s3.mdx (about)

     1  ---
     2  page_title: 'Backend Type: s3'
     3  description: Terraform can store state remotely in S3 and lock that state with DynamoDB.
     4  ---
     5  
     6  # S3
     7  
     8  Stores the state as a given key in a given bucket on
     9  [Amazon S3](https://aws.amazon.com/s3/).
    10  This backend also supports state locking and consistency checking via
    11  [Dynamo DB](https://aws.amazon.com/dynamodb/), which can be enabled by setting
    12  the `dynamodb_table` field to an existing DynamoDB table name.
    13  A single DynamoDB table can be used to lock multiple remote state files. Terraform generates key names that include the values of the `bucket` and `key` variables.
    14  
    15  ~> **Warning!** It is highly recommended that you enable
    16  [Bucket Versioning](https://docs.aws.amazon.com/AmazonS3/latest/userguide/manage-versioning-examples.html)
    17  on the S3 bucket to allow for state recovery in the case of accidental deletions and human error.
    18  
    19  ## Example Configuration
    20  
    21  ```hcl
    22  terraform {
    23    backend "s3" {
    24      bucket = "mybucket"
    25      key    = "path/to/my/key"
    26      region = "us-east-1"
    27    }
    28  }
    29  ```
    30  
    31  This assumes we have a bucket created called `mybucket`. The
    32  Terraform state is written to the key `path/to/my/key`.
    33  
    34  Note that for the access credentials we recommend using a
    35  [partial configuration](/language/settings/backends/configuration#partial-configuration).
    36  
    37  ### S3 Bucket Permissions
    38  
    39  Terraform will need the following AWS IAM permissions on
    40  the target backend bucket:
    41  
    42  * `s3:ListBucket` on `arn:aws:s3:::mybucket`
    43  * `s3:GetObject` on `arn:aws:s3:::mybucket/path/to/my/key`
    44  * `s3:PutObject` on `arn:aws:s3:::mybucket/path/to/my/key`
    45  * `s3:DeleteObject` on `arn:aws:s3:::mybucket/path/to/my/key`
    46  
    47  This is seen in the following AWS IAM Statement:
    48  
    49  ```json
    50  {
    51    "Version": "2012-10-17",
    52    "Statement": [
    53      {
    54        "Effect": "Allow",
    55        "Action": "s3:ListBucket",
    56        "Resource": "arn:aws:s3:::mybucket"
    57      },
    58      {
    59        "Effect": "Allow",
    60        "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
    61        "Resource": "arn:aws:s3:::mybucket/path/to/my/key"
    62      }
    63    ]
    64  }
    65  ```
    66  
    67  -> **Note:** AWS can control access to S3 buckets with either IAM policies
    68  attached to users/groups/roles (like the example above) or resource policies
    69  attached to bucket objects (which look similar but also require a `Principal` to
    70  indicate which entity has those permissions). For more details, see Amazon's
    71  documentation about
    72  [S3 access control](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-access-control.html).
    73  
    74  ### DynamoDB Table Permissions
    75  
    76  If you are using state locking, Terraform will need the following AWS IAM
    77  permissions on the DynamoDB table (`arn:aws:dynamodb:::table/mytable`):
    78  
    79  * `dynamodb:GetItem`
    80  * `dynamodb:PutItem`
    81  * `dynamodb:DeleteItem`
    82  
    83  This is seen in the following AWS IAM Statement:
    84  
    85  ```json
    86  {
    87    "Version": "2012-10-17",
    88    "Statement": [
    89      {
    90        "Effect": "Allow",
    91        "Action": [
    92          "dynamodb:GetItem",
    93          "dynamodb:PutItem",
    94          "dynamodb:DeleteItem"
    95        ],
    96        "Resource": "arn:aws:dynamodb:*:*:table/mytable"
    97      }
    98    ]
    99  }
   100  ```
   101  
   102  ## Data Source Configuration
   103  
   104  To make use of the S3 remote state in another configuration, use the
   105  [`terraform_remote_state` data
   106  source](/language/state/remote-state-data).
   107  
   108  ```hcl
   109  data "terraform_remote_state" "network" {
   110    backend = "s3"
   111    config = {
   112      bucket = "terraform-state-prod"
   113      key    = "network/terraform.tfstate"
   114      region = "us-east-1"
   115    }
   116  }
   117  ```
   118  
   119  The `terraform_remote_state` data source will return all of the root module
   120  outputs defined in the referenced remote state (but not any outputs from
   121  nested modules unless they are explicitly output again in the root). An
   122  example output might look like:
   123  
   124  ```
   125  data.terraform_remote_state.network:
   126    id = 2016-10-29 01:57:59.780010914 +0000 UTC
   127    addresses.# = 2
   128    addresses.0 = 52.207.220.222
   129    addresses.1 = 54.196.78.166
   130    backend = s3
   131    config.% = 3
   132    config.bucket = terraform-state-prod
   133    config.key = network/terraform.tfstate
   134    config.region = us-east-1
   135    elb_address = web-elb-790251200.us-east-1.elb.amazonaws.com
   136    public_subnet_id = subnet-1e05dd33
   137  ```
   138  
   139  ## Configuration
   140  
   141  This backend requires the configuration of the AWS Region and S3 state storage. Other configuration, such as enabling DynamoDB state locking, is optional.
   142  
   143  ### Credentials and Shared Configuration
   144  
   145  The following configuration is required:
   146  
   147  * `region` - (Required) AWS Region of the S3 Bucket and DynamoDB Table (if used). This can also be sourced from the `AWS_DEFAULT_REGION` and `AWS_REGION` environment variables.
   148  
   149  The following configuration is optional:
   150  
   151  * `access_key` - (Optional) AWS access key. If configured, must also configure `secret_key`. This can also be sourced from the `AWS_ACCESS_KEY_ID` environment variable, AWS shared credentials file (e.g. `~/.aws/credentials`), or AWS shared configuration file (e.g. `~/.aws/config`).
   152  * `secret_key` - (Optional) AWS access key. If configured, must also configure `access_key`. This can also be sourced from the `AWS_SECRET_ACCESS_KEY` environment variable, AWS shared credentials file (e.g. `~/.aws/credentials`), or AWS shared configuration file (e.g. `~/.aws/config`).
   153  * `iam_endpoint` - (Optional) Custom endpoint for the AWS Identity and Access Management (IAM) API. This can also be sourced from the `AWS_IAM_ENDPOINT` environment variable.
   154  * `max_retries` - (Optional) The maximum number of times an AWS API request is retried on retryable failure. Defaults to 5.
   155  * `profile` - (Optional) Name of AWS profile in AWS shared credentials file (e.g. `~/.aws/credentials`) or AWS shared configuration file (e.g. `~/.aws/config`) to use for credentials and/or configuration. This can also be sourced from the `AWS_PROFILE` environment variable.
   156  * `shared_credentials_file`  - (Optional) Path to the AWS shared credentials file. Defaults to `~/.aws/credentials`.
   157  * `skip_credentials_validation` - (Optional) Skip credentials validation via the STS API.
   158  * `skip_region_validation` - (Optional) Skip validation of provided region name.
   159  * `skip_metadata_api_check` - (Optional) Skip usage of EC2 Metadata API.
   160  * `sts_endpoint` - (Optional) Custom endpoint for the AWS Security Token Service (STS) API. This can also be sourced from the `AWS_STS_ENDPOINT` environment variable.
   161  * `token` - (Optional) Multi-Factor Authentication (MFA) token. This can also be sourced from the `AWS_SESSION_TOKEN` environment variable.
   162  
   163  #### Assume Role Configuration
   164  
   165  The following configuration is optional:
   166  
   167  * `assume_role_duration_seconds` - (Optional) Number of seconds to restrict the assume role session duration.
   168  * `assume_role_policy` - (Optional) IAM Policy JSON describing further restricting permissions for the IAM Role being assumed.
   169  * `assume_role_policy_arns` - (Optional) Set of Amazon Resource Names (ARNs) of IAM Policies describing further restricting permissions for the IAM Role being assumed.
   170  * `assume_role_tags` - (Optional) Map of assume role session tags.
   171  * `assume_role_transitive_tag_keys` - (Optional) Set of assume role session tag keys to pass to any subsequent sessions.
   172  * `external_id` - (Optional) External identifier to use when assuming the role.
   173  * `role_arn` - (Optional) Amazon Resource Name (ARN) of the IAM Role to assume.
   174  * `session_name` - (Optional) Session name to use when assuming the role.
   175  
   176  ### S3 State Storage
   177  
   178  The following configuration is required:
   179  
   180  * `bucket` - (Required) Name of the S3 Bucket.
   181  * `key` - (Required) Path to the state file inside the S3 Bucket. When using a non-default [workspace](/language/state/workspaces), the state path will be `/workspace_key_prefix/workspace_name/key` (see also the `workspace_key_prefix` configuration).
   182  
   183  The following configuration is optional:
   184  
   185  * `acl` - (Optional) [Canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl) to be applied to the state file.
   186  * `encrypt` - (Optional) Enable [server side encryption](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingServerSideEncryption.html) of the state file.
   187  * `endpoint` - (Optional) Custom endpoint for the AWS S3 API. This can also be sourced from the `AWS_S3_ENDPOINT` environment variable.
   188  * `force_path_style` - (Optional) Enable path-style S3 URLs (`https://<HOST>/<BUCKET>` instead of `https://<BUCKET>.<HOST>`).
   189  * `kms_key_id` - (Optional) Amazon Resource Name (ARN) of a Key Management Service (KMS) Key to use for encrypting the state. Note that if this value is specified, Terraform will need `kms:Encrypt`, `kms:Decrypt` and `kms:GenerateDataKey` permissions on this KMS key.
   190  * `sse_customer_key` - (Optional) The key to use for encrypting state with [Server-Side Encryption with Customer-Provided Keys (SSE-C)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ServerSideEncryptionCustomerKeys.html). This is the base64-encoded value of the key, which must decode to 256 bits. This can also be sourced from the `AWS_SSE_CUSTOMER_KEY` environment variable, which is recommended due to the sensitivity of the value. Setting it inside a terraform file will cause it to be persisted to disk in `terraform.tfstate`.
   191  * `workspace_key_prefix` - (Optional) Prefix applied to the state path inside the bucket. This is only relevant when using a non-default workspace. Defaults to `env:`.
   192  
   193  ### DynamoDB State Locking
   194  
   195  The following configuration is optional:
   196  
   197  * `dynamodb_endpoint` - (Optional) Custom endpoint for the AWS DynamoDB API. This can also be sourced from the `AWS_DYNAMODB_ENDPOINT` environment variable.
   198  * `dynamodb_table` - (Optional) Name of DynamoDB Table to use for state locking and consistency. The table must have a partition key named `LockID` with type of `String`. If not configured, state locking will be disabled.
   199  
   200  ## Multi-account AWS Architecture
   201  
   202  A common architectural pattern is for an organization to use a number of
   203  separate AWS accounts to isolate different teams and environments. For example,
   204  a "staging" system will often be deployed into a separate AWS account than
   205  its corresponding "production" system, to minimize the risk of the staging
   206  environment affecting production infrastructure, whether via rate limiting,
   207  misconfigured access controls, or other unintended interactions.
   208  
   209  The S3 backend can be used in a number of different ways that make different
   210  tradeoffs between convenience, security, and isolation in such an organization.
   211  This section describes one such approach that aims to find a good compromise
   212  between these tradeoffs, allowing use of
   213  [Terraform's workspaces feature](/language/state/workspaces) to switch
   214  conveniently between multiple isolated deployments of the same configuration.
   215  
   216  Use this section as a starting-point for your approach, but note that
   217  you will probably need to make adjustments for the unique standards and
   218  regulations that apply to your organization. You will also need to make some
   219  adjustments to this approach to account for _existing_ practices within your
   220  organization, if for example other tools have previously been used to manage
   221  infrastructure.
   222  
   223  Terraform is an administrative tool that manages your infrastructure, and so
   224  ideally the infrastructure that is used by Terraform should exist outside of
   225  the infrastructure that Terraform manages. This can be achieved by creating a
   226  separate _administrative_ AWS account which contains the user accounts used by
   227  human operators and any infrastructure and tools used to manage the other
   228  accounts. Isolating shared administrative tools from your main environments
   229  has a number of advantages, such as avoiding accidentally damaging the
   230  administrative infrastructure while changing the target infrastructure, and
   231  reducing the risk that an attacker might abuse production infrastructure to
   232  gain access to the (usually more privileged) administrative infrastructure.
   233  
   234  ### Administrative Account Setup
   235  
   236  Your administrative AWS account will contain at least the following items:
   237  
   238  * One or more [IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users.html)
   239    for system administrators that will log in to maintain infrastructure in
   240    the other accounts.
   241  * Optionally, one or more [IAM groups](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_groups.html)
   242    to differentiate between different groups of users that have different
   243    levels of access to the other AWS accounts.
   244  * An [S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingBucket.html)
   245    that will contain the Terraform state files for each workspace.
   246  * A [DynamoDB table](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.TablesItemsAttributes)
   247    that will be used for locking to prevent concurrent operations on a single
   248    workspace.
   249  
   250  Provide the S3 bucket name and DynamoDB table name to Terraform within the
   251  S3 backend configuration using the `bucket` and `dynamodb_table` arguments
   252  respectively, and configure a suitable `workspace_key_prefix` to contain
   253  the states of the various workspaces that will subsequently be created for
   254  this configuration.
   255  
   256  ### Environment Account Setup
   257  
   258  For the sake of this section, the term "environment account" refers to one
   259  of the accounts whose contents are managed by Terraform, separate from the
   260  administrative account described above.
   261  
   262  Your environment accounts will eventually contain your own product-specific
   263  infrastructure. Along with this it must contain one or more
   264  [IAM roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)
   265  that grant sufficient access for Terraform to perform the desired management
   266  tasks.
   267  
   268  ### Delegating Access
   269  
   270  Each Administrator will run Terraform using credentials for their IAM user
   271  in the administrative account.
   272  [IAM Role Delegation](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html)
   273  is used to grant these users access to the roles created in each environment
   274  account.
   275  
   276  Full details on role delegation are covered in the AWS documentation linked
   277  above. The most important details are:
   278  
   279  * Each role's _Assume Role Policy_ must grant access to the administrative AWS
   280    account, which creates a trust relationship with the administrative AWS
   281    account so that its users may assume the role.
   282  * The users or groups within the administrative account must also have a
   283    policy that creates the converse relationship, allowing these users or groups
   284    to assume that role.
   285  
   286  Since the purpose of the administrative account is only to host tools for
   287  managing other accounts, it is useful to give the administrative accounts
   288  restricted access only to the specific operations needed to assume the
   289  environment account role and access the Terraform state. By blocking all
   290  other access, you remove the risk that user error will lead to staging or
   291  production resources being created in the administrative account by mistake.
   292  
   293  When configuring Terraform, use either environment variables or the standard
   294  credentials file `~/.aws/credentials` to provide the administrator user's
   295  IAM credentials within the administrative account to both the S3 backend _and_
   296  to Terraform's AWS provider.
   297  
   298  Use conditional configuration to pass a different `assume_role` value to
   299  the AWS provider depending on the selected workspace. For example:
   300  
   301  ```hcl
   302  variable "workspace_iam_roles" {
   303    default = {
   304      staging    = "arn:aws:iam::STAGING-ACCOUNT-ID:role/Terraform"
   305      production = "arn:aws:iam::PRODUCTION-ACCOUNT-ID:role/Terraform"
   306    }
   307  }
   308  
   309  provider "aws" {
   310    # No credentials explicitly set here because they come from either the
   311    # environment or the global credentials file.
   312  
   313    assume_role = {
   314      role_arn = "${var.workspace_iam_roles[terraform.workspace]}"
   315    }
   316  }
   317  ```
   318  
   319  If workspace IAM roles are centrally managed and shared across many separate
   320  Terraform configurations, the role ARNs could also be obtained via a data
   321  source such as [`terraform_remote_state`](/language/state/remote-state-data)
   322  to avoid repeating these values.
   323  
   324  ### Creating and Selecting Workspaces
   325  
   326  With the necessary objects created and the backend configured, run
   327  `terraform init` to initialize the backend and establish an initial workspace
   328  called "default". This workspace will not be used, but is created automatically
   329  by Terraform as a convenience for users who are not using the workspaces
   330  feature.
   331  
   332  Create a workspace corresponding to each key given in the `workspace_iam_roles`
   333  variable value above:
   334  
   335  ```
   336  $ terraform workspace new staging
   337  Created and switched to workspace "staging"!
   338  
   339  ...
   340  
   341  $ terraform workspace new production
   342  Created and switched to workspace "production"!
   343  
   344  ...
   345  ```
   346  
   347  Due to the `assume_role` setting in the AWS provider configuration, any
   348  management operations for AWS resources will be performed via the configured
   349  role in the appropriate environment AWS account. The backend operations, such
   350  as reading and writing the state from S3, will be performed directly as the
   351  administrator's own user within the administrative account.
   352  
   353  ```
   354  $ terraform workspace select staging
   355  $ terraform apply
   356  ...
   357  ```
   358  
   359  ### Running Terraform in Amazon EC2
   360  
   361  Teams that make extensive use of Terraform for infrastructure management
   362  often [run 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)
   363  to ensure a consistent operating environment and to limit access to the
   364  various secrets and other sensitive information that Terraform configurations
   365  tend to require.
   366  
   367  When running Terraform in an automation tool running on an Amazon EC2 instance,
   368  consider running this instance in the administrative account and using an
   369  [instance profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html)
   370  in place of the various administrator IAM users suggested above. An IAM
   371  instance profile can also be granted cross-account delegation access via
   372  an IAM policy, giving this instance the access it needs to run Terraform.
   373  
   374  To isolate access to different environment accounts, use a separate EC2
   375  instance for each target account so that its access can be limited only to
   376  the single account.
   377  
   378  Similar approaches can be taken with equivalent features in other AWS compute
   379  services, such as ECS.
   380  
   381  ### Protecting Access to Workspace State
   382  
   383  In a simple implementation of the pattern described in the prior sections,
   384  all users have access to read and write states for all workspaces. In many
   385  cases it is desirable to apply more precise access constraints to the
   386  Terraform state objects in S3, so that for example only trusted administrators
   387  are allowed to modify the production state, or to control _reading_ of a state
   388  that contains sensitive information.
   389  
   390  Amazon S3 supports fine-grained access control on a per-object-path basis
   391  using IAM policy. A full description of S3's access control mechanism is
   392  beyond the scope of this guide, but an example IAM policy granting access
   393  to only a single state object within an S3 bucket is shown below:
   394  
   395  ```json
   396  {
   397    "Version": "2012-10-17",
   398    "Statement": [
   399      {
   400        "Effect": "Allow",
   401        "Action": "s3:ListBucket",
   402        "Resource": "arn:aws:s3:::myorg-terraform-states"
   403      },
   404      {
   405        "Effect": "Allow",
   406        "Action": ["s3:GetObject", "s3:PutObject"],
   407        "Resource": "arn:aws:s3:::myorg-terraform-states/myapp/production/tfstate"
   408      }
   409    ]
   410  }
   411  ```
   412  
   413  It is not possible to apply such fine-grained access control to the DynamoDB
   414  table used for locking, so it is possible for any user with Terraform access
   415  to lock any workspace state, even if they do not have access to read or write
   416  that state. If a malicious user has such access they could block attempts to
   417  use Terraform against some or all of your workspaces as long as locking is
   418  enabled in the backend configuration.
   419  
   420  ### Configuring Custom User-Agent Information
   421  
   422  Note this feature is optional and only available in Terraform v0.13.1+.
   423  
   424  By default, the underlying AWS client used by the Terraform AWS Provider creates requests with User-Agent headers including information about Terraform and AWS Go SDK versions. To provide additional information in the User-Agent headers, the `TF_APPEND_USER_AGENT` environment variable can be set and its value will be directly added to HTTP requests. e.g.
   425  
   426  ```sh
   427  $ export TF_APPEND_USER_AGENT="JenkinsAgent/i-12345678 BuildID/1234 (Optional Extra Information)"
   428  ```