Skip to content

Instantly share code, notes, and snippets.

@veksh
Created January 3, 2024 18:33
Show Gist options
  • Save veksh/c6804b0d32f5138a92fa1cdb7ed67f7d to your computer and use it in GitHub Desktop.
Save veksh/c6804b0d32f5138a92fa1cdb7ed67f7d to your computer and use it in GitHub Desktop.
create AWS S3 bucket for terraform state
provider "aws" {
region = "eu-west-2"
}
resource "aws_s3_bucket" "terraform_state" {
bucket = "terraform-running-state"
# Prevent accidental deletion of this S3 bucket
lifecycle {
prevent_destroy = true
}
}
resource "aws_s3_bucket_versioning" "enabled" {
bucket = aws_s3_bucket.terraform_state.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "default" {
bucket = aws_s3_bucket.terraform_state.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_public_access_block" "public_access" {
bucket = aws_s3_bucket.terraform_state.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# also, dynamodb table
resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-running-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
terraform {
backend "s3" {
# Replace this with your bucket name!
bucket = "terraform-running-state"
key = "global/s3/terraform.tfstate"
region = "us-east-2"
# Replace this with your DynamoDB table name!
dynamodb_table = "terraform-running-locks"
encrypt = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment