Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Last active March 6, 2023 14:27
Show Gist options
  • Save stavxyz/c6e1fd15ff7975abc831a8e8193eaf69 to your computer and use it in GitHub Desktop.
Save stavxyz/c6e1fd15ff7975abc831a8e8193eaf69 to your computer and use it in GitHub Desktop.
Bootstrap terraform remote config 🐔 🥚
tf plan -target=aws_s3_bucket.tf_remote_config_bucket -out=confbucket.plan
tf apply confbucket.plan
# Configure against your new bucket.
terraform remote config -backend="S3" \
-backend-config="bucket=project-name-terraform-state" \
-backend-config="key=env-region-terraform-state/terraform.tfstate" \
-backend-config="region=us-west-2" \
-backend-config="encrypt=1"
tf refresh
tf plan -out=conf.plan
tf apply conf.plan
variable "aws_region" {
default = "us-west-2"
}
variable "tf_remote_backend_bucket_name" {
default = "project-name-terraform-state"
}
variable "tf_remote_backend_key" {
default = "env-region-terraform-state/terraform.tfstate"
}
# Set this to 0 if you are using a local tfstate file.
variable "tf_remote_backend" {
default = 1
}
variable "tf_remote_backend_type" {
default = "s3"
}
resource "aws_s3_bucket" "tf_remote_config_bucket" {
lifecycle {
prevent_destroy = true
}
count = "${var.tf_remote_backend ? 1 : 0}"
bucket = "${var.tf_remote_backend_bucket_name}"
versioning {
enabled = true
}
}
# This might be useful at some point, for referencing values in the remote state.
data "terraform_remote_state" "this" {
count = "${var.tf_remote_backend ? 1 : 0}"
backend = "${var.tf_remote_backend ? var.tf_remote_backend_type : "local"}"
config {
bucket = "${var.tf_remote_backend_bucket_name}"
key = "${var.tf_remote_backend_key}"
region = "${var.aws_region}"
encrypt = 1
}
}
output "tf_config_s3_bucket_id" {
value = "${aws_s3_bucket.tf_remote_config_bucket.id}"
}
output "tf_config_s3_bucket_arn" {
value = "${aws_s3_bucket.tf_remote_config_bucket.arn}"
}
output "tf_config_s3_bucket_region" {
value = "${aws_s3_bucket.tf_remote_config_bucket.region}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment