Git Product home page Git Product logo

terraform-aws-s3-state-for-organization's Introduction

terraform-aws-state-storage

  • A main S3 State Bucket that requires encrypted communication
  • A backup S3 State Bucket (which can be in a different region)
  • Replication between the main and backup bucket
  • A role with limited access to manipulate state in the main S3 bucket, which can be assumed by specific AWS principals or SSO Permission-Sets.
  • DynamoDB State locking according to Terraform backend "s3" specs

Terraform Syntax

This is a sample snippet of what would be added to your Terraform stack in order to access state using this module setup.

terraform {
  backend "s3" {
    role_arn       = "arn:aws:iam::000000000000:role/state-bucket-test-s2r4-state"
    bucket         = "state-bucket-test-s2r4-main"
    dynamodb_table = "state-bucket-test-s2r4-locks"
    key            = "example/prototype/test"
    region         = "us-east-1"
    encrypt        = true
    max_retries    = 2
  }
}

Important thing to note about the block above, is that the credentials used to assume the role_arn are pulled from the environment.

Blocks like the following, DO NOT have any relation to the above, meaning that the aws provider is not needed in order to access AWS S3 state.

terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.27"
    }
  }
}

Likewise, the credentials in the block below are also not related to the backend "s3" block.

provider "aws" {
  region      = "someregion"
  access_key  = "123"
  secret_key  = "456"
  token       = "789"
}

DynamoDB locking

For example, a stack with the name example/prototype/test will generate a DynamoDB entry with LockID being state-bucket-test-s2r4-main/example/prototype/test and the following content.

LockID is a standard used by the S3/DynamoDB Terraform backend.

{
  "ID": "15c87bd1-b189-4cb5-359d-746c71e3068e",
  "Operation": "OperationTypeApply",
  "Info": "",
  "Who": "username@pop-os",
  "Version": "1.2.7",
  "Created": "2022-08-25T04:08:31.335995976Z",
  "Path": "state-bucket-test-s2r4-main/example/prototype/test"
}

What do I do if the statefile lock was not released?

# The ID would show up in the error message you get when trying to run terraform
terraform force-unlock <ID>

PLEASE be sure that the statefile lock was yours!

Sample Error:

Acquiring state lock. This may take a few moments...
╷
│ Error: Error acquiring the state lock
│ 
│ Error message: ConditionalCheckFailedException: The conditional request failed
│ Lock Info:
│   ID:        6b0b048e-5cf4-c88b-667e-4522ccf65659
│   Path:      state-bucket-test-s2r4-main/example/prototype/test
│   Operation: OperationTypeApply
│   Who:       phadviger@pop-os
│   Version:   1.2.7
│   Created:   2022-09-08 10:50:46.24286514 +0000 UTC
│   Info:      

terraform-aws-s3-state-for-organization's People

Contributors

datfinesoul avatar

Watchers

 avatar

terraform-aws-s3-state-for-organization's Issues

More Restrictive S3 Bucket Policy

The bucket policy should:

  • Only allow proper roles and permission sets to view files
  • Prevent any unauthorized activities such as but not limited to:
    • Version delete
    • Bucket delete
  • Backup bucket should in addition
    • Only allow replication rule
    • Allow authorized roles to see files only
  • Module could have a variable that lets terraform disable the bucket policy, so that the bucket can then later be deleted if needed (although, potentially, maybe that needs to intentionally remain hard to do, need to brainstorm that)
    • Need to likely add workflow hooks in spacelist to allow automated testing of this, for cleanup

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.