Skip to content

Commit 95caabd

Browse files
authored
Create terra_remotestate.tf
Demo to transfer state file from local to remote using s3 storage
1 parent 804a5e6 commit 95caabd

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#Demo file to test around concepts of terraform state and its remote storage in S3
2+
3+
provider "aws" {
4+
region = "us-east-1"
5+
access_key = "your_access"
6+
secret_key = "your_secret"
7+
}
8+
9+
resource "aws_instance" "us-east1" {
10+
ami = "ami-02f3f602d23f1659d" # us-east-1
11+
instance_type = "t2.micro"
12+
}
13+
14+
resource "aws_s3_bucket" "terraform_state" {
15+
bucket = "terraform-up-and-running-state"
16+
17+
# Prevent accidental deletion of this S3 bucket
18+
lifecycle {
19+
prevent_destroy = true
20+
}
21+
}
22+
23+
# Versioning. Every update to a file in the bucket creates a new version of it.
24+
# Allows to revert to older versions if something goes wrong
25+
resource "aws_s3_bucket_versioning" "enabled" {
26+
bucket = aws_s3_bucket.terraform_state.id
27+
versioning_configuration {
28+
status = "Enabled"
29+
}
30+
}
31+
32+
resource "aws_s3_bucket_server_side_encryption_configuration" "default" {
33+
bucket = aws_s3_bucket.terraform_state.id
34+
35+
rule {
36+
apply_server_side_encryption_by_default {
37+
sse_algorithm = "AES256"
38+
}
39+
}
40+
}
41+
42+
# Block all public access to the S3 bucket
43+
resource "aws_s3_bucket_public_access_block" "public_access" {
44+
bucket = aws_s3_bucket.terraform_state.id
45+
block_public_acls = true
46+
block_public_policy = true
47+
ignore_public_acls = true
48+
restrict_public_buckets = true
49+
}
50+
51+
#DynamoDB table for locking with Terraform, with a primary key named LockID
52+
resource "aws_dynamodb_table" "terraform_locks" {
53+
name = "terraform-up-and-running-locks"
54+
billing_mode = "PAY_PER_REQUEST"
55+
hash_key = "LockID"
56+
57+
attribute {
58+
name = "LockID"
59+
type = "S"
60+
}
61+
}
62+
63+
#---------------------------------
64+
#Run terraform init before this
65+
#State is generated locally, but s3 and dynamodb are created on AWS
66+
67+
#Configures Terraform to store the state in your S3 bucket (with encryption and locking)
68+
#Note that variables and references dont work in this, mention directly
69+
terraform {
70+
backend "s3" {
71+
# Replace this with your bucket name!
72+
bucket = "terraform-up-and-running-state"
73+
key = "global/s3/terraform.tfstate"
74+
region = "us-east-1"
75+
76+
# Replace this with your DynamoDB table name!
77+
dynamodb_table = "terraform-up-and-running-locks"
78+
encrypt = true
79+
}
80+
}
81+
82+
#Now run Terraform init again, so the state is uploaded to s3
83+
output "s3_bucket_arn" {
84+
value = aws_s3_bucket.terraform_state.arn
85+
description = "The ARN of the S3 bucket"
86+
}
87+
88+
output "dynamodb_table_name" {
89+
value = aws_dynamodb_table.terraform_locks.name
90+
description = "The name of the DynamoDB table"
91+
}
92+
93+
#Now run terraform apply to complete the process
94+
95+
#To reverse all of this, remove backend config and run terraform init. This copies terraform state back to local
96+
#Then run terraform destroy s3 and dynamodb

0 commit comments

Comments
 (0)