Skip to content

Instantly share code, notes, and snippets.

@ryu1kn
Created March 11, 2020 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu1kn/98a995e35b73ec144ce02f6d51a7243a to your computer and use it in GitHub Desktop.
Save ryu1kn/98a995e35b73ec144ce02f6d51a7243a to your computer and use it in GitHub Desktop.
Terraform, instantiate the same module multiple times
provider "aws" {
region = "ap-southeast-2"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "test-bucket-whatever-1jfibabl"
}
module "file_a" {
source = "./mod-a"
bucket_name = aws_s3_bucket.my_bucket.id
file_path = "../foo.txt"
}
module "file_b" {
source = "./mod-a"
bucket_name = aws_s3_bucket.my_bucket.id
file_path = "../bar.txt"
}
export AWS_PROFILE := your-aws-profile
auto_approve_target = apply destroy
init apply destroy:
cd infra && terraform $@ $(if $(filter $@,$(auto_approve_target)),-auto-approve)
# This file should be named as mod-a/main.tf
variable "file_path" {}
variable "bucket_name" {}
locals {
path_in_bucket = "foo/${basename(var.file_path)}"
}
resource "aws_s3_bucket_object" "object" {
bucket = var.bucket_name
key = local.path_in_bucket
source = var.file_path
}
output "path_in_bucket" {
value = local.path_in_bucket
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment