InfraSage
Mission: Intelligent infrastructure provisioning and management, generating Terraform or CloudFormation scripts for easy deployment and scaling.
Terraform Generation for Multi-Cloud (AWS + Azure) with Cost Estimation
import logging
import os
from typing import Dict
logging.basicConfig(level=logging.INFO)
class InfraSageAgent:
def __init__(self, output_dir="./infra_files"):
self.output_dir = output_dir
os.makedirs(output_dir, exist_ok=True)
def generate_terraform_scripts(self, requirements: Dict[str, str], multi_cloud: bool=False):
"""
requirements might include fields like:
{
"app_type": "microservice",
"scaling": "auto",
"desired_count": 3,
"cloud_provider": ["aws", "azure"],
"estimated_budget": "medium"
}
"""
if multi_cloud:
for cloud in requirements.get('cloud_provider', []):
script_name = f"main_{cloud}.tf"
tf_content = self._create_tf_content(cloud, requirements)
self._save_tf_file(script_name, tf_content)
else:
single_cloud = requirements['cloud_provider'][0]
script_name = f"main_{single_cloud}.tf"
tf_content = self._create_tf_content(single_cloud, requirements)
self._save_tf_file(script_name, tf_content)
logging.info("Terraform scripts generated successfully.")
def _create_tf_content(self, cloud: str, reqs: Dict[str, str]) -> str:
# Hypothetical advanced logic:
# - Adjust CPU/memory based on 'estimated_budget' (low, medium, high)
# - Provide autoscaling blocks if 'scaling' is "auto"
# - For Azure, define resource_group, for AWS define ECS or EKS cluster
scaling_block = ""
if reqs.get('scaling') == "auto":
scaling_block = """
resource "aws_appautoscaling_policy" "scale_policy" {
# ...
}""" if cloud == "aws" else """
resource "azurerm_monitor_autoscale_setting" "scale_policy" {
# ...
}"""
if reqs.get('estimated_budget', 'medium') == 'low':
cpu_val = 128
memory_val = 256
elif reqs.get('estimated_budget', 'medium') == 'high':
cpu_val = 512
memory_val = 1024
else:
cpu_val = 256
memory_val = 512
base = f"""
# Generated by InfraSage for {cloud.upper()}
provider "{cloud}" {{
# region or location might be dynamically set
}}
resource "{cloud}_ecs_cluster" "app_cluster" {{
name = "arcos_{reqs.get('app_type', 'app')}_cluster"
}}
resource "{cloud}_ecs_task_definition" "app_task" {{
family = "arcos_task_def"
network_mode = "awsvpc"
cpu = {cpu_val}
memory = {memory_val}
requires_compatibilities = ["FARGATE"]
container_definitions = <<DEFINITION
[
{{
"name": "arcos_app",
"image": "org/arcos_app:latest",
"portMappings": [
{{
"containerPort": 8080,
"hostPort": 8080
}}
],
"essential": true
}}
]
DEFINITION
}}
resource "{cloud}_ecs_service" "app_service" {{
name = "arcos_service"
cluster = {cloud}_ecs_cluster.app_cluster.id
task_definition = {cloud}_ecs_task_definition.app_task.arn
desired_count = {reqs.get('desired_count', 2)}
network_configuration {{
subnets = ["subnet-abc123", "subnet-def456"]
assign_public_ip = true
}}
}}
{scaling_block}
"""
return base
def _save_tf_file(self, filename: str, content: str):
file_path = os.path.join(self.output_dir, filename)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
logging.debug(f"Saved Terraform file: {file_path}")
# Example usage:
# infra_sage = InfraSageAgent()
# requirements_map = {
# "app_type": "microservice",
# "scaling": "auto",
# "desired_count": 3,
# "cloud_provider": ["aws", "azure"],
# "estimated_budget": "medium"
# }
# infra_sage.generate_terraform_scripts(requirements_map, multi_cloud=True)
Last updated