Giter VIP home page Giter VIP logo

tf-s3-remote-backend's Introduction

Terraformf S3 remote backend example

Prerequisites

Create S3 bucket and DynamoDB table

You can use the default terraform.tfvars.example file (just need to add AWS keys and remove the .example from the file name). If you prefere to use cmd arguments, that's up to you.

Go in the backend.tf file and comment the following block of code

terraform {
  backend "s3" {
    bucket         = "remote-state-me-us-east-1"
    encrypt        = true
    key            = "remote-state-me-us-east-1/backend/terraform.tfstate"
    dynamodb_table = "remote-state-me-us-east-1"
    region         = "us-east-1"
  }
}

Initiatialize Terraform project by importing the nessecary provider

terraform init

Check the S3 bucket and DynamoDB table that will be created

terraform plan

Create the S3 bucket and DynamoDB table

terraform apply -auto-approve

Go in the backend.tf file and uncomment the following block of code

terraform {
  backend "s3" {
    bucket         = "remote-state-me-us-east-1"
    encrypt        = true
    key            = "remote-state-me-us-east-1/backend/terraform.tfstate"
    dynamodb_table = "remote-state-me-us-east-1"
    region         = "us-east-1"
  }
}

Modify the various informations. Here is a maping of what is corresponding to what:

  • bucket: name of the S3 bucket to push the state to
  • encrypt: make sure that the terraform.tfstate file is encrypted on the S3 bucket
  • key: path on the S3 bucket where the terraform.tfstate file will be stored (it is up to you to define it, I strongly encourage you to follow a patern)
  • dynamodb_table: name of the DynamoDB table used to lock the state during Terraform operations
  • region: region where the S3 bucket is

Reinitiatialize the Terraform project to push the local state to the S3 bucket

terraform init

When asked, agree to push your local terraform.tfstate file to the remote S3 bucket.

Delete S3 bucket and DynamoDB table

Go in the backend.tf file and comment the following block of code, it is present in the S3 bucket and in the DynamoDB table ressources, so you will have to comment it in both places

lifecycle {
  prevent_destroy = true
}

Delete previously created DynamoDB table

terraform destroy -force

You will get an error, but the DynamoDB table will have been deleted. Go to your AWS console and manually delete the S3 bucket where you used to store the terraform.tfstate file.

Bonus

Install autocompletion for terraform:

terraform -install-autocomplete

Want more

You have a project? Want to discuss? Contact me at [email protected]

tf-s3-remote-backend's People

Contributors

woernfl avatar

Watchers

 avatar

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.