Skip to content

Commit ed78951

Browse files
committed
up setup of remote terraform state
time track: 3.5h
1 parent bdedba4 commit ed78951

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*.tfstate
66
*.tfstate.*
77

8+
*.terraform.lock.hcl
9+
810
# Crash log files
911
crash.log
1012

@@ -27,3 +29,7 @@ override.tf.json
2729

2830
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
2931
# example: *tfplan*
32+
33+
# terragrunt
34+
*.out
35+
**/.terragrunt-cache/*

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Infrastructure Example
22
This example was conceived as an infrastructure for one AWS account with one region and division into dev, management and prod environments.
3+
But it can be extended for multiple regions, aws accounts and other clouds.
34

45
Current files structure:
56
```
@@ -19,10 +20,20 @@ terraform/modules/.gitkeep
1920
```
2021

2122
Time track:
22-
- [Filipp Frizzy](https://github.com/Friz-zy/) 11.5h
23+
- [Filipp Frizzy](https://github.com/Friz-zy/) 15.0h
2324

2425
## [Terraform](https://www.terraform.io/) and [Terragrunt](https://terragrunt.gruntwork.io)
2526
In this setup I use terraform with terragrunt for provisioning whole infrastructure.
27+
Terraform can store it's state in files or in remote backend via S3 or [Terraform Cloud](https://cloud.hashicorp.com/products/terraform).
28+
For command work we should use only remote state. In this setup I use AWS S3 `terraform_state` bucket + DynamoDB for locking.
29+
This require some initial preparation:
30+
```
31+
cd terraform/environments/aws-account-id/us-east-2/management/s3/terraform_state/
32+
terraform init
33+
terraform apply
34+
sed -i "s/terraform_state_bucket/$(terraform output terraform_state_s3_bucket_name|sed 's/\"//g')/g" ../../../../backend.hcl
35+
terragrunt init
36+
```
2637

2738
## [Ansible](https://www.ansible.com/)
2839

terraform/environments/aws-account-id/backend.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ remote_state {
55
if_exists = "overwrite_terragrunt"
66
}
77
config = {
8-
bucket = "terraform_state"
8+
bucket = "terraform_state_bucket"
99
key = "${path_relative_to_include()}/terraform.tfstate"
1010
region = "us-east-2"
1111
encrypt = true

terraform/environments/aws-account-id/us-east-2/management/s3/terraform_state/main.tf

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ provider "aws" {
66

77
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket
88
resource "aws_s3_bucket" "terraform_state" {
9-
bucket_prefix = "terraform_state"
9+
bucket_prefix = "terraform-state-"
1010

1111
tags = {
1212
Name = "terraform_state"
1313
Terraform = "terraform_state"
1414
Environment = "management"
1515
}
1616

17+
# change it for deleting bucket with all content
18+
force_destroy = false
19+
1720
lifecycle {
1821
prevent_destroy = true
1922
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "terraform_state_s3_bucket_name" {
2+
value = aws_s3_bucket.terraform_state.id
3+
description = "The Name of the S3 bucket for terraform state"
4+
}

0 commit comments

Comments
 (0)