Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 130 additions & 3 deletions app/template_generators/terraform/aws/s3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,133 @@
def IaC_template_generator_s3(input) -> str:


s3 = ['aws_s3_bucket', 'aws_s3_bucket_versioning']

prompt = f""" """
return prompt
aws_s3_create_bucket = 'true' if input.s3_bucket else 'false'
aws_s3_create_bucket_versioning = 'true' if input.bucket_versioning else 'false'

prompt = f"""
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
that dynamically provisions {s3} resources ensuring a modular, flexible structure to enable users
to configure all essential settings at the root level. Only provide Python code, no explanations or
markdown formatting. The project should be organized as follows:
1. Root Directory Structure:
- main.tf:
- Define the provider block as follows:
```
provider "aws" {{
region = "us-east-1"
}}
```
- Defines a module block that references "s3" from a subdirectory within modules.
This module block should expose all variables that {s3} resources require, allowing
configuration at the root level rather than directly within the module.
- Every variable defined in {s3} resources should be passed through the module block,
ensuring that users can adjust all critical parameters of {s3} resources by modifying
root main.tf. Avoid using any other parameters. just use the parameters of {s3} resources with the same keys
- variables.tf:
- Sets these variables names for aws_s3_bucket resource:
s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string))
- Sets these variables names for aws_s3_bucket_versioning resource:
s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string)
- terraform.tfvars:
- Structure as follows:
s3_create_bucket = {aws_s3_create_bucket}
s3_bucket_name = "UniqueName"
s3_bucket_force_destroy = false
s3_bucket_tags = {{
Name = "My bucket"
Environment = "Dev"
}}
s3_create_bucket_versioning = {aws_s3_create_bucket_versioning}
s3_bucket_versioning_status = "Enabled"
- versions.tf:
- Structure as follows:
terraform {{
required_version = ">= 1.0"

required_providers {{
aws = {{
source = "hashicorp/aws"
version = ">= 5.20"
}}
}}
}}
2. Module Directory Structure (modules/s3):
- main.tf:
- Set the following parameters for aws_s3_bucket resource (name its terraform resource to "s3_bucket")and avoid using any other parameters:
- 1. count (type: number): follow the below syntax for count:
```
count = var.s3_create_bucket ? 1 : 0
```
- 2. bucket (type: string): Specifies the bucket name.
- 3. force_destroy (type: boolean): Indicates all objects should be deleted from the bucket when the bucket is destroyed
- 4. tags (map(string) type): Includes the following fields:
- Name (type: string): A tag for the bucket.
- Environment (type: string): A tag for the bucket
- Set the following parameters for aws_s3_bucket_versioning resource (name its terraform resource to "s3_bucket_versioning")and avoid using any other parameters:
- 1. count (type: number): follow the below syntax for count:
```
count = var.s3_create_bucket && var.s3_create_bucket_versioning ? 1 : 0
```
- 2. bucket: it must points to the s3_bucket resource like the following syntax:
```
bucket = aws_s3_bucket.s3_bucket[0].id
```
- 3. versioning_configuration: this is a block which has a key/value pair as follows:
```
versioning_configuration {{
status = var.s3_bucket_versioning_status
}}
```
- variables.tf:
- Sets these variables names for aws_s3_bucket resource:
s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string))
- Sets these variables names for aws_s3_bucket_versioning resource:
s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string)
- terraform.tfvars:
- Structure as follows:
s3_create_bucket = {aws_s3_create_bucket}
s3_bucket_name = "UniqueName"
s3_bucket_force_destroy = false
s3_bucket_tags = {{
Name = "My bucket"
Environment = "Dev"
}}
s3_create_bucket_versioning = {aws_s3_create_bucket_versioning}
s3_bucket_versioning_status = "Enabled"
- versions.tf:
- Structure as follows:
terraform {{
required_version = ">= 1.0"

required_providers {{
aws = {{
source = "hashicorp/aws"
version = ">= 5.20"
}}
}}
}}
Ensure this project structure supports {s3}’s configurability, extensibility, and
reusability across diverse Terraform providers, empowering users to manage their resources through a
single, customizable root configuration while keeping module internals robustly modular.

finally just give me a python code without any note that can generate a project folder with the given
schema without ```python entry. and we dont need any base directory in the python code. the final
terraform template must work very well without any error!

Python code you give me, must have structure like that:

import os
project_name = "app/media/MyTerraform"
modules_dir = os.path.join(project_name, "modules")
s3_dir = os.path.join(modules_dir, "s3")

# Create project directories
os.makedirs(s3_dir, exist_ok=True)

# Create main.tf
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
# any thing you need

"""
return prompt
36 changes: 20 additions & 16 deletions app/template_generators/terraform/docker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def IaC_template_generator_docker(input) -> str:

docker = ['docker_container', 'docker_image']
docker_image_count = 1 if input.docker_image else 0
docker_container_count = 1 if input.docker_container else 0
create_docker_image = 'true' if input.docker_image else 'false'
create_docker_container = 'true' if input.docker_container else 'false'

prompt = f"""
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
Expand All @@ -25,25 +25,24 @@ def IaC_template_generator_docker(input) -> str:
root main.tf. Avoid using any other parameters. just use the parameters of {docker} resources with the same keys
- variables.tf:
- Sets these variables names for docker_image resource:
image_name(string), image_force_remove(bool), image_build(object), image_count(number)
create_image(bool), image_name(string), image_force_remove(bool), image_build(object)
- Sets these variables names for docker_container resource:
container_image(string), container_name(string), container_hostname(string),
container_restart(string), container_count(number)
create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string)
- terraform.tfvars:
- Structure as follows:
create_image = {create_docker_image}
image_name = "my-image"
image_force_remove = true
image_build = {{
context = "./"
tag = ["my-image:latest"]
}}
image_count = {docker_image_count}

create_container = {create_docker_container}
container_image = "my-image"
container_name = "my-container"
container_hostname = "my-host"
container_restart = "always"
container_count = {docker_container_count}
- versions.tf:
- Structure as follows:
terraform {{
Expand All @@ -58,41 +57,46 @@ def IaC_template_generator_docker(input) -> str:
}}
2. Module Directory Structure (modules/docker):
- main.tf:
- Set the following parameters for docker_image resource and avoid using any other parameters:
- 1. count (type: number) Specifies the number of images
- Set the following parameters for docker_image resource (name its terraform resource to "image") and avoid using any other parameters:
- 1. count (type: number): follow the below syntax for count:
```
count = var.create_image ? 1 : 0
```
- 2. name (type: string): Specifies the image name.
- 3. force_remove (type: boolean): Determines whether to forcibly remove intermediate containers.
- 4. build (block type): Includes the following required field:
- context (type: string, required): Specifies the build context for the image.
- tag(type: List of Strings, required): Specifices the image tag in the 'name:tag'
format, (e.g., ["NAME:VERSION"])
- Set the following parameters for docker_container resource and avoid using any other parameters:
- 1. count (type: number): Specifies the number of containers
- Set the following parameters for docker_container resource (name its terraform resource to "container") and avoid using any other parameters:
- 1. count (type: number): follow the below syntax for count:
```
count = var.create_container ? 1 : 0
```
- 2. image (type: string): Specifies the container image.
- 3. name (type: string): Sets the container name.
- 4. hostname (type: string): Configures the container hostname.
- 5. restart (type: string): Defines the container's restart policy (e.g., always, on-failure, no).
- variables.tf:
- Sets these variables names for docker_image resource:
image_name(string), image_force_remove(bool), image_build(object), image_count(number)
create_image(bool), image_name(string), image_force_remove(bool), image_build(object)
- Sets these variables names for docker_container resource:
container_image(string), container_name(string), container_hostname(string),
container_restart(string), container_count(number)
create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string)
- terraform.tfvars:
- Structure as follows:
create_image = {create_docker_image}
image_name = "my-image"
image_force_remove = true
image_build = {{
context = "./"
tag = ["my-image:latest"]
}}
image_count = {docker_image_count}

create_container = {create_docker_container}
container_image = "my-image"
container_name = "my-container"
container_hostname = "my-host"
container_restart = "always"
container_count = {docker_container_count}
- versions.tf:
- Structure as follows:
terraform {{
Expand Down
Loading