How to create Terraform backend on AWS S3 using Terraform itself?

Hey everyone! I’m new to Terraform and I’m trying to figure out how to set up my backend on AWS S3 using Terraform. I know I need to create an S3 bucket, IAM groups, and policies for the backend storage. But here’s the tricky part:

terraform {
  backend "s3" {
    container = "tf_state_bucket"
    file_path = "project_state"
    area = "us-west-2"
  }
}

When I try to set this up before creating the actual infrastructure, Terraform complains that the bucket doesn’t exist yet. It’s like a chicken and egg problem!

I’m thinking about writing a script to check if the bucket exists and bootstrap Terraform, then copy the state file to S3 after the first run. But that seems like a lot of work. Am I missing something obvious here? How do you guys handle setting up your Terraform backend with Terraform itself? Any tips or best practices would be super helpful!

yo growingTree, i feel ya. wut if u start with local backend, then switch to s3 after? create ur bucket n stuff first, then update backend config to s3. run ‘terraform init -migrate-state’ to move state. no separate configs needed. easypeasy!

Creating a separate Terraform configuration for backend setup is indeed a solid approach. However, there’s another method worth considering: utilizing a local backend initially, then transitioning to S3 after the necessary resources are created.

Start by configuring your Terraform with a local backend. Once you’ve successfully created the S3 bucket and associated resources, you can modify your backend configuration to use S3. Then, execute ‘terraform init’ with the ‘-migrate-state’ flag to transfer your state to S3.

This approach eliminates the need for separate configurations while still addressing the chicken-and-egg dilemma. It’s a streamlined process that maintains a single, cohesive Terraform workflow throughout your project’s lifecycle.

hey growingTree! that’s a tricky situation. have u considered a separate terraform config just for backend setup? that way, create the s3 bucket and iam stuff first, then use it for the main project. what do u think? any worries about managing two configs?