mirror of
https://github.com/BagelHole/DevOps-Security-Agent-Skills.git
synced 2026-08-22 12:49:53 +02:00
.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
---
|
||||
name: terraform-gcp
|
||||
description: Provision GCP infrastructure with Terraform. Configure providers and deploy Google Cloud resources. Use when implementing IaC for GCP.
|
||||
license: MIT
|
||||
metadata:
|
||||
author: devops-skills
|
||||
version: "1.0"
|
||||
---
|
||||
|
||||
# Terraform GCP
|
||||
|
||||
Provision Google Cloud infrastructure with Terraform.
|
||||
|
||||
## Provider Configuration
|
||||
|
||||
```hcl
|
||||
terraform {
|
||||
required_providers {
|
||||
google = {
|
||||
source = "hashicorp/google"
|
||||
version = "~> 5.0"
|
||||
}
|
||||
}
|
||||
backend "gcs" {
|
||||
bucket = "tf-state-bucket"
|
||||
prefix = "terraform/state"
|
||||
}
|
||||
}
|
||||
|
||||
provider "google" {
|
||||
project = var.project_id
|
||||
region = var.region
|
||||
}
|
||||
```
|
||||
|
||||
## Example Resources
|
||||
|
||||
```hcl
|
||||
resource "google_compute_network" "vpc" {
|
||||
name = "main-vpc"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "vm" {
|
||||
name = "web-server"
|
||||
machine_type = "e2-micro"
|
||||
zone = "us-central1-a"
|
||||
|
||||
boot_disk {
|
||||
initialize_params {
|
||||
image = "debian-cloud/debian-11"
|
||||
}
|
||||
}
|
||||
|
||||
network_interface {
|
||||
network = google_compute_network.vpc.name
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
- Use service accounts for authentication
|
||||
- Store state in GCS
|
||||
- Use labels consistently
|
||||
- Implement least-privilege IAM
|
||||
@@ -0,0 +1,114 @@
|
||||
# GCP VPC Module Template
|
||||
|
||||
variable "network_name" {
|
||||
description = "VPC network name"
|
||||
type = string
|
||||
default = "main"
|
||||
}
|
||||
|
||||
# VPC Network
|
||||
resource "google_compute_network" "main" {
|
||||
name = "${var.project_name}-${var.environment}-vpc"
|
||||
auto_create_subnetworks = false
|
||||
project = var.project_id
|
||||
}
|
||||
|
||||
# Public Subnet
|
||||
resource "google_compute_subnetwork" "public" {
|
||||
name = "${var.project_name}-${var.environment}-public"
|
||||
ip_cidr_range = "10.0.1.0/24"
|
||||
region = var.region
|
||||
network = google_compute_network.main.id
|
||||
project = var.project_id
|
||||
|
||||
secondary_ip_range {
|
||||
range_name = "gke-pods"
|
||||
ip_cidr_range = "10.1.0.0/16"
|
||||
}
|
||||
|
||||
secondary_ip_range {
|
||||
range_name = "gke-services"
|
||||
ip_cidr_range = "10.2.0.0/20"
|
||||
}
|
||||
}
|
||||
|
||||
# Private Subnet
|
||||
resource "google_compute_subnetwork" "private" {
|
||||
name = "${var.project_name}-${var.environment}-private"
|
||||
ip_cidr_range = "10.0.2.0/24"
|
||||
region = var.region
|
||||
network = google_compute_network.main.id
|
||||
project = var.project_id
|
||||
private_ip_google_access = true
|
||||
}
|
||||
|
||||
# Cloud Router (for NAT)
|
||||
resource "google_compute_router" "main" {
|
||||
name = "${var.project_name}-${var.environment}-router"
|
||||
region = var.region
|
||||
network = google_compute_network.main.id
|
||||
project = var.project_id
|
||||
}
|
||||
|
||||
# Cloud NAT
|
||||
resource "google_compute_router_nat" "main" {
|
||||
name = "${var.project_name}-${var.environment}-nat"
|
||||
router = google_compute_router.main.name
|
||||
region = var.region
|
||||
project = var.project_id
|
||||
nat_ip_allocate_option = "AUTO_ONLY"
|
||||
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
|
||||
}
|
||||
|
||||
# Firewall - Allow SSH
|
||||
resource "google_compute_firewall" "allow_ssh" {
|
||||
name = "${var.project_name}-${var.environment}-allow-ssh"
|
||||
network = google_compute_network.main.name
|
||||
project = var.project_id
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["22"]
|
||||
}
|
||||
|
||||
source_ranges = ["0.0.0.0/0"]
|
||||
target_tags = ["ssh"]
|
||||
}
|
||||
|
||||
# Firewall - Allow Internal
|
||||
resource "google_compute_firewall" "allow_internal" {
|
||||
name = "${var.project_name}-${var.environment}-allow-internal"
|
||||
network = google_compute_network.main.name
|
||||
project = var.project_id
|
||||
|
||||
allow {
|
||||
protocol = "icmp"
|
||||
}
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["0-65535"]
|
||||
}
|
||||
|
||||
allow {
|
||||
protocol = "udp"
|
||||
ports = ["0-65535"]
|
||||
}
|
||||
|
||||
source_ranges = ["10.0.0.0/8"]
|
||||
}
|
||||
|
||||
output "network_name" {
|
||||
value = google_compute_network.main.name
|
||||
}
|
||||
|
||||
output "network_id" {
|
||||
value = google_compute_network.main.id
|
||||
}
|
||||
|
||||
output "subnet_ids" {
|
||||
value = {
|
||||
public = google_compute_subnetwork.public.id
|
||||
private = google_compute_subnetwork.private.id
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
#!/bin/bash
|
||||
# Terraform GCP Project Initialization Script
|
||||
# Usage: ./tf-init-gcp.sh <project-name> <gcp-project-id> [region]
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_NAME="${1:-}"
|
||||
GCP_PROJECT="${2:-}"
|
||||
REGION="${3:-us-central1}"
|
||||
|
||||
if [ -z "$PROJECT_NAME" ] || [ -z "$GCP_PROJECT" ]; then
|
||||
echo "Usage: $0 <project-name> <gcp-project-id> [region]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "========================================="
|
||||
echo "Terraform GCP Project Setup"
|
||||
echo "Project: $PROJECT_NAME"
|
||||
echo "GCP Project: $GCP_PROJECT"
|
||||
echo "Region: $REGION"
|
||||
echo "========================================="
|
||||
echo ""
|
||||
|
||||
mkdir -p "$PROJECT_NAME"
|
||||
cd "$PROJECT_NAME"
|
||||
|
||||
# Create main.tf
|
||||
cat > main.tf << EOF
|
||||
terraform {
|
||||
required_version = ">= 1.0"
|
||||
|
||||
required_providers {
|
||||
google = {
|
||||
source = "hashicorp/google"
|
||||
version = "~> 5.0"
|
||||
}
|
||||
}
|
||||
|
||||
# Uncomment for remote state
|
||||
# backend "gcs" {
|
||||
# bucket = "${PROJECT_NAME}-tfstate"
|
||||
# prefix = "terraform/state"
|
||||
# }
|
||||
}
|
||||
|
||||
provider "google" {
|
||||
project = var.project_id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
locals {
|
||||
common_labels = {
|
||||
project = var.project_name
|
||||
environment = var.environment
|
||||
managed-by = "terraform"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create variables.tf
|
||||
cat > variables.tf << EOF
|
||||
variable "project_name" {
|
||||
description = "Project name for resource naming"
|
||||
type = string
|
||||
default = "${PROJECT_NAME}"
|
||||
}
|
||||
|
||||
variable "project_id" {
|
||||
description = "GCP Project ID"
|
||||
type = string
|
||||
default = "${GCP_PROJECT}"
|
||||
}
|
||||
|
||||
variable "environment" {
|
||||
description = "Environment (dev, staging, prod)"
|
||||
type = string
|
||||
default = "dev"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "GCP region"
|
||||
type = string
|
||||
default = "${REGION}"
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create outputs.tf
|
||||
cat > outputs.tf << EOF
|
||||
output "project_id" {
|
||||
description = "GCP Project ID"
|
||||
value = var.project_id
|
||||
}
|
||||
|
||||
output "region" {
|
||||
description = "GCP region"
|
||||
value = var.region
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create terraform.tfvars
|
||||
cat > terraform.tfvars << EOF
|
||||
project_name = "${PROJECT_NAME}"
|
||||
project_id = "${GCP_PROJECT}"
|
||||
environment = "dev"
|
||||
region = "${REGION}"
|
||||
EOF
|
||||
|
||||
# Create .gitignore
|
||||
cat > .gitignore << EOF
|
||||
.terraform/
|
||||
*.tfstate
|
||||
*.tfstate.*
|
||||
*.tfvars.json
|
||||
crash.log
|
||||
*.tfplan
|
||||
!terraform.tfvars.example
|
||||
.idea/
|
||||
*.swp
|
||||
.vscode/
|
||||
EOF
|
||||
|
||||
# Initialize Terraform
|
||||
echo ""
|
||||
echo "Initializing Terraform..."
|
||||
terraform init
|
||||
|
||||
echo ""
|
||||
echo "========================================="
|
||||
echo "Project created successfully!"
|
||||
echo "========================================="
|
||||
Reference in New Issue
Block a user