Convert Existing Terraform Configuration to Terraform Module

Hi,
I have terraform configurations in aws provider version 3.6 but not using any module and the resources have been created for awhile. I am thinking to upgrade it to aws provider version 4.0/later and move to terraform module.

I know to use terraform module it required some effort to import.
But consider a lot of existing resources that have been created that need to import to state in modules configuration. Is it possible to import to module and ensure no impact to existing resources?

You have not shown your existing Terraform configuration, or mentioned which module you are considering moving to. These are both necessary things to know to answer this question.

Without that information, all I can say is that unless the module is extremely similar to the resources you already have defined, it would not be possible - and even if it is possible, it would still require a complex custom series of operations.

Migrating from AWS provider v3 to v4 at the same time only makes things more complicated, and should probably not be attempted at the same time as other changes.

Thanks @maxb
sorry for the insufficient information. cannot provide the module yet, cause i am still trying to create it.

Original V3 s3 config is :

resource "aws_s3_bucket" "example" {
  bucket        = "example-s3"
  acl           = "private"
  force_destroy = false
  policy        = data.aws_iam_policy_document.example.json

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        kms_master_key_id = data.aws_kms_key.example-kms.arn
        sse_algorithm     = "aws:kms"
      }
    }
  }

  logging {
    target_bucket = aws_s3_bucket.example.id
    target_prefix = "example/"
  }

  versioning {
    enabled = true
  }
}

True. can says it’s required a lot of effort and become more complex.
In version4 we have to split all the s3 sub-configs (versioning, logging, encryption etc) into individual pieces and import those config to state file. But before that i still have to create the s3 module for it and import all the config to state file.

Ah, OK.

Since you’re writing the module specifically to match your existing configuration, you are basically talking about a normal migration from AWS provider v3 to v4, except you plan to place the reconfigured v4 resources in a module.

In that case, it’s not really much different from any AWS v3 to v4 upgrade, except that your new resource addresses are inside a module, so their resource addresses will include that.