├── LICENSE ├── README.md ├── base ├── main.tf ├── outputs.tf └── variables.tf └── web ├── main.tf ├── outputs.tf └── variables.tf /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Avi Tzurel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # terraform-example-with-modules 2 | 3 | This is an example of dividing terraform definitions into modules. Basically 4 | taking the `two-tier` example from the terraform repository and modulirizing 5 | it. 6 | 7 | 8 | You can read more about it in the blog post http://avi.io/blog/2016/01/05/using-modules-in-terraform/ 9 | -------------------------------------------------------------------------------- /base/main.tf: -------------------------------------------------------------------------------- 1 | # Specify the provider and access details 2 | 3 | resource "aws_key_pair" "auth" { 4 | key_name = "${var.key_name}" 5 | public_key = "${file(var.public_key_path)}" 6 | } 7 | 8 | provider "aws" { 9 | region = "${var.aws_region}" 10 | } 11 | 12 | # Create a VPC to launch our instances into 13 | resource "aws_vpc" "default" { 14 | cidr_block = "10.0.0.0/16" 15 | } 16 | 17 | # Create an internet gateway to give our subnet access to the outside world 18 | resource "aws_internet_gateway" "default" { 19 | vpc_id = "${aws_vpc.default.id}" 20 | } 21 | 22 | # Grant the VPC internet access on its main route table 23 | resource "aws_route" "internet_access" { 24 | route_table_id = "${aws_vpc.default.main_route_table_id}" 25 | destination_cidr_block = "0.0.0.0/0" 26 | gateway_id = "${aws_internet_gateway.default.id}" 27 | } 28 | 29 | # Create a subnet to launch our instances into 30 | resource "aws_subnet" "default" { 31 | vpc_id = "${aws_vpc.default.id}" 32 | cidr_block = "10.0.1.0/24" 33 | map_public_ip_on_launch = true 34 | } 35 | 36 | # Our default security group to access 37 | # the instances over SSH and HTTP 38 | resource "aws_security_group" "default" { 39 | name = "terraform_example" 40 | description = "Used in the terraform" 41 | vpc_id = "${aws_vpc.default.id}" 42 | 43 | # SSH access from anywhere 44 | ingress { 45 | from_port = 22 46 | to_port = 22 47 | protocol = "tcp" 48 | cidr_blocks = ["0.0.0.0/0"] 49 | } 50 | 51 | # HTTP access from the VPC 52 | ingress { 53 | from_port = 80 54 | to_port = 80 55 | protocol = "tcp" 56 | cidr_blocks = ["10.0.0.0/16"] 57 | } 58 | 59 | # outbound internet access 60 | egress { 61 | from_port = 0 62 | to_port = 0 63 | protocol = "-1" 64 | cidr_blocks = ["0.0.0.0/0"] 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /base/outputs.tf: -------------------------------------------------------------------------------- 1 | output "default_vpc_id" { 2 | value = "${aws_vpc.default.id}" 3 | } 4 | 5 | output "default_security_group_id" { 6 | value = "${aws_security_group.default.id}" 7 | } 8 | 9 | output "default_subnet_id" { 10 | value = "${aws_subnet.default.id}" 11 | } 12 | 13 | output "aws_key_pair_id" { 14 | source "${aws_key_pair.id.auth.id}" 15 | } 16 | -------------------------------------------------------------------------------- /base/variables.tf: -------------------------------------------------------------------------------- 1 | variable "public_key_path" { 2 | description = <