You are tasked with designing and deploying a multi-tier web application infrastructure using Terraform. The infrastructure must include:
Web Server Module: A module that creates a web server (simulated using an AWS EC2 instance or a null_resource if you’re not using a cloud provider). This module should include security group settings (if applicable) and utilize a provisioner to simulate web server installation.
Database Module: A module that creates a database server (for example, an AWS RDS instance or a simulated resource). It should be parameterized and expose key outputs such as connection details.
Dynamic Data Sources: Use a data source (e.g., fetching the latest Amazon Machine Image [AMI] for EC2) to drive dynamic configuration.
Environment Isolation with Workspaces: Configure separate workspaces for “development” and “production” environments. Each workspace should allow you to customize variables like instance sizes or counts.
Remote State Management: Configure a backend (e.g., local for simulation or a remote backend like AWS S3) to manage state files and enable team collaboration.
Versioning and Provider Configuration: Ensure your configuration uses version constraints for providers and modules for stability.
Challenge Objectives
Modularization: Create two reusable modules—one for the web server and one for the database.
Parameterization & Interpolation: Use variables and outputs to make your configuration dynamic. Use interpolation to reference values between modules.
Execution Workflow: Utilize the Terraform CLI to run init, plan, apply, and destroy. Visualize the dependency graph using terraform graph.
State Management: Configure a backend for state management and set up multiple workspaces (development vs. production).
Dynamic Data: Integrate a data source (such as an AMI lookup) to drive part of your configuration.
Provisioning: Incorporate provisioners in your modules to simulate post-deployment configuration steps (like installing a web server)
Deliverables and Evaluation
Repository Structure:
Your Git repository should reflect the directory structure with clear separation between the main configuration and modules.
Documentation:
Include a README.md file that:
Explains the purpose of each module.
Details how to switch between workspaces.
Describes how to adjust variables for different environments.
Summarizes how each key concept (e.g., provider configuration, state management, interpolation) is implemented.
Evidence of Execution:
Save logs or screenshots of terraform plan, terraform apply, the dependency graph, and outputs. This demonstrates that you successfully deployed and validated the infrastructure.
Versioning:
Ensure that your terraform { required_providers { ... } } block includes version constraints to test your provider configuration management skills.
Step-by-Step Instructions
Step 1: Project and Backend Setup
Directory Structure:
Create a directory structure similar to:
Backend Configuration:
In backend.tf, configure your backend. For simulation purposes, you can use the local backend:
If you have access to a remote backend (like AWS S3), follow the corresponding documentation to set it up.
Step 2: Web Server Module
Module Creation:
In modules/web_server/main.tf, define a resource that simulates a web server. For example, if using AWS, create an EC2 instance or, if not, use a null_resource:
Variables & Outputs:
In modules/web_server/variables.tf, declare variables like instance_type or server_name. In modules/web_server/outputs.tf, output key information such as a simulated public IP or instance ID.
Step 3: Database Module
Module Creation:
In modules/database/main.tf, define a resource representing a database. For AWS, you might use an RDS instance or simulate with a null_resource:
Variables & Outputs:
In modules/database/variables.tf, declare variables like db_name and connection parameters. In modules/database/outputs.tf, output details like a connection string or endpoint.
Step 4: Main Configuration with Dynamic Data
Reference Modules:
In your root main.tf, call the web server and database modules:
Integrate Data Sources:
Use a data source to fetch dynamic information (for example, the latest AMI for your region if using AWS):
Variables and Outputs:
In your root variables.tf, declare variables like web_server_name, instance_type, and db_name. In outputs.tf, aggregate outputs from the modules to display key connection details.
Step 5: Workspaces and Environment Isolation
Creating Workspaces:
Initialize your project and create workspaces:
Environment Customization:
Adjust variable values in each workspace (e.g., different instance types, database sizes, or naming conventions) either via different tfvars files or through command-line variable overrides.
Step 6: Execution and Validation
Plan and Apply:
In each workspace, run:
Validate that the outputs display the expected web server and database details.
Dependency Graph:
Generate and view the dependency graph:
Open the generated image to inspect resource dependencies.
Testing Provisioners:
Check that the provisioners in your modules output their messages, confirming the simulated installation steps.
Step 7: Clean-Up
Once you’re satisfied with your deployment and testing, use:
to clean up the deployed infrastructure in each workspace.
Challenge Recap:
Modularization: You built two modules—one for the web server and one for the database.
Dynamic Configuration: You used variables, outputs, interpolation, and data sources to make your configuration dynamic.
Execution Workflow: You managed the lifecycle of your infrastructure using Terraform CLI commands.
State and Environment Management: You configured remote state management, set up workspaces, and applied environment-specific configurations.
Provisioning and Versioning: You integrated provisioners to simulate resource configuration and set provider version constraints.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (