227 lines
5.3 KiB
Markdown
227 lines
5.3 KiB
Markdown
---
|
|
description: Generate Infrastructure as Code
|
|
---
|
|
|
|
# Infrastructure as Code Generator
|
|
|
|
Generate production-ready IaC for multiple platforms and cloud providers.
|
|
|
|
## IaC Platforms Supported
|
|
|
|
1. **Terraform**: AWS, GCP, Azure infrastructure
|
|
2. **CloudFormation**: AWS native IaC
|
|
3. **Pulumi**: Multi-cloud with programming languages
|
|
4. **ARM Templates**: Azure Resource Manager
|
|
5. **CDK**: Cloud Development Kit (AWS, Terraform)
|
|
|
|
## Terraform Example (AWS ECS Fargate)
|
|
|
|
```hcl
|
|
terraform {
|
|
required_version = ">= 1.0"
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = "~> 5.0"
|
|
}
|
|
}
|
|
}
|
|
|
|
variable "environment" {
|
|
type = string
|
|
default = "production"
|
|
}
|
|
|
|
variable "app_name" {
|
|
type = string
|
|
default = "web-app"
|
|
}
|
|
|
|
# VPC Configuration
|
|
module "vpc" {
|
|
source = "terraform-aws-modules/vpc/aws"
|
|
|
|
name = "${var.app_name}-${var.environment}"
|
|
cidr = "10.0.0.0/16"
|
|
|
|
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
|
|
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
|
|
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
|
|
|
|
enable_nat_gateway = true
|
|
enable_vpn_gateway = false
|
|
|
|
tags = {
|
|
Environment = var.environment
|
|
Terraform = "true"
|
|
}
|
|
}
|
|
|
|
# ECS Cluster
|
|
resource "aws_ecs_cluster" "main" {
|
|
name = "${var.app_name}-${var.environment}"
|
|
|
|
setting {
|
|
name = "containerInsights"
|
|
value = "enabled"
|
|
}
|
|
}
|
|
|
|
# ECS Task Definition
|
|
resource "aws_ecs_task_definition" "app" {
|
|
family = "${var.app_name}"
|
|
network_mode = "awsvpc"
|
|
requires_compatibilities = ["FARGATE"]
|
|
cpu = "256"
|
|
memory = "512"
|
|
execution_role_arn = aws_iam_role.ecs_execution.arn
|
|
task_role_arn = aws_iam_role.ecs_task.arn
|
|
|
|
container_definitions = jsonencode([
|
|
{
|
|
name = var.app_name
|
|
image = "${aws_ecr_repository.app.repository_url}:latest"
|
|
|
|
portMappings = [
|
|
{
|
|
containerPort = 8080
|
|
protocol = "tcp"
|
|
}
|
|
]
|
|
|
|
environment = [
|
|
{
|
|
name = "NODE_ENV"
|
|
value = var.environment
|
|
}
|
|
]
|
|
|
|
secrets = [
|
|
{
|
|
name = "DATABASE_URL"
|
|
valueFrom = aws_ssm_parameter.db_url.arn
|
|
}
|
|
]
|
|
|
|
logConfiguration = {
|
|
logDriver = "awslogs"
|
|
options = {
|
|
"awslogs-group" = aws_cloudwatch_log_group.app.name
|
|
"awslogs-region" = "us-east-1"
|
|
"awslogs-stream-prefix" = "ecs"
|
|
}
|
|
}
|
|
|
|
healthCheck = {
|
|
command = ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"]
|
|
interval = 30
|
|
timeout = 5
|
|
retries = 3
|
|
startPeriod = 60
|
|
}
|
|
}
|
|
])
|
|
}
|
|
|
|
# ECS Service
|
|
resource "aws_ecs_service" "app" {
|
|
name = "${var.app_name}-service"
|
|
cluster = aws_ecs_cluster.main.id
|
|
task_definition = aws_ecs_task_definition.app.arn
|
|
desired_count = 3
|
|
launch_type = "FARGATE"
|
|
|
|
network_configuration {
|
|
subnets = module.vpc.private_subnets
|
|
security_groups = [aws_security_group.ecs_tasks.id]
|
|
assign_public_ip = false
|
|
}
|
|
|
|
load_balancer {
|
|
target_group_arn = aws_lb_target_group.app.arn
|
|
container_name = var.app_name
|
|
container_port = 8080
|
|
}
|
|
|
|
deployment_configuration {
|
|
maximum_percent = 200
|
|
minimum_healthy_percent = 100
|
|
}
|
|
|
|
depends_on = [aws_lb_listener.app]
|
|
}
|
|
|
|
# Application Load Balancer
|
|
resource "aws_lb" "app" {
|
|
name = "${var.app_name}-alb"
|
|
internal = false
|
|
load_balancer_type = "application"
|
|
security_groups = [aws_security_group.alb.id]
|
|
subnets = module.vpc.public_subnets
|
|
|
|
enable_deletion_protection = true
|
|
|
|
tags = {
|
|
Environment = var.environment
|
|
}
|
|
}
|
|
|
|
resource "aws_lb_target_group" "app" {
|
|
name = "${var.app_name}-tg"
|
|
port = 8080
|
|
protocol = "HTTP"
|
|
vpc_id = module.vpc.vpc_id
|
|
target_type = "ip"
|
|
|
|
health_check {
|
|
healthy_threshold = 2
|
|
unhealthy_threshold = 2
|
|
timeout = 5
|
|
interval = 30
|
|
path = "/health"
|
|
matcher = "200"
|
|
}
|
|
}
|
|
|
|
resource "aws_lb_listener" "app" {
|
|
load_balancer_arn = aws_lb.app.arn
|
|
port = "443"
|
|
protocol = "HTTPS"
|
|
ssl_policy = "ELBSecurityPolicy-2016-08"
|
|
certificate_arn = aws_acm_certificate.app.arn
|
|
|
|
default_action {
|
|
type = "forward"
|
|
target_group_arn = aws_lb_target_group.app.arn
|
|
}
|
|
}
|
|
|
|
# Auto Scaling
|
|
resource "aws_appautoscaling_target" "ecs" {
|
|
max_capacity = 10
|
|
min_capacity = 3
|
|
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.app.name}"
|
|
scalable_dimension = "ecs:service:DesiredCount"
|
|
service_namespace = "ecs"
|
|
}
|
|
|
|
resource "aws_appautoscaling_policy" "cpu" {
|
|
name = "cpu-autoscaling"
|
|
policy_type = "TargetTrackingScaling"
|
|
resource_id = aws_appautoscaling_target.ecs.resource_id
|
|
scalable_dimension = aws_appautoscaling_target.ecs.scalable_dimension
|
|
service_namespace = aws_appautoscaling_target.ecs.service_namespace
|
|
|
|
target_tracking_scaling_policy_configuration {
|
|
predefined_metric_specification {
|
|
predefined_metric_type = "ECSServiceAverageCPUUtilization"
|
|
}
|
|
target_value = 70.0
|
|
}
|
|
}
|
|
```
|
|
|
|
## When Invoked
|
|
|
|
Generate complete Infrastructure as Code for deploying applications on any cloud platform.
|