From fa0bd115bbac038246d86f14301b803dc26a2f31 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Wed, 24 May 2017 16:36:33 -0600 Subject: [PATCH 1/3] Commit first run at using modules Commit a set of files that show the use of modules Signed-off-by: Scott Lowe --- terraform/aws/simple-module/main.tf | 23 ++++++++++++ .../aws/simple-module/modules/vpc/main.tf | 36 +++++++++++++++++++ .../aws/simple-module/modules/vpc/output.tf | 11 ++++++ .../simple-module/modules/vpc/variables.tf | 29 +++++++++++++++ terraform/aws/simple-module/provider.tf | 3 ++ terraform/aws/simple-module/variables.tf | 4 +++ 6 files changed, 106 insertions(+) create mode 100644 terraform/aws/simple-module/main.tf create mode 100644 terraform/aws/simple-module/modules/vpc/main.tf create mode 100644 terraform/aws/simple-module/modules/vpc/output.tf create mode 100644 terraform/aws/simple-module/modules/vpc/variables.tf create mode 100644 terraform/aws/simple-module/provider.tf create mode 100644 terraform/aws/simple-module/variables.tf diff --git a/terraform/aws/simple-module/main.tf b/terraform/aws/simple-module/main.tf new file mode 100644 index 0000000..8a47463 --- /dev/null +++ b/terraform/aws/simple-module/main.tf @@ -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-1" + 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" +} diff --git a/terraform/aws/simple-module/modules/vpc/main.tf b/terraform/aws/simple-module/modules/vpc/main.tf new file mode 100644 index 0000000..8b29d73 --- /dev/null +++ b/terraform/aws/simple-module/modules/vpc/main.tf @@ -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}" +} diff --git a/terraform/aws/simple-module/modules/vpc/output.tf b/terraform/aws/simple-module/modules/vpc/output.tf new file mode 100644 index 0000000..a41b292 --- /dev/null +++ b/terraform/aws/simple-module/modules/vpc/output.tf @@ -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}" +} diff --git a/terraform/aws/simple-module/modules/vpc/variables.tf b/terraform/aws/simple-module/modules/vpc/variables.tf new file mode 100644 index 0000000..2201506 --- /dev/null +++ b/terraform/aws/simple-module/modules/vpc/variables.tf @@ -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" +} diff --git a/terraform/aws/simple-module/provider.tf b/terraform/aws/simple-module/provider.tf new file mode 100644 index 0000000..d7c130e --- /dev/null +++ b/terraform/aws/simple-module/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.user_region}" +} diff --git a/terraform/aws/simple-module/variables.tf b/terraform/aws/simple-module/variables.tf new file mode 100644 index 0000000..7b8a12a --- /dev/null +++ b/terraform/aws/simple-module/variables.tf @@ -0,0 +1,4 @@ +variable "user_region" { + type = "string" + description = "AWS region to use for all resources" +} From 23f55bbdcc24a90b5cf4dd2b26cc095264df437e Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 25 May 2017 09:02:44 -0600 Subject: [PATCH 2/3] Fix small typo Fix a small typo in one of the attributes of the VPCs Signed-off-by: Scott Lowe --- terraform/aws/simple-module/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform/aws/simple-module/main.tf b/terraform/aws/simple-module/main.tf index 8a47463..f5e391e 100644 --- a/terraform/aws/simple-module/main.tf +++ b/terraform/aws/simple-module/main.tf @@ -13,7 +13,7 @@ module "vpc20" { module "vpc50" { source = "./modules/vpc" - name = "test-vpc-1" + name = "test-vpc-2" vpc_cidr_block = "10.50.0.0/16" vpc_dns_hostnames = "true" vpc_dns_support = "true" From 0f20740eece07cd8f77df6c6bb99dc6e1797f9f6 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 25 May 2017 09:06:48 -0600 Subject: [PATCH 3/3] Update README.md for Terraform/AWS environments Update the README.md for Terraform/AWS environments to include a brief description of the new `simple-module` directory Signed-off-by: Scott Lowe --- terraform/aws/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/terraform/aws/README.md b/terraform/aws/README.md index f0ba8dc..86783f0 100644 --- a/terraform/aws/README.md +++ b/terraform/aws/README.md @@ -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.