|
12 | 12 |
|
13 | 13 | ## Prerequisites
|
14 | 14 |
|
| 15 | +- [Direnv](https://direnv.net/) for loading environment variables. |
15 | 16 | - [Terraform](https://www.terraform.io/downloads.html) for infrastructure provisioning.
|
16 | 17 | - [TFswitch](https://tfswitch.warrensbox.com/) to switch between Terraform versions easily.
|
17 | 18 |
|
18 | 19 | ## Setup
|
19 | 20 |
|
20 |
| -1. **Set Terraform Version:** |
| 21 | +1. **Change Directory:** |
| 22 | + |
| 23 | + Navigate to the directory containing the Terraform configuration: |
| 24 | + |
| 25 | + ```sh |
| 26 | + cd live/common-infra |
| 27 | + ``` |
| 28 | + |
| 29 | +2. **Create .envrc file:** |
| 30 | + |
| 31 | + Create a new `.envrc` file in this directory by copying the `.envrc.example` file: |
| 32 | + |
| 33 | + ```sh |
| 34 | + cp .envrc.example .envrc |
| 35 | + ``` |
| 36 | + |
| 37 | + Then, update the `.envrc` file with the values for your environment! |
| 38 | + |
| 39 | +3. **Load Environment Variables:** |
| 40 | + |
| 41 | + Load the environment variables using `direnv`: |
| 42 | + |
| 43 | + ```sh |
| 44 | + direnv allow |
| 45 | + ``` |
| 46 | + |
| 47 | +4. **Set Terraform Version:** |
21 | 48 |
|
22 | 49 | Ensure you are using the correct Terraform version:
|
23 | 50 |
|
24 | 51 | ```sh
|
25 | 52 | tfswitch
|
26 | 53 | ```
|
27 | 54 |
|
28 |
| -2. **Initialize Terraform:** |
| 55 | +5. **Initialize Terraform:** |
29 | 56 |
|
30 | 57 | Initialize the working directory with the required providers and modules:
|
31 | 58 |
|
32 | 59 | ```sh
|
33 |
| - terraform init -backend-config="./configs/prod-backend.tfvars" |
| 60 | + terraform init -backend-config="./configs/${ENVIRONMENT}-backend.tfvars" |
34 | 61 | ```
|
35 | 62 |
|
36 |
| -3. **Workspace Management:** |
| 63 | +6. **Workspace Management:** |
37 | 64 |
|
38 | 65 | Select or create a new workspace tailored to your deployment environment:
|
39 | 66 |
|
40 | 67 | ```sh
|
41 | 68 | # Select an existing workspace
|
42 |
| - terraform workspace select prod |
| 69 | + terraform workspace select "${TF_WORKSPACE}" |
43 | 70 |
|
44 |
| - # Create a new workspace if it doesn't exist |
45 |
| - # and select it |
46 |
| - terraform workspace new prod |
47 |
| - terraform workspace select prod |
| 71 | + # Create a new workspace if it doesn't exist and select it |
| 72 | + terraform workspace new "${TF_WORKSPACE}" |
48 | 73 | ```
|
49 | 74 |
|
50 | 75 | ## Deploy
|
|
56 | 81 | Review and verify the deployment plan:
|
57 | 82 |
|
58 | 83 | ```sh
|
59 |
| - terraform plan -var-file ./configs/prod.tfvars -out prod.tfplan |
| 84 | + terraform plan -var-file "./configs/${ENVIRONMENT}.tfvars" -out "${ENVIRONMENT}.tfplan" |
60 | 85 | ```
|
61 | 86 |
|
62 | 87 | 2. **Execute the Plan:**
|
63 | 88 |
|
64 | 89 | Apply the planned configuration to provision the infrastructure:
|
65 | 90 |
|
66 | 91 | ```sh
|
67 |
| - terraform apply "prod.tfplan" |
| 92 | + terraform apply "${ENVIRONMENT}.tfplan" |
68 | 93 | ```
|
69 | 94 |
|
70 | 95 | ## Post Deployment Steps
|
@@ -139,12 +164,10 @@ These steps will help you verify the successful setup of the database and ensure
|
139 | 164 |
|
140 | 165 | ## Destroy
|
141 | 166 |
|
142 |
| -💣 **NOTE:** In this example, we are using the `prod` environment and the `us-west-2` region. Modify these values according to your environment and region. |
143 |
| -
|
144 | 167 | To destroy the infrastructure, run the following command:
|
145 | 168 |
|
146 | 169 | ```sh
|
147 |
| -terraform destroy -var-file ./configs/prod.tfvars |
| 170 | +terraform destroy -var-file "./configs/${ENVIRONMENT}.tfvars" |
148 | 171 | ```
|
149 | 172 |
|
150 | 173 | ## Module Documentation
|
|
0 commit comments