|
1 | 1 | def IaC_template_generator_ec2(input) -> str: |
2 | 2 |
|
3 | | - |
| 3 | + ec2 = ['aws_key_pair', 'aws_security_group'] |
4 | 4 |
|
5 | | - prompt = f""" """ |
| 5 | + aws_ec2_create_key_pair = 'true' if input.key_pair else 'false' |
| 6 | + aws_ec2_create_security_group = 'true' if input.security_group else 'false' |
| 7 | + |
| 8 | + |
| 9 | + key_path = "${path.module}/terraform.pub" |
| 10 | + |
| 11 | + prompt = f""" |
| 12 | + Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) |
| 13 | + that dynamically provisions {ec2} resources ensuring a modular, flexible structure to enable users |
| 14 | + to configure all essential settings at the root level. Only provide Python code, no explanations or |
| 15 | + markdown formatting. The project should be organized as follows: |
| 16 | + 1. Root Directory Structure: |
| 17 | + - main.tf: |
| 18 | + - Define the provider block as follows: |
| 19 | + ``` |
| 20 | + provider "aws" {{ |
| 21 | + region = "us-east-1" |
| 22 | + }} |
| 23 | + ``` |
| 24 | + - Defines a module block that references "ec2" from a subdirectory within modules. |
| 25 | + This module block should expose all variables that {ec2} resources require, allowing |
| 26 | + configuration at the root level rather than directly within the module. |
| 27 | + - Every variable defined in {ec2} resources should be passed through the module block, |
| 28 | + ensuring that users can adjust all critical parameters of {ec2} resources by modifying |
| 29 | + root main.tf. Avoid using any other parameters. just use the parameters of {ec2} resources with the same keys |
| 30 | + - variables.tf: |
| 31 | + - Sets these variables names for aws_key_pair resource: |
| 32 | + key_pair_create(bool), key_pair_name(string) |
| 33 | + - Sets these variables names for aws_security_group resource: |
| 34 | + security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) |
| 35 | + - terraform.tfvars: |
| 36 | + - Structure as follows: |
| 37 | + key_pair_create = {aws_ec2_create_key_pair} |
| 38 | + key_pair_name = "ec2" |
| 39 | +
|
| 40 | + security_group_create = {aws_ec2_create_security_group} |
| 41 | + security_group_name = "my_rules" |
| 42 | + security_group_ingress_rules = {{ |
| 43 | + ssh_rule = {{ |
| 44 | + description = "SSH Ingress" |
| 45 | + from_port = 22 |
| 46 | + to_port = 22 |
| 47 | + protocol = "tcp" |
| 48 | + cidr_blocks = ["0.0.0.0/0"] |
| 49 | + }}, |
| 50 | + http_rule = {{ |
| 51 | + description = "HTTP Ingress" |
| 52 | + from_port = 80 |
| 53 | + to_port = 80 |
| 54 | + protocol = "tcp" |
| 55 | + cidr_blocks = ["0.0.0.0/0"] |
| 56 | + }} |
| 57 | + }} |
| 58 | + security_group_egress_rule = {{ |
| 59 | + from_port = 0 |
| 60 | + to_port = 0 |
| 61 | + protocol = "-1" |
| 62 | + cidr_blocks = ["0.0.0.0/0"] |
| 63 | + }} |
| 64 | + - versions.tf: |
| 65 | + - Structure as follows: |
| 66 | + terraform {{ |
| 67 | + required_version = ">= 1.0" |
| 68 | +
|
| 69 | + required_providers {{ |
| 70 | + aws = {{ |
| 71 | + source = "hashicorp/aws" |
| 72 | + version = ">= 5.20" |
| 73 | + }} |
| 74 | + }} |
| 75 | + }} |
| 76 | + 2. Module Directory Structure (modules/ec2): |
| 77 | + - create an empty file called "terraform.pub" to store the public key for key_pair resource |
| 78 | + - main.tf: |
| 79 | + - Set the following parameters for aws_key_pair resource (name its terraform resource to "key_pair") and avoid using any other parameters: |
| 80 | + - 1. count (type: number): follow the below syntax for count: |
| 81 | + ``` |
| 82 | + count = var.key_pair_create ? 1 : 0 |
| 83 | + ``` |
| 84 | + - 2. key_name (type: string): follow the below syntax for key_name: |
| 85 | + ``` |
| 86 | + key_name = var.key_pair_name |
| 87 | + ``` |
| 88 | + - 3. public_key (type: string): follow the below syntax for public_key: |
| 89 | + ``` |
| 90 | + public_key = file("{key_path}") |
| 91 | + ``` |
| 92 | + - Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters: |
| 93 | + - 1. count (type: number): follow the below syntax for count: |
| 94 | + ``` |
| 95 | + count = var.security_group_create ? 1 : 0 |
| 96 | + ``` |
| 97 | + - 2. name: follow the below syntax for name: |
| 98 | + ``` |
| 99 | + name = var.security_group_name |
| 100 | + ``` |
| 101 | + - 3. create a dynamic block for ingress rules as follows: |
| 102 | + ``` |
| 103 | + dynamic "ingress" {{ |
| 104 | + for_each = var.security_group_ingress_rules |
| 105 | + content {{ |
| 106 | + description = ingress.value["description"] |
| 107 | + from_port = ingress.value["from_port"] |
| 108 | + to_port = ingress.value["to_port"] |
| 109 | + protocol = ingress.value["protocol"] |
| 110 | + cidr_blocks = ingress.value["cidr_blocks"] |
| 111 | + }} |
| 112 | + }} |
| 113 | + ``` |
| 114 | + - 4. create a block for egress rule as follows: |
| 115 | + ``` |
| 116 | + egress {{ |
| 117 | + from_port = var.security_group_egress_rule["from_port"] |
| 118 | + to_port = var.security_group_egress_rule["to_port"] |
| 119 | + protocol = var.security_group_egress_rule["protocol"] |
| 120 | + cidr_blocks = var.security_group_egress_rule["cidr_blocks"] |
| 121 | + }} |
| 122 | + ``` |
| 123 | + - variables.tf: |
| 124 | + - Sets these variables names for aws_key_pair resource: |
| 125 | + key_pair_create(bool), key_pair_name(string) |
| 126 | + - Sets these variables names for aws_security_group resource: |
| 127 | + security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) |
| 128 | + - terraform.tfvars: |
| 129 | + - Structure as follows: |
| 130 | + key_pair_create = {aws_ec2_create_key_pair} |
| 131 | + key_pair_name = "ec2" |
| 132 | +
|
| 133 | + security_group_create = {aws_ec2_create_security_group} |
| 134 | + security_group_name = "my_rules" |
| 135 | + security_group_ingress_rules = {{ |
| 136 | + ssh_rule = {{ |
| 137 | + description = "SSH Ingress" |
| 138 | + from_port = 22 |
| 139 | + to_port = 22 |
| 140 | + protocol = "tcp" |
| 141 | + cidr_blocks = ["0.0.0.0/0"] |
| 142 | + }}, |
| 143 | + http_rule = {{ |
| 144 | + description = "HTTP Ingress" |
| 145 | + from_port = 80 |
| 146 | + to_port = 80 |
| 147 | + protocol = "tcp" |
| 148 | + cidr_blocks = ["0.0.0.0/0"] |
| 149 | + }} |
| 150 | + }} |
| 151 | + security_group_egress_rule = {{ |
| 152 | + from_port = 0 |
| 153 | + to_port = 0 |
| 154 | + protocol = "-1" |
| 155 | + cidr_blocks = ["0.0.0.0/0"] |
| 156 | + }} |
| 157 | + - versions.tf: |
| 158 | + - Structure as follows: |
| 159 | + terraform {{ |
| 160 | + required_version = ">= 1.0" |
| 161 | +
|
| 162 | + required_providers {{ |
| 163 | + aws = {{ |
| 164 | + source = "hashicorp/aws" |
| 165 | + version = ">= 5.20" |
| 166 | + }} |
| 167 | + }} |
| 168 | + }} |
| 169 | + Ensure this project structure supports {ec2}’s configurability, extensibility, and |
| 170 | + reusability across diverse Terraform providers, empowering users to manage their resources through a |
| 171 | + single, customizable root configuration while keeping module internals robustly modular. |
| 172 | +
|
| 173 | + finally just give me a python code without any note that can generate a project folder with the given |
| 174 | + schema without ```python entry. and we dont need any base directory in the python code. the final |
| 175 | + terraform template must work very well without any error! |
| 176 | +
|
| 177 | + Python code you give me, must have structure like that: |
| 178 | +
|
| 179 | + import os |
| 180 | + project_name = "app/media/MyTerraform" |
| 181 | + modules_dir = os.path.join(project_name, "modules") |
| 182 | + ec2_dir = os.path.join(modules_dir, "ec2") |
| 183 | +
|
| 184 | + # Create project directories |
| 185 | + os.makedirs(ec2_dir, exist_ok=True) |
| 186 | +
|
| 187 | + # Create main.tf |
| 188 | + with open(os.path.join(project_name, "main.tf"), "w") as main_file: |
| 189 | + # any thing you need |
| 190 | +
|
| 191 | + """ |
6 | 192 | return prompt |
0 commit comments