I have a below module structure where where I am trying to run common terraform modules using a parent child relationship.
Common terraform modules are under the common folder and the root terraform files and under the eu-west-1/tf-modules folder. Inside the eu-west-1/tf-modules I have initialised common module as below inside the aws-provider-config.tf.
provider “aws” {
alias = “nVirginia-region”
region = “us-east-1”
}
provider “aws” {
alias = “delegate-account”
region = “eu-west-1”
profile = “delegateprofile”
}
module “common-modules” {
source = “./../../../common/tf-modules”
aws_partition = var.aws_partition
api_gateway_apis = var.api_gateway_apis
In the common module I have initialised a cloudformation stack and the variables are passed using a -var-file argument in the terraform apply command .The apply command runs under the root folder
Api gateway Implementation:
resource “aws_cloudformation_stack” “api-gateway” {
count = length(var.api_gateway_apis)
name = “${lookup(var.api_gateway_apis[count.index], “id”)}-api-gateway-stack”
parameters = {
ProjectName = var.project_name
Environment = var.target_environment
terraform apply command:
terraform -chdir=src/regions/eu-west-1/tf-modules apply -var-file=“../environments/devtest.auto.tfvars”
varable for api gateway:
api_gateway_apis = [
{
id = “message-management-service”,
name = “Message Management Service”,
base-path = “message-management”,
target_type = “Lambda”,
routes = [
The issue I am facing is when running the terraform apply command in the root folder the terraform files in the common folder is not getting picked up and is trying to destroy api gateway resources defined in the common folder. I can see the common modules getting initialised in the build console and in the tarraform module configuration the common module path is correctly set .
Build console when running tarraform init:
terraform -chdir=src/regions/eu-west-1/tf-modules init -backend-config=“../config/backend-config-devtest.tfbackend”
Initializing modules…e
- common-modules in ../../../common/tf-modules
terraform module config:
{“Modules”:[{“Key”:“”,“Source”:“”,“Dir”:“.”},{“Key”:“common-modules”,“Source”:“../../../common/tf-modules”,“Dir”:“../../../common/tf-modules”}]}
I have tried multiple options and when I move the tf file from the common back to the root the issue is resolved but I would like to keep the folder structure as is for the moment.
Some help would be greatly appreciated.
Thanks.