Home > Blockchain >  How to add multiple "aws_s3_bucket_object" in a terraform script?
How to add multiple "aws_s3_bucket_object" in a terraform script?

Time:08-24

I am new to terraform and trying out few things.

here is my script.

    resource "aws_s3_bucket" "abt_tfstate" {
    bucket = "abt-tfstate"
    # acl = "private"
  
}
resource "aws_s3_bucket_acl" "abt_acl" {
  bucket = aws_s3_bucket.abt_tfstate.id
  acl    = "private"
}

resource "aws_s3_bucket_object" "s3_object_DevQa" {
    bucket = aws_s3_bucket.abt_tfstate.id
    for_each = fileset("C:\\Users\\SHETTSX18\\Desktop\\Terraform\\S3_bucket\\statefiles\\DevQa","*")
    key = "${var.folder_name_dev}/${each.value}"
    source = "C:\\Users\\SHETTSX18\\Desktop\\Terraform\\S3_bucket\\statefiles\\DevQa\\${each.value}"
}

resource "aws_s3_bucket_object" "s3_object_Prod" {
    bucket = aws_s3_bucket.abt_tfstate.id
    for_each = fileset("C:\\Users\\SHETTSX18\\Desktop\\Terraform\\S3_bucket\\statefiles\\Prod","*")
    key = "${var.folder_name_prod}/${each.value}"
    source = "C:\\Users\\SHETTSX18\\Desktop\\Terraform\\S3_bucket\\statefiles\\Prod\\${each.value}"
}

here is my variable file:

variable "folder_name_dev" {
    type = string
    default = "DevQa"
  
}

variable "folder_name_prod" {
    type = string
    default = "Prod"
  
}

for some reason only the s3_object_DevQa is being populating into the S3 bucket and there is no object with s3_object_Prod.

Also I would like to handle the variable in a single variable may be like folder_name. Not able to figure these two things out.

Could you please help me out with this?

CodePudding user response:

For the first point, I would use the resource 'aws_s3_object'.

This configuration seemed to work:

resource "aws_s3_bucket" "abt_tfstate" {
    bucket = "lt-abt-tfstate"
}

resource "aws_s3_bucket_acl" "abt_acl" {
  bucket = aws_s3_bucket.abt_tfstate.id
  acl    = "private"
}

resource "aws_s3_object" "s3_object_DevQa" {
    bucket = aws_s3_bucket.abt_tfstate.id
    for_each = fileset("DevQa/","*")
    key = "${var.folder_name_dev}/${each.value}"
    source = "DevQa/${each.value}"
}

resource "aws_s3_object" "s3_object_Prod" {
    bucket = aws_s3_bucket.abt_tfstate.id
    for_each = fileset("Prod/","*")
    key = "${var.folder_name_prod}/${each.value}"
    source = "Prod/${each.value}"
}

Now to refactor this so you can use a single 'folder_name' variable for both 'DevQa' and 'Prod', you could move the objects into a reusable module.

    ├── DevQa
    │   ├── hello1
    │   └── hello2
    ├── Prod
    │   ├── hello3
    │   └── hello4
    ├── backend.tf
    ├── main.tf
    ├── modules
    │   └── s3_objects
    │       ├── outputs.tf
    │       ├── resources.tf
    │       └── variables.tf
    ├── resources.tf
    └── variables.tf

./variables.tf:

variable "region" {
  type = string
  description = "AWS Region"
  default = "eu-west-1"
}

./resources.tf:

resource "aws_s3_bucket" "abt_tfstate" {
    bucket = "abt-tfstate-20220822"
}

resource "aws_s3_bucket_acl" "abt_acl" {
  bucket = aws_s3_bucket.abt_tfstate.id
  acl    = "private"
}

module "abt_tfstate_objects_dev" {
  source = "modules/s3_objects"
  folder_name = "DevQa"
}

module "abt_tfstate_objects_prd" {
  source = "modules/s3_objects"
  folder_name = "Prod"
}

modules/s3_objects/variables.tf:

variable "folder_name" {
    type = string
    description = "The folder name for the environment. Can be DevQa or Prod."
}

modules/s3_objects/resources.tf:

resource "aws_s3_object" "s3_object" {
    bucket = aws_s3_bucket.abt_tfstate.id
    for_each = fileset("${var.folder_name}/","*")
    key = "${var.folder_name}/${each.value}"
    source = "${var.folder_name}/${each.value}"
}
  • Related