11import os
2-
32project_name = "app/media/MyTerraform"
4- base_directory = project_name .replace ("/" , os .sep )
5- modules_directory = os .path .join (base_directory , "modules" )
6- ci_directory = os .path .join (base_directory , ".github" , "workflows" )
7-
8- os .makedirs (modules_directory , exist_ok = True )
9- os .makedirs (ci_directory , exist_ok = True )
10-
11- terraform_main = f"""provider "aws" {{
12- region = "us-east-1"
13- }}
14-
15- resource "aws_instance" "web" {{
16- ami = "ami-0c55b159cbfafe1f0"
17- instance_type = "t2.micro"
18-
19- tags = {{
20- Name = "MyEC2Instance"
21- }}
22- }}
23- """
24-
25- terraform_variables = """variable "region" {{
26- description = "AWS region"
3+ modules_dir = os .path .join (project_name , "modules" )
4+ docker_dir = os .path .join (modules_dir , "docker" )
5+
6+ # Create project directories
7+ os .makedirs (docker_dir , exist_ok = True )
8+
9+ # Create main.tf at root
10+ with open (os .path .join (project_name , "main.tf" ), "w" ) as main_file :
11+ main_file .write ('''provider "docker" {
12+ host = var.docker_host
13+ }
14+
15+ module "docker" {
16+ source = "./modules/docker"
17+ image = var.image
18+ name = var.container_name
19+ ports = var.ports
20+ }
21+ ''' )
22+
23+ # Create variables.tf at root
24+ with open (os .path .join (project_name , "variables.tf" ), "w" ) as variables_file :
25+ variables_file .write ('''variable "docker_host" {
26+ description = "The Docker host Uri."
27+ type = string
28+ }
29+
30+ variable "image" {
31+ description = "The Docker image to use."
2732 type = string
28- default = "us-east-1"
29- }}
33+ }
3034
31- variable "instance_type" {{
32- description = "EC2 Instance type"
35+ variable "container_name" {
36+ description = "The name of the Docker container."
37+ type = string
38+ }
39+
40+ variable "ports" {
41+ description = "List of ports to expose."
42+ type = list(string)
43+ }
44+ ''' )
45+
46+ # Create terraform.tfvars at root
47+ with open (os .path .join (project_name , "terraform.tfvars" ), "w" ) as tfvars_file :
48+ tfvars_file .write ('''docker_host = "tcp://localhost:2375"
49+ image = "nginx:latest"
50+ container_name = "my_nginx"
51+ ports = ["80:80"]
52+ ''' )
53+
54+ # Create versions.tf at root
55+ with open (os .path .join (project_name , "versions.tf" ), "w" ) as versions_file :
56+ versions_file .write ('''terraform {
57+ required_version = ">= 1.0"
58+
59+ required_providers {
60+ docker = {
61+ source = "hashicorp/docker"
62+ version = ">= 2.0"
63+ }
64+ }
65+ }
66+ ''' )
67+
68+ # Create outputs.tf at root
69+ with open (os .path .join (project_name , "outputs.tf" ), "w" ) as outputs_file :
70+ outputs_file .write ('''output "container_id" {
71+ description = "The ID of the Docker container."
72+ value = module.docker.container_id
73+ }
74+
75+ output "container_ip" {
76+ description = "The IP address of the Docker container."
77+ value = module.docker.container_ip
78+ }
79+ ''' )
80+
81+ # Create main.tf in modules/docker
82+ with open (os .path .join (docker_dir , "main.tf" ), "w" ) as docker_main_file :
83+ docker_main_file .write ('''resource "docker_container" "this" {
84+ name = var.name
85+ image = var.image
86+ ports {
87+ internal = var.ports[0]
88+ external = var.ports[1]
89+ }
90+ }
91+ ''' )
92+
93+ # Create variables.tf in modules/docker
94+ with open (os .path .join (docker_dir , "variables.tf" ), "w" ) as docker_variables_file :
95+ docker_variables_file .write ('''variable "image" {
96+ description = "The Docker image to use."
3397 type = string
34- default = "t2.micro"
35- }}
98+ }
3699
37- variable "ami" { {
38- description = "AMI ID "
100+ variable "name" {
101+ description = "The name of the Docker container. "
39102 type = string
40- default = "ami-0c55b159cbfafe1f0"
41- }}
42- """
43-
44- github_actions = """name: Terraform CI
45-
46- on:
47- push:
48- branches:
49- - main
50- pull_request:
51- branches:
52- - main
53-
54- jobs:
55- terraform:
56- runs-on: ubuntu-latest
57-
58- steps:
59- - name: Checkout code
60- uses: actions/checkout@v2
61-
62- - name: Set up Terraform
63- uses: hashicorp/setup-terraform@v1
64- with:
65- terraform_version: 1.0.0
66-
67- - name: Terraform Init
68- run: terraform init
69-
70- - name: Terraform Plan
71- run: terraform plan
72-
73- - name: Terraform Apply
74- run: terraform apply -auto-approve
75- env:
76- TF_VAR_region: ${{ secrets.AWS_REGION }}
77- TF_VAR_instance_type: ${{ secrets.AWS_INSTANCE_TYPE }}
78- TF_VAR_ami: ${{ secrets.AWS_AMI }}
79- """
80-
81- with open (os .path .join (base_directory , "main.tf" ), "w" ) as f :
82- f .write (terraform_main )
83-
84- with open (os .path .join (base_directory , "variables.tf" ), "w" ) as f :
85- f .write (terraform_variables )
86-
87- with open (os .path .join (ci_directory , "terraform-ci.yml" ), "w" ) as f :
88- f .write (github_actions )
103+ }
104+
105+ variable "ports" {
106+ description = "List of ports for the container."
107+ type = list(string)
108+ }
109+ ''' )
110+
111+ # Create terraform.tfvars in modules/docker
112+ with open (os .path .join (docker_dir , "terraform.tfvars" ), "w" ) as docker_tfvars_file :
113+ docker_tfvars_file .write ('''image = "nginx:latest"
114+ name = "my_nginx"
115+ ports = ["80", "80"]
116+ ''' )
117+
118+ # Create versions.tf in modules/docker
119+ with open (os .path .join (docker_dir , "versions.tf" ), "w" ) as docker_versions_file :
120+ docker_versions_file .write ('''terraform {
121+ required_version = ">= 1.0"
122+
123+ required_providers {
124+ docker = {
125+ source = "hashicorp/docker"
126+ version = ">= 2.0"
127+ }
128+ }
129+ }
130+ ''' )
131+
132+ # Create outputs.tf in modules/docker
133+ with open (os .path .join (docker_dir , "outputs.tf" ), "w" ) as docker_outputs_file :
134+ docker_outputs_file .write ('''output "container_id" {
135+ description = "The ID of the Docker container."
136+ value = docker_container.this.id
137+ }
138+
139+ output "container_ip" {
140+ description = "The IP address of the Docker container."
141+ value = docker_container.this.ip_address
142+ }
143+ ''' )
0 commit comments