|
1 |
| -# terraform-aws-state-backend |
| 1 | +# terraform-aws-tfstate-backend [](https://travis-ci.org/cloudposse/terraform-aws-tfstate-backend) |
| 2 | + |
| 3 | +Terraform module to provision an S3 bucket to store `terraform.tfstate` file and a DynamoDB table to lock the state file |
| 4 | +to prevent concurrent modifications and state corruption. |
| 5 | + |
| 6 | +The module supports the following: |
| 7 | + |
| 8 | +1. Forced server-side encryption at rest for the S3 bucket |
| 9 | +2. S3 bucket versioning to allow for Terraform state recovery in the case of accidental deletions and human errors |
| 10 | +3. State locking and consistency checking via DynamoDB table to prevent concurrent operations |
| 11 | +4. DynamoDB server-side encryption |
| 12 | + |
| 13 | +https://www.terraform.io/docs/backends/types/s3.html |
| 14 | + |
| 15 | + |
| 16 | +__NOTE:__ The operators of the module (IAM Users) must have permissions to create S3 buckets and DynamoDB tables when performing `terraform plan` and `terraform apply` |
| 17 | + |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```hcl |
| 22 | +terraform { |
| 23 | + required_version = ">= 0.11.3" |
| 24 | +} |
| 25 | +
|
| 26 | +module "terraform_state_backend" { |
| 27 | + source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=master" |
| 28 | + namespace = "cp" |
| 29 | + stage = "dev" |
| 30 | + name = "app" |
| 31 | + region = "us-east-1" |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +__NOTE:__ First create the bucket and table without any state enabled (Terraform will use the local file system to store state). |
| 36 | +You can then import the bucket and table by using [`terraform import`](https://www.terraform.io/docs/import/index.html) and store the state file into the bucket. |
| 37 | + |
| 38 | +Once the bucket and table have been created, configure the [backend](https://www.terraform.io/docs/backends/types/s3.html) |
| 39 | + |
| 40 | +```hcl |
| 41 | +terraform { |
| 42 | + required_version = ">= 0.11.3" |
| 43 | + |
| 44 | + backend "s3" { |
| 45 | + region = "us-east-1" |
| 46 | + bucket = "< the name of the S3 bucket >" |
| 47 | + key = "terraform.tfstate" |
| 48 | + dynamodb_table = "< the name of the DynamoDB table >" |
| 49 | + encrypt = true |
| 50 | + } |
| 51 | +} |
| 52 | +
|
| 53 | +module "another_module" { |
| 54 | + source = "....." |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Initialize the backend with `terraform init`. |
| 59 | + |
| 60 | +After `terraform apply`, `terraform.tfstate` file will be stored in the bucket, |
| 61 | +and the DynamoDB table will be used to lock the state to prevent concurrent modifications. |
| 62 | + |
| 63 | +<br/> |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## Variables |
| 69 | + |
| 70 | +| Name | Default | Description | Required | |
| 71 | +|:-------------------------|:-------------|:----------------------------------------------------------------------------------|:--------:| |
| 72 | +| `namespace` | `` | Namespace (_e.g._ `cp` or `cloudposse`) | Yes | |
| 73 | +| `stage` | `` | Stage (_e.g._ `prod`, `dev`, `staging`) | Yes | |
| 74 | +| `region` | `us-east-1` | AWS Region the S3 bucket should reside in | Yes | |
| 75 | +| `name` | `terraform` | Name (_e.g._ `app`, `cluster`, or `terraform`) | No | |
| 76 | +| `attributes` | `["state"]` | Additional attributes (_e.g._ `policy` or `role`) | No | |
| 77 | +| `tags` | `{}` | Additional tags (_e.g._ `map("BusinessUnit","XYZ")` | No | |
| 78 | +| `delimiter` | `-` | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | No | |
| 79 | +| `acl` | `private` | The canned ACL to apply to the S3 bucket | No | |
| 80 | +| `read_capacity` | `5` | DynamoDB read capacity units | No | |
| 81 | +| `write_capacity` | `5` | DynamoDB write capacity units | No | |
| 82 | + |
| 83 | + |
| 84 | +## Outputs |
| 85 | + |
| 86 | +| Name | Description | |
| 87 | +|:-------------------------|:-----------------------------| |
| 88 | +| `s3_bucket_domain_name` | S3 bucket domain name | |
| 89 | +| `s3_bucket_id` | S3 bucket ID | |
| 90 | +| `s3_bucket_arn` | S3 bucket ARN | |
| 91 | +| `dynamodb_table_id` | DynamoDB table ID | |
| 92 | +| `dynamodb_table_arn` | DynamoDB table ARN | |
| 93 | +| `dynamodb_table_name` | DynamoDB table name | |
| 94 | + |
| 95 | + |
| 96 | +## Help |
| 97 | + |
| 98 | +**Got a question?** |
| 99 | + |
| 100 | +File a GitHub [issue ](https://github.com/cloudposse/terraform-aws-tfstate-backend/issues), send us an [email ](mailto:[email protected]) or reach out to us on [Gitter ](https://gitter.im/cloudposse/). |
| 101 | + |
| 102 | + |
| 103 | +## Contributing |
| 104 | + |
| 105 | +### Bug Reports & Feature Requests |
| 106 | + |
| 107 | +Please use the [issue tracker](https://github.com/cloudposse/terraform-aws-tfstate-backend/issues) to report any bugs or file feature requests. |
| 108 | + |
| 109 | +### Developing |
| 110 | + |
| 111 | +If you are interested in being a contributor and want to get involved in developing `terraform-aws-tfstate-backend`, we would love to hear from you! Shoot us an [email ](mailto:[email protected]). |
| 112 | + |
| 113 | +In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. |
| 114 | + |
| 115 | + 1. **Fork** the repo on GitHub |
| 116 | + 2. **Clone** the project to your own machine |
| 117 | + 3. **Commit** changes to your own branch |
| 118 | + 4. **Push** your work back up to your fork |
| 119 | + 5. Submit a **Pull request** so that we can review your changes |
| 120 | + |
| 121 | +**NOTE:** Be sure to merge the latest from "upstream" before making a pull request! |
| 122 | + |
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +[APACHE 2.0](LICENSE) © 2018 [Cloud Posse, LLC](https://cloudposse.com) |
| 127 | + |
| 128 | +See [LICENSE](LICENSE) for full details. |
| 129 | + |
| 130 | + Licensed to the Apache Software Foundation (ASF) under one |
| 131 | + or more contributor license agreements. See the NOTICE file |
| 132 | + distributed with this work for additional information |
| 133 | + regarding copyright ownership. The ASF licenses this file |
| 134 | + to you under the Apache License, Version 2.0 (the |
| 135 | + "License"); you may not use this file except in compliance |
| 136 | + with the License. You may obtain a copy of the License at |
| 137 | + |
| 138 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 139 | + |
| 140 | + Unless required by applicable law or agreed to in writing, |
| 141 | + software distributed under the License is distributed on an |
| 142 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 143 | + KIND, either express or implied. See the License for the |
| 144 | + specific language governing permissions and limitations |
| 145 | + under the License. |
| 146 | + |
| 147 | + |
| 148 | +## About |
| 149 | + |
| 150 | +`terraform-aws-tfstate-backend` is maintained and funded by [Cloud Posse, LLC][website]. |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +Like it? Please let us know at <[email protected]> |
| 156 | + |
| 157 | +We love [Open Source Software](https://github.com/cloudposse/)! |
| 158 | + |
| 159 | +See [our other projects][community] |
| 160 | +or [hire us][hire] to help build your next cloud platform. |
| 161 | + |
| 162 | + [website]: https://cloudposse.com/ |
| 163 | + [community]: https://github.com/cloudposse/ |
| 164 | + [hire]: https://cloudposse.com/contact/ |
| 165 | + |
| 166 | + |
| 167 | +### Contributors |
| 168 | + |
| 169 | +| [![Erik Osterman][erik_img]][erik_web]<br/>[Erik Osterman][erik_web] | [![Andriy Knysh][andriy_img]][andriy_web]<br/>[Andriy Knysh][andriy_web] | |
| 170 | +|-------------------------------------------------------|------------------------------------------------------------------| |
| 171 | + |
| 172 | + [erik_img]: http://s.gravatar.com/avatar/88c480d4f73b813904e00a5695a454cb?s=144 |
| 173 | + [erik_web]: https://github.com/osterman/ |
| 174 | + [andriy_img]: https://avatars0.githubusercontent.com/u/7356997?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144 |
| 175 | + [andriy_web]: https://github.com/aknysh/ |
0 commit comments