Refactored example-environments: separated envs

This commit is contained in:
Simone Ruffilli 2021-10-15 09:21:52 +02:00
parent b3838cfdb4
commit f344a863d1
9 changed files with 46 additions and 27 deletions

View File

@ -8,33 +8,36 @@ At the top level of this directory, besides the `README.md` your're reading now,
- `dev/`, a directory which holds all configurations for the *development* environment
- `prod/`, a directory which holds all configurations for the *production* environment
- `main.tf`, a simple terraform file which consumes the [`subnets`](../subnets/) module
and on each directory, `main.tf`, a simple terraform file which consumes the [`subnets`](../subnets/) module and the configurations.
Each environment directory structure is meant to mimic your GCP resource structure
```
.
├── dev # Environment
│ ├── project-dev-a # Project id
│ │ └── vpc-alpha # VPC name
│ │ ├── subnet-alpha-a.yaml # Subnet name (one file per subnet)
│ │ └── subnet-alpha-b.yaml
│ └── project-dev-b
│ ├── vpc-beta
│ │ └── subnet-beta-a.yaml
│ └── vpc-gamma
│ └── subnet-gamma-a.yaml
├── prod
│ └── project-prod-a
│ └── vpc-alpha
│ ├── subnet-alpha-a.yaml
│ └── subnet-alpha-b.yaml
├── main.tf
└── README.md
├── dev # Environment
│ ├── conf # Configuration directory
│ │ ├── project-dev-a # Project id
│ │ │ └── vpc-alpha # VPC name
│ │ │ ├── subnet-alpha-a.yaml # Subnet name (one file per subnet)
│ │ │ └── subnet-alpha-b.yaml
│ │ └── project-dev-b
│ │ ├── vpc-beta
│ │ │ └── subnet-beta-a.yaml
│ │ └── vpc-gamma
│ │ └── subnet-gamma-a.yaml
│ └── main.tf
└── prod
├── conf
│ └── project-prod-a
│ └── vpc-alpha
│ ├── subnet-alpha-a.yaml
│ └── subnet-alpha-b.yaml
└── main.tf
```
Since this resource factory only creates subnets, projects and VPCs are expected to exist.
In this example, a single `main.tf` file (hence a single state) drives the creation of both the `dev` and the `prod` environment. Another option you might want to consider, in line with the CI/CD pipeline or processes you have in place, might be to move `main.tf` to each environment's directory and hardwire the data path, so that states (and pipelines) can be separated.
In this example, each environment is implemented as a distinct factory, and each has its own `main.tf` file (and hence a dedicated state).
Another option you might want to consider, in line with the CI/CD pipeline or processes you have in place, might be to leverage a single `main.tf` consuming both environment directories.

View File

@ -15,12 +15,7 @@
*/
module "subnets-dev" {
module "subnets" {
source = "../subnets"
config_folder = "dev"
}
module "subnets-prod" {
source = "../subnets"
config_folder = "prod"
config_folder = "conf"
}

View File

@ -0,0 +1,21 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module "subnets" {
source = "../subnets"
config_folder = "conf"
}