Merge pull request #78 from lowescott/module1

Add an example of using Terraform modules
This commit is contained in:
Scott S. Lowe 2017-05-25 09:08:58 -06:00 committed by GitHub
commit 9476c3ab2f
7 changed files with 108 additions and 0 deletions

View file

@ -12,6 +12,8 @@ This directory contains a number of subdirectories with example configurations i
* **simple-ec2**: This directory contains a simple Terraform configuration for spinning up an Ubuntu 14.04 EC2 instance in an account's default VPC.
* **simple-module**: This directory contains a very simple example of using a Terraform module for more reusable code blocks.
## Prerequisites
These instructions assume you've already installed Terraform and that you have access to a valid AWS account. Installing Terraform and/or working with AWS accounts are not covered in any of these materials.

View file

@ -0,0 +1,23 @@
module "vpc20" {
source = "./modules/vpc"
name = "test-vpc-1"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_cidr_block = "10.20.1.0/24"
subnet_map_pub_ip = "true"
}
module "vpc50" {
source = "./modules/vpc"
name = "test-vpc-2"
vpc_cidr_block = "10.50.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_cidr_block = "10.50.1.0/24"
subnet_map_pub_ip = "true"
}

View file

@ -0,0 +1,36 @@
# Create a new VPC
resource "aws_vpc" "vpc" {
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
}
# Create a public subnet in the new VPC
resource "aws_subnet" "subnet" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.subnet_cidr_block}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
# Create a route table for the new VPC
resource "aws_route_table" "routes" {
vpc_id = "${aws_vpc.vpc.id}"
}
# Create a route in new route table
resource "aws_route" "default_route" {
route_table_id = "${aws_route_table.routes.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
# Associate route table with subnet in VPC
resource "aws_route_table_association" "rte_tbl_assoc" {
subnet_id = "${aws_subnet.subnet.id}"
route_table_id = "${aws_route_table.routes.id}"
}

View file

@ -0,0 +1,11 @@
output "new_vpc_id" {
value = "${aws_vpc.vpc.id}"
}
output "new_subnet_id" {
value = "${aws_subnet.subnet.id}"
}
output "new_subnet_az" {
value = "${aws_subnet.subnet.availability_zone}"
}

View file

@ -0,0 +1,29 @@
variable "name" {
type = "string"
description = "Name prefix to use for networking resources"
}
variable "vpc_cidr_block" {
type = "string"
description = "CIDR block to use for new VPC"
}
variable "vpc_dns_hostnames" {
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
}
variable "vpc_dns_support" {
type = "string"
description = "True/False to enable DNS support in new VPC"
}
variable "subnet_cidr_block" {
type = "string"
description = "CIDR block (in VPC CIDR block) to use for new subnet"
}
variable "subnet_map_pub_ip" {
type = "string"
description = "True/False to map public IP addresses on launch"
}

View file

@ -0,0 +1,3 @@
provider "aws" {
region = "${var.user_region}"
}

View file

@ -0,0 +1,4 @@
variable "user_region" {
type = "string"
description = "AWS region to use for all resources"
}