Home > Net >  Terraform:- can we create " data source" in a seperate file like local or variables?
Terraform:- can we create " data source" in a seperate file like local or variables?

Time:10-18

I want to seperate data form main code and use them in seperate file similar to local.tf or variables.tf, however even in the docs there is no reference.

use case

I am trying to create access logging for s3 bucket. Target bucket is not managed by s3 so I want to make sure that it exists before using it via data source

resource "aws_s3_bucket" "artifact" {
  bucket   = "jatin-123"
}

data "aws_s3_bucket" "selected" {
  bucket = "bucket.test.com"
}
resource "aws_s3_bucket_logging" "artifacts_server_access_logs" {
  for_each = local.env
  bucket   = data.aws_s3_bucket.selected.id

  target_bucket = local.s3_artifact_access_logs_bucket_name
  target_prefix = "${aws_s3_bucket.artifact[each.key].id}/"
}

CodePudding user response:

Yes, you can have data sources in whatever file you want.
Terraform basically does not care about the file composition and their names and just lumps all .tf files in the same directory into one big blob.

  • Related