Welcome to my GCP Advanced Terraform Interactive Learning Challenge repository. Inspired by Erol Kavas's initiative, which can be explored at Advanced Terraform Interactive Learning Challenge by Erol Kavas
-
Deploy a robust web application infrastructure using Terraform, leveraging GCP services for a comprehensive and functional architecture.
-
Implement security best practices through GCP's security features, including VPCs, security groups, and IAM roles, to protect the application from unauthorized access.
-
Ensure scalability and high availability by utilizing auto-scaling for web servers and employing managed database services, ensuring the application can handle load changes and maintain uptime.
Key steps involve preparing the environment, building a secure network foundation, configuring the web and database tiers with security and scalability in mind, and setting up auto-scaling to dynamically adjust resources.
This challenge is not just about deploying infrastructure; it's about mastering real-world cloud architecture principles to create resilient, efficient, and secure applications on GCP.
- Terraform installation
- GCP account set up
Note: I utilized MODULE approach which is best practice to follow reuseable, more secure and compliant code base. Please see the values for the modules in the .tfvars file.
You can either create your project from GCP UI or with the following Terraform code:
It's good to use Service Account for the operations. To create the Service Account :
- Go under IAM & Admin and create a Service account.
- Go to KEY tab and create a new one to use in your Terraform config.
- Enable services in newly created GCP Project with the following code:
- See the tfvars file for the values :
- Create a bucket with the following code:
- Once the previous steps are done, you can configure your provider.tf as the following : provider.tf
- Make sure you give the right path for your credentials file in this code snippet:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/provider.tf
Lines 18 to 19 in ac4db42
As stated in the challange, we need a VPC, a private and public subnet. Set up an Internet Gateway for public subnet access and NAT Gateways for private subnet access.
-
1 public subnet:
- For internet access to Public Subnet, I used Firewall with HTTP and HTTPS traffic allowed.
GCP-Advanced-Terraform-Interactive-Learning-Challenge/project.tfvars
Lines 19 to 27 in bd58380
- For internet access to Public Subnet, I used Firewall with HTTP and HTTPS traffic allowed.
-
1 private subnet:
- NAT Gateway for the private subnet, I used compute_router and compute_router_nat
GCP-Advanced-Terraform-Interactive-Learning-Challenge/project.tfvars
Lines 29 to 62 in bd58380
- NAT Gateway for the private subnet, I used compute_router and compute_router_nat
As stated in the challange, Use IAM roles for secure CLOUD service interactions without hard-coded credentials.
- See the tfvars details:
As stated in the challange: Deploy web servers within an auto-scaling group, ensuring they can handle load spikes and failures gracefully.- Utilize an Application Load Balancer to distribute traffic evenly across your instances.
- Template's NIC resides in the Private Subnet
- See the module values:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/project.tfvars
Lines 80 to 100 in 4b3b7eb
- See the module values:
- See the module values:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/project.tfvars
Lines 102 to 113 in 4b3b7eb
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 50 to 60 in 4b3b7eb
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 62 to 76 in 4b3b7eb
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 97 to 104 in 4b3b7eb
I used compute_target_http_proxy Represents a TargetHttpProxy resource, which is used by one or more global forwarding rule to route incoming HTTP requests to a URL map.
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 106 to 113 in 4b3b7eb
I used compute_global_forwarding_rule : takes a name, the target HTTP proxy, and the range of port numbers this rule will serve for TCP.
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 115 to 123 in 4b3b7eb
As stated in the challange: Provision a managed database in a private subnet, accessible only by your application servers.
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/database.tf
Lines 1 to 18 in 75ec34b
As stated in the challange: Configure auto-scaling policies based on metrics (CPU, memory) to scale your web server fleet up or down automatically.
- See the module:
GCP-Advanced-Terraform-Interactive-Learning-Challenge/compute.tf
Lines 78 to 95 in 4b3b7eb
The journey began with a deep dive into understanding the specific requirements of deploying a secure and scalable web application on Google Cloud Platform (GCP). Although I was familiar with GCP, identifying the precise resources and their components necessary for this project posed an initial challenge. This step was crucial to ensure that the architecture would not only meet the challenge's objectives but also adhere to best practices in cloud infrastructure.
The next step involved an exhaustive exploration of Terraform's documentation for the required resources. Understanding how each resource needed to be configured and how they interconnect within the cloud environment was both time-consuming and enlightening. This phase was particularly intensive as I decided to write Terraform modules from scratch. The goal here was not just to meet the challenge's immediate needs but to create reusable components that could simplify future infrastructure as code (IaC) projects. Although this approach demanded a significant investment of time, the outcome was a set of modular, flexible, and reusable Terraform modules that could serve as a foundation for various cloud architectures.
Visualizing the architecture was another critical step in the process. Creating an infrastructure diagram took time but was essential for several reasons: it facilitated a better understanding of the overall architecture, helped in identifying potential issues or improvements, and provided a clear reference for both current and future project stakeholders. This diagram serves as a map, guiding the viewer through the complex interconnections of cloud resources that make up the secure and scalable web application.
One of the most significant challenges I encountered was inadvertently locking myself out of the project by deleting the google_project_iam_policy resource. This mistake was particularly frustrating and time-consuming, leading to a substantial setback.
I was like:
The issue stemmed from following guidance on HashiCorp's Terraform Registry, which, while helpful, resulted in an unexpected complication. To resolve this, I had to start from scratch by creating a new project and meticulously reconfiguring IAM policies in alignment with HashiCorp's recommendations. This experience underscored the importance of careful resource management and the potential pitfalls of managing IAM policies through Terraform.
Another challenge specific to GCP was the requirement to enable APIs for each resource before it could be managed through Terraform. This step is distinct from my experiences with Azure and AWS, where resource APIs are generally enabled by default or managed differently. Adapting to GCP's approach required a shift in mindset and an appreciation for the platform's unique operational model. Despite the initial learning curve, this aspect of GCP's infrastructure management ultimately added a valuable perspective to my cloud engineering skillset.
This project was not just about meeting the challenge's technical requirements; it was a journey of growth, learning, and adaptation. The process of building from the ground up, facing and overcoming obstacles, and reflecting on these experiences has deepened my understanding of GCP infrastructure and Terraform's capabilities.