User Tools

Site Tools


terraform:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
terraform:start [19/01/2024 16:36] – created - external edit 127.0.0.1terraform:start [07/10/2025 10:03] (current) – [State files and Lock files] andrew
Line 105: Line 105:
 </code> </code>
  
 +
 +===== Modules =====
 +
 +[[terraform:modules1|Terraform Modules]] \\
 +
 +[[terraform:modules2|Another Modules example]] \\
  
 ===== State files and Lock files ===== ===== State files and Lock files =====
  
-See [[aws:aws-cloudformation-terraformstate|Cloudformation for Terraform State Files and Lock Table]], this is some Cloudformation code which runs to bootstrap Terraform by creating an S3 bucket, DynamoDB Lock Table and stores the resulting Bucket and Table name in parameter Store where Terraform can retreive it.+See [[aws:cloudformation-terraformstate|Cloudformation for Terraform State Files and Lock Table]], this is some Cloudformation code which runs to bootstrap Terraform by creating an S3 bucket, DynamoDB Lock Table and stores the resulting Bucket and Table name in AWS Parameter Store where Terraform can retreive it.
  
  
-Use S3 and Dynamo db to store this info.+Code to create bucket with Terraform and then remove it from the state file with a tf cli command, this means that Terraform won't try to destroy the state file bucket
  
 +<code yaml>
 +resource "aws_s3_bucket" "state_bucket" {
 + bucket = "tf-mystate"
 +
 + tags = {
 +   Name = "State Bucket"
 + }
 +}
 +</code>
 +This create the bucket, then we remove it from the state file, probably worth moving the code to crate the bucket out of the path Teraform sees to avoid errors trying to recreate an existing resourse.
 +<code bash>
 +terraform state rm aws_s3_bucket.state_bucket
 +</code>
 +
 +Then, we can add the state file bucket to the AWS provider code:-
 +<file yaml terraform.tf>
 +terraform {
 +  required_version = ">= 1.2"
 +  required_providers {
 +    aws = {
 +      source  = "hashicorp/aws"
 +      version = "~> 5.92"
 +    }
 +  }
 +
 + backend "s3" {
 +   bucket = "tf-mystate"
 +   key    = "state"
 +   region = "eu-west-1"
 + }
 +}
 +
 +</file>
 +
 +**Updates to lockfile** - previously DynamoDb was used frequently for lock files, this can be stored in S3 along with the state:-
 +
 +<file yaml terraform.tf>
 +terraform {
 +  required_version = ">= 1.12"
 +  required_providers {
 +    aws = {
 +      source  = "hashicorp/aws"
 +      version = "~> 5.92"
 +    }
 +  }
 +
 + backend "s3" {
 +   bucket = "tf-123-state"
 +   key    = "state"
 +   region = "eu-west-1"
 +   use_lockfile = true  #S3 native locking
 + }
 +}
 +</file>
  
 ===== AWS Provisioner code ===== ===== AWS Provisioner code =====
Line 166: Line 226:
  
  
-==== Snippets ====+===== Snippets =====
  
  
Line 359: Line 419:
  
 </code> </code>
 +
 +
 +
 +===== Variables =====
 +
 +{{:terraform:image8-2.png?400|}}
 +
 +
 +
 +
 +
 +
  
 This page has been accessed for:- \\ This page has been accessed for:- \\
terraform/start.1705682162.txt.gz · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki