{"id":51507962,"url":"https://github.com/explicit-logic/terraform-module-12.2","last_synced_at":"2026-07-08T02:30:44.303Z","repository":{"id":352962868,"uuid":"1216344968","full_name":"explicit-logic/terraform-module-12.2","owner":"explicit-logic","description":"Modularize Project","archived":false,"fork":false,"pushed_at":"2026-04-21T21:08:17.000Z","size":3863,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-21T23:17:20.207Z","etag":null,"topics":["aws","devops-bootcamp","docker","git","github","linux","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/explicit-logic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-20T20:12:10.000Z","updated_at":"2026-04-21T21:09:17.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/explicit-logic/terraform-module-12.2","commit_stats":null,"previous_names":["explicit-logic/terraform-module-12.2"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/explicit-logic/terraform-module-12.2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explicit-logic%2Fterraform-module-12.2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explicit-logic%2Fterraform-module-12.2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explicit-logic%2Fterraform-module-12.2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explicit-logic%2Fterraform-module-12.2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/explicit-logic","download_url":"https://codeload.github.com/explicit-logic/terraform-module-12.2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/explicit-logic%2Fterraform-module-12.2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35249883,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-08T02:00:06.796Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","devops-bootcamp","docker","git","github","linux","terraform"],"created_at":"2026-07-08T02:30:43.359Z","updated_at":"2026-07-08T02:30:44.292Z","avatar_url":"https://github.com/explicit-logic.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Module 12 - Infrastructure as Code with Terraform\n\nThis repository contains a demo project created as part of my **DevOps studies** in the [TechWorld with Nana – DevOps Bootcamp](https://www.techworld-with-nana.com/devops-bootcamp).\n\n**Demo Project:** Modularize Project\n\n**Technologies used:** Terraform, AWS, Docker, Linux, Git\n\n**Project Description:**\n\n- Divide Terraform resources into reusable modules\n\n---\n\n### Prerequisites\n\nComplete this demo project before start:\n\nAutomate AWS Infrastructure\n\nhttps://github.com/explicit-logic/terraform-module-12.1\n\nCreate `terraform.tfvars` and set your values\n\n```sh\ncp terraform.tfvars.example terraform.tfvars\n```\n\n---\n\n### Modules\n\n\u003e Container for multiple resources, used together\n\nWhy Modules?\n\n- Organize and group configurations\n- Encapsulate into distinct logical components\n- Re-use\n- Group multiple resources into a logical unit\n\n```\nInput variables -\u003e Module -\u003e Output values\n```\n\n- **Input variables** — like function arguments\n- **Output values** — like function return values\n\n---\n\n### Project structure\n\n```\nterraform-module-12.2/\n├── main.tf                 # Root: provider, VPC, module calls\n├── variables.tf            # Root input variables\n├── outputs.tf              # Root outputs (ec2_public_ip)\n├── providers.tf            # Terraform + AWS provider version pin\n├── terraform.tfvars        # Your values (gitignored)\n├── terraform.tfvars.example\n├── entry-script.sh         # EC2 user_data bootstrap (Docker + nginx)\n└── modules/\n    ├── subnet/\n    │   ├── main.tf         # aws_subnet, aws_internet_gateway, aws_default_route_table\n    │   ├── variables.tf\n    │   ├── outputs.tf      # subnet\n    │   └── providers.tf\n    └── webserver/\n        ├── main.tf         # aws_default_security_group, aws_ami, aws_key_pair, aws_instance\n        ├── variables.tf\n        ├── outputs.tf      # instance\n        └── providers.tf\n```\n\n---\n\n### Prepare modules directory\n\n- Create `modules` directory and subfolders for modules: `subnet`, `webserver`\n- Create tf files inside each module dir: `main.tf`, `outputs.tf`, `variables.tf`, `providers.tf`\n\nEach module's `providers.tf` pins the AWS provider so the module stays self-describing:\n\n```tf\nterraform {\n  required_providers {\n    aws = {\n      source  = \"hashicorp/aws\"\n      version = \"6.41.0\"\n    }\n  }\n}\n```\n\n---\n\n### Create `subnet` module\n\n- Move `\"aws_subnet\" \"myapp-subnet-1\"`, `\"aws_internet_gateway\" \"myapp-igw\"`, and `\"aws_default_route_table\" \"main-rtb\"` to `modules/subnet/main.tf`\n- Replace references to resources that no longer live inside the module with input variables\n- Declare those inputs in the module's `variables.tf`\n\nfile: `modules/subnet/variables.tf`\n```tf\nvariable subnet_cidr_block {}\nvariable avail_zone {}\nvariable env_prefix {}\nvariable vpc_id {}\nvariable default_route_table_id {}\n```\n\n- Expose the subnet as a module output so the root (and other modules) can reference it\n\nfile: `modules/subnet/outputs.tf`\n```tf\noutput \"subnet\" {\n  value = aws_subnet.myapp-subnet-1\n}\n```\n\n- Wire the module into the root `main.tf` and pass variables\n\n```tf\nmodule \"myapp-subnet\" {\n  source = \"./modules/subnet\"\n  subnet_cidr_block = var.subnet_cidr_block\n  avail_zone = var.avail_zone\n  env_prefix = var.env_prefix\n  vpc_id = aws_vpc.myapp-vpc.id\n  default_route_table_id = aws_vpc.myapp-vpc.default_route_table_id\n}\n```\n\n\u003e The `aws_instance.myapp-server` resource's `subnet_id` will now reference the module's output: `subnet_id = module.myapp-subnet.subnet.id`. This reference will move into the webserver module in the next step.\n\n- Apply terraform configuration\n\n```sh\nterraform init\n```\n\n![](./images/init-subnet-module.png)\n\n\u003e `terraform init` is required whenever you add or change a `module` block — it installs/links the module source.\n\n```sh\nterraform plan\n```\n\n![](./images/plan-subnet-module.png)\n\n```sh\nterraform apply --auto-approve\n```\n\n![](./images/apply-subnet-module.png)\n\nEC2 instance was created and running\n![](./images/ec2-instance.png)\n\n---\n\n### Create `webserver` module\n\n- Move `\"aws_default_security_group\" \"default-sg\"`, `\"aws_ami\" \"latest-amazon-linux-image\"` (data source), `\"aws_key_pair\" \"ssh-key\"`, and `\"aws_instance\" \"myapp-server\"` to `modules/webserver/main.tf`\n- Replace external references with input variables\n\nfile: `modules/webserver/variables.tf`\n```tf\nvariable vpc_id {}\nvariable my_ip {}\nvariable env_prefix {}\nvariable image_name {}\nvariable public_key_location {}\nvariable instance_type {}\nvariable subnet_id {}\nvariable avail_zone {}\n```\n\n- Expose the EC2 instance as a module output\n\nfile: `modules/webserver/outputs.tf`\n```tf\noutput \"instance\" {\n  value = aws_instance.myapp-server\n}\n```\n\n- Wire the module into the root `main.tf` and pass variables (including the subnet id from the other module)\n\n```tf\nmodule \"myapp-server\" {\n  source = \"./modules/webserver\"\n  vpc_id = aws_vpc.myapp-vpc.id\n  my_ip = var.my_ip\n  env_prefix = var.env_prefix\n  image_name = var.image_name\n  public_key_location = var.public_key_location\n  instance_type = var.instance_type\n  subnet_id = module.myapp-subnet.subnet.id\n  avail_zone = var.avail_zone\n}\n```\n\n- Surface the public IP at the root so it's visible after `apply`\n\nfile: `outputs.tf`\n```tf\noutput \"ec2_public_ip\" {\n  value = module.myapp-server.instance.public_ip\n}\n```\n\nAfter modularization, the root `main.tf` is left with only the provider, the VPC, and the two module calls:\n\n```tf\nprovider \"aws\" {\n  region = \"eu-central-1\"\n}\n\nresource \"aws_vpc\" \"myapp-vpc\" {\n  cidr_block = var.vpc_cidr_block\n  tags = {\n    Name: \"${var.env_prefix}-vpc\"\n  }\n}\n\nmodule \"myapp-subnet\" { ... }\nmodule \"myapp-server\" { ... }\n```\n\n- Apply terraform configuration\n\n```sh\nterraform init\n```\n\n![](./images/init-webserver-module.png)\n\n```sh\nterraform plan\n```\n\n![](./images/plan-webserver-module.png)\n\n```sh\nterraform apply --auto-approve\n```\n\n![](./images/apple-webserver-module.png)\n\nSSH to the instance\n```sh\nssh ec2-user@\u003cec2_public_ip\u003e\n# verify the user_data script ran\ndocker -v\n```\n\n![](./images/ssh.png)\n\nThe app (nginx in a container) is exposed on port 8080: `http://\u003cec2_public_ip\u003e:8080`.\n\n---\n\n### Clean up\n\nDestroy all managed resources when finished to avoid AWS charges:\n\n```sh\nterraform destroy --auto-approve\n```\n\n---\n\n### Key takeaways\n\n- **Modules are the unit of reuse** — group related resources behind a clean input/output interface\n- **Outputs are the module's public API** — expose only what callers need (`subnet`, `instance`)\n- **Cross-module wiring happens at the root** — one module's output becomes another module's input (`module.myapp-subnet.subnet.id` → `module.myapp-server` as `subnet_id`)\n- **Pin provider versions per module** — each module's own `providers.tf` keeps it portable\n- **Run `terraform init` after adding or changing a module block** — Terraform needs to resolve the new source\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexplicit-logic%2Fterraform-module-12.2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexplicit-logic%2Fterraform-module-12.2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexplicit-logic%2Fterraform-module-12.2/lists"}