├── .gitignore ├── LICENSE ├── NOTICE.txt ├── README.md ├── deploy ├── main.tf ├── outputs.tf └── variables.tf ├── main.tf ├── modules └── vpc │ ├── locals.tf │ ├── main.tf │ ├── outputs.tf │ └── variables.tf ├── outputs.tf ├── setup_workspace ├── variables.tf └── workspace.tf └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | 11 | # Exclude all .tfvars files, which are likely to contain sentitive data, such as 12 | # password, private keys, and other secrets. These should not be part of version 13 | # control as they are data points which are potentially sensitive and subject 14 | # to change depending on the environment. 15 | # 16 | *.tfvars 17 | 18 | # Ignore override files as they are usually used to override resources locally and so 19 | # are not checked in 20 | override.tf 21 | override.tf.json 22 | *_override.tf 23 | *_override.tf.json 24 | 25 | # Include override files you do wish to add to version control using negated pattern 26 | # 27 | # !example_override.tf 28 | 29 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 30 | # example: *tfplan* 31 | 32 | # Ignore CLI configuration files 33 | .terraformrc 34 | terraform.rc 35 | .terraform.lock.hcl 36 | 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2016-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at 4 | 5 | http://aws.amazon.com/apache2.0/ 6 | 7 | or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Terraform AWS VPC 2 | Terraform modules on AWS are available in the Terraform registry on the [AWS Integration and Automation namespace page](https://registry.terraform.io/namespaces/aws-ia). 3 | 4 | To install the Terraform AWS VPC module, see https://registry.terraform.io/modules/aws-ia/vpc/aws/latest. 5 | -------------------------------------------------------------------------------- /deploy/main.tf: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------------------------------------------------- 2 | # REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER 3 | # This module has been updated with 0.15.1 syntax, which means it is no longer compatible with any versions below 0.15.1. 4 | # ---------------------------------------------------------------------------------------------------------------------- 5 | ###################################### 6 | # Defaults 7 | ###################################### 8 | terraform { 9 | required_version = ">= 0.15.1" 10 | backend "remote" {} 11 | } 12 | 13 | provider "aws" { 14 | region = var.region 15 | } 16 | 17 | resource "random_string" "rand4" { 18 | length = 4 19 | special = false 20 | upper = false 21 | } 22 | 23 | module "vpc_label" { 24 | source = "aws-quickstart/label/aws" 25 | version = "0.0.1" 26 | region = var.region 27 | namespace = var.namespace 28 | env = var.env 29 | #account = var.account_name 30 | name = "${var.name}-${random_string.rand4.result}" 31 | delimiter = var.delimiter 32 | tags = tomap({ propogate_at_launch = "true", "terraform" = "true" }) 33 | } 34 | 35 | ###################################### 36 | # Create VPC 37 | ###################################### 38 | module "quickstart_vpc" { 39 | source = "../modules/vpc" 40 | create_vpc = var.create_vpc 41 | name = module.vpc_label.id 42 | region = var.region 43 | cidr = var.cidr 44 | public_subnets = var.public_subnets 45 | private_subnets_A = var.private_subnets_A 46 | private_subnets_B = var.private_subnets_B 47 | tags = module.vpc_label.tags 48 | } 49 | -------------------------------------------------------------------------------- /deploy/outputs.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | output "vpc_cidr" { 3 | description = "VPC_CIDR " 4 | value = module.quickstart_vpc.vpc_cidr 5 | } 6 | output "vpc_id" { 7 | description = "The ID of the VPC" 8 | value = module.quickstart_vpc.vpc_id 9 | } 10 | output "NAT1EIP" { 11 | description = "NAT 1 IP address" 12 | value = module.quickstart_vpc.NAT1EIP 13 | } 14 | 15 | output "NAT2EIP" { 16 | description = " NAT 2 IP address" 17 | value = module.quickstart_vpc.NAT2EIP 18 | } 19 | 20 | output "NAT3EIP" { 21 | description = " NAT 3 IP address" 22 | value = module.quickstart_vpc.NAT3EIP 23 | } 24 | 25 | output "NAT4EIP" { 26 | description = " NAT 4 IP address" 27 | value = module.quickstart_vpc.NAT4EIP 28 | } 29 | 30 | output "PrivateSubnet1ACIDR" { 31 | description = " Private subnet 1A CIDR in Availability Zone 1" 32 | value = module.quickstart_vpc.PrivateSubnet1ACIDR 33 | } 34 | 35 | output "PrivateSubnet1AID" { 36 | description = " Private subnet 1A ID in Availability Zone 1" 37 | value = module.quickstart_vpc.PrivateSubnet1AID 38 | } 39 | 40 | output "PrivateSubnet1BCIDR" { 41 | description = " Private subnet 1B CIDR in Availability Zone 1" 42 | value = module.quickstart_vpc.PrivateSubnet1BCIDR 43 | } 44 | 45 | output "PrivateSubnet1BID" { 46 | description = " Private subnet 1B ID in Availability Zone 1" 47 | value = module.quickstart_vpc.PrivateSubnet1BID 48 | } 49 | 50 | output "PrivateSubnet2ACIDR" { 51 | description = " Private subnet 2A CIDR in Availability Zone 2" 52 | value = module.quickstart_vpc.PrivateSubnet2ACIDR 53 | } 54 | 55 | output "PrivateSubnet2AID" { 56 | description = " Private subnet 2A ID in Availability Zone 2" 57 | value = module.quickstart_vpc.PrivateSubnet2AID 58 | } 59 | 60 | output "PrivateSubnet2BCIDR" { 61 | description = " Private subnet 2B CIDR in Availability Zone 2" 62 | value = module.quickstart_vpc.PrivateSubnet2BCIDR 63 | } 64 | 65 | output "PrivateSubnet2BID" { 66 | description = " Private subnet 2B ID in Availability Zone 2" 67 | value = module.quickstart_vpc.PrivateSubnet2BID 68 | } 69 | 70 | output "PrivateSubnet3ACIDR" { 71 | description = " Private subnet 3A CIDR in Availability Zone 3" 72 | value = module.quickstart_vpc.PrivateSubnet3ACIDR 73 | } 74 | 75 | output "PrivateSubnet3AID" { 76 | description = " Private subnet 3A ID in Availability Zone 3" 77 | value = module.quickstart_vpc.PrivateSubnet3AID 78 | } 79 | 80 | output "PrivateSubnet3BCIDR" { 81 | description = " Private subnet 3B CIDR in Availability Zone 3" 82 | value = module.quickstart_vpc.PrivateSubnet3BCIDR 83 | } 84 | 85 | output "PrivateSubnet3BID" { 86 | description = " Private subnet 3B ID in Availability Zone 3" 87 | value = module.quickstart_vpc.PrivateSubnet3BID 88 | } 89 | 90 | output "PrivateSubnet4ACIDR" { 91 | description = " Private subnet 4A CIDR in Availability Zone 4" 92 | value = module.quickstart_vpc.PrivateSubnet4ACIDR 93 | } 94 | 95 | output "PrivateSubnet4AID" { 96 | description = " Private subnet 4A ID in Availability Zone 4" 97 | value = module.quickstart_vpc.PrivateSubnet4AID 98 | } 99 | 100 | output "PrivateSubnet4BCIDR" { 101 | description = " Private subnet 4B CIDR in Availability Zone 4" 102 | value = module.quickstart_vpc.PrivateSubnet4BCIDR 103 | } 104 | 105 | output "PrivateSubnet4BID" { 106 | description = " Private subnet 4B ID in Availability Zone 4" 107 | value = module.quickstart_vpc.PrivateSubnet4BID 108 | } 109 | 110 | output "PublicSubnet1CIDR" { 111 | description = " Public subnet 1 CIDR in Availability Zone 1" 112 | value = module.quickstart_vpc.PublicSubnet1CIDR 113 | } 114 | 115 | output "PublicSubnet1ID" { 116 | description = " Public subnet 1 ID in Availability Zone 1" 117 | value = module.quickstart_vpc.PublicSubnet1ID 118 | } 119 | 120 | output "PublicSubnet2CIDR" { 121 | description = " Public subnet 2 CIDR in Availability Zone 2" 122 | value = module.quickstart_vpc.PublicSubnet2CIDR 123 | } 124 | 125 | output "PublicSubnet2ID" { 126 | description = " Public subnet 2 ID in Availability Zone 2" 127 | value = module.quickstart_vpc.PublicSubnet2ID 128 | } 129 | 130 | output "PublicSubnet3CIDR" { 131 | description = " Public subnet 3 CIDR in Availability Zone 3" 132 | value = module.quickstart_vpc.PublicSubnet3CIDR 133 | } 134 | 135 | output "PublicSubnet3ID" { 136 | description = " Public subnet 3 ID in Availability Zone 3" 137 | value = module.quickstart_vpc.PublicSubnet3ID 138 | } 139 | 140 | output "PublicSubnet4CIDR" { 141 | description = " Public subnet 4 CIDR in Availability Zone 4" 142 | value = module.quickstart_vpc.PublicSubnet4CIDR 143 | } 144 | 145 | output "PublicSubnet4ID" { 146 | description = " Public subnet 4 ID in Availability Zone 4" 147 | value = module.quickstart_vpc.PublicSubnet4ID 148 | } 149 | 150 | output "S3VPCEndpoint" { 151 | description = " S3 VPC Endpoint" 152 | value = module.quickstart_vpc.S3VPCEndpoint 153 | } 154 | 155 | output "PrivateSubnet1ARouteTable" { 156 | description = " Private subnet 1A route table" 157 | value = module.quickstart_vpc.PrivateSubnet1ARouteTable 158 | } 159 | 160 | output "PrivateSubnet1BRouteTable" { 161 | description = " Private subnet 1B route table" 162 | value = module.quickstart_vpc.PrivateSubnet1BRouteTable 163 | } 164 | 165 | output "PrivateSubnet2ARouteTable" { 166 | description = " Private subnet 2A route table" 167 | value = module.quickstart_vpc.PrivateSubnet2ARouteTable 168 | } 169 | 170 | output "PrivateSubnet2BRouteTable" { 171 | description = " Private subnet 2B route table" 172 | value = module.quickstart_vpc.PrivateSubnet2BRouteTable 173 | } 174 | 175 | output "PrivateSubnet3ARouteTable" { 176 | description = " Private subnet 3A route table" 177 | value = module.quickstart_vpc.PrivateSubnet3ARouteTable 178 | } 179 | 180 | output "PrivateSubnet3BRouteTable" { 181 | description = " Private subnet 3B route table" 182 | value = module.quickstart_vpc.PrivateSubnet3BRouteTable 183 | } 184 | 185 | output "PrivateSubnet4ARouteTable" { 186 | description = " Private subnet 4A route table" 187 | value = module.quickstart_vpc.PrivateSubnet4ARouteTable 188 | } 189 | 190 | output "PrivateSubnet4BRouteTable" { 191 | description = " Private subnet 4B route table" 192 | value = module.quickstart_vpc.PrivateSubnet4BRouteTable 193 | } 194 | 195 | output "PublicSubnetRouteTable" { 196 | description = " Public subnet route table" 197 | value = module.quickstart_vpc.PublicSubnetRouteTable 198 | } 199 | -------------------------------------------------------------------------------- /deploy/variables.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | description = "Sets the region" 3 | type = string 4 | default = "us-west-2" 5 | } 6 | 7 | variable "namespace" { 8 | description = "Namespace, which could be your organiation name, e.g. amazon" 9 | default = "myorg" 10 | } 11 | variable "env" { 12 | description = "Environment, e.g. 'sit', 'uat', 'prod' etc" 13 | default = "dev" 14 | } 15 | variable "account" { 16 | description = "Account, which could be AWS Account Name or Number" 17 | default = "test" 18 | } 19 | variable "name" { 20 | description = "vpc name" 21 | default = "vpc1" 22 | } 23 | variable "delimiter" { 24 | description = "Delimiter, which could be used between name, namespace and env" 25 | default = "-" 26 | } 27 | 28 | variable "tags" { 29 | default = {} 30 | description = "Tags, which could be used for additional tags" 31 | } 32 | 33 | variable "create_vpc" { 34 | description = "Controls if VPC should be created (it affects almost all resources)" 35 | type = bool 36 | default = true 37 | } 38 | 39 | variable "enable_dns_hostnames" { 40 | description = "Should be true to enable DNS hostnames in the VPC" 41 | type = bool 42 | default = true 43 | } 44 | 45 | variable "enable_dns_support" { 46 | description = "Should be true to enable DNS support in the VPC" 47 | type = bool 48 | default = true 49 | } 50 | 51 | variable "instance_tenancy" { 52 | description = "A tenancy option for instances launched into the VPC" 53 | type = string 54 | default = "default" 55 | } 56 | 57 | variable "cidr" { 58 | description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden" 59 | type = string 60 | default = "10.0.0.0/16" 61 | } 62 | 63 | variable "public_subnets" { 64 | description = "A list of private subnets inside the VPC" 65 | type = list(string) 66 | default = ["10.0.128.0/20", "10.0.144.0/20", "10.0.160.0/20"] 67 | } 68 | 69 | variable "private_subnets_A" { 70 | description = "A list of private subnets inside the VPC" 71 | type = list(string) 72 | default = ["10.0.96.0/19", "10.0.232.0/22", "10.0.236.0/22"] 73 | } 74 | 75 | variable "private_subnets_B" { 76 | description = "A list of private subnets inside the VPC" 77 | type = list(string) 78 | default = ["10.0.216.0/21", "10.0.224.0/22", "10.0.228.0/22"] 79 | } 80 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------------------------------------------------- 2 | # REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER 3 | # This module has been updated with 0.15.1 syntax, which means it is no longer compatible with any versions below 0.15.1. 4 | # ---------------------------------------------------------------------------------------------------------------------- 5 | ###################################### 6 | # Defaults 7 | ###################################### 8 | terraform { 9 | required_version = ">= 0.15.1" 10 | backend "remote" {} 11 | } 12 | 13 | provider "aws" { 14 | region = var.region 15 | profile = "default" 16 | } 17 | 18 | resource "random_pet" "name" { 19 | prefix = "tfm-aws" 20 | length = 1 21 | } 22 | 23 | ###################################### 24 | # Create VPC 25 | ###################################### 26 | 27 | module "tfm-aws-vpc" { 28 | source = "./modules/vpc" 29 | region = var.region 30 | name = random_pet.name.id 31 | cidr = "10.0.0.0/16" 32 | public_subnets = ["10.0.128.0/20", "10.0.144.0/20", "10.0.160.0/20", "10.0.176.0/20", "10.0.240.0/22", "10.0.244.0/22"] 33 | private_subnets_A = ["10.0.0.0/19", "10.0.32.0/19", "10.0.64.0/19", "10.0.96.0/19", "10.0.232.0/22", "10.0.236.0/22"] 34 | private_subnets_B = ["10.0.192.0/21", "10.0.200.0/21", "10.0.208.0/21", "10.0.216.0/21", "10.0.224.0/22", "10.0.228.0/22"] 35 | tags = var.tags 36 | } -------------------------------------------------------------------------------- /modules/vpc/locals.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | max_subnet_length = max( 3 | length(var.private_subnets_A), 4 | length(var.private_subnets_B), 5 | ) 6 | nat_gateway_count = local.max_subnet_length 7 | 8 | # Use `local.vpc_id` to give a hint to Terraform that subnets should be deleted before secondary CIDR blocks can be free! 9 | vpc_id = element( 10 | concat( 11 | aws_vpc.main.*.id, 12 | [""], 13 | ), 14 | 0, 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /modules/vpc/main.tf: -------------------------------------------------------------------------------- 1 | ########### 2 | # Defaults 3 | ########## 4 | 5 | terraform { 6 | required_version = ">= 0.13" 7 | required_providers { 8 | aws = { 9 | source = "hashicorp/aws" 10 | version = ">= 3.10" 11 | } 12 | } 13 | backend "remote" {} 14 | } 15 | 16 | provider "aws" { 17 | region = var.region 18 | } 19 | 20 | data "aws_availability_zones" "available" { 21 | state = "available" 22 | } 23 | 24 | ###### 25 | # VPC 26 | ###### 27 | resource "aws_vpc" "main" { 28 | count = var.create_vpc == true ? 1 : 0 29 | 30 | cidr_block = var.cidr 31 | instance_tenancy = var.instance_tenancy 32 | enable_dns_hostnames = var.enable_dns_hostnames 33 | enable_dns_support = var.enable_dns_support 34 | 35 | tags = merge( 36 | var.tags, 37 | ) 38 | } 39 | 40 | resource "aws_vpc_endpoint" "s3" { 41 | count = var.create_vpc == true ? 1 : 0 42 | 43 | vpc_id = aws_vpc.main[count.index].id 44 | service_name = "com.amazonaws.${var.region}.s3" 45 | 46 | tags = merge( 47 | var.tags, 48 | ) 49 | } 50 | 51 | resource "aws_vpc_endpoint_route_table_association" "private_A" { 52 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? 1 : 0 53 | 54 | route_table_id = aws_route_table.private_A[count.index].id 55 | vpc_endpoint_id = aws_vpc_endpoint.s3[0].id 56 | } 57 | 58 | resource "aws_vpc_endpoint_route_table_association" "private_B" { 59 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 60 | 61 | route_table_id = aws_route_table.private_B[count.index].id 62 | vpc_endpoint_id = aws_vpc_endpoint.s3[0].id 63 | } 64 | 65 | ################### 66 | # Internet Gateway 67 | ################### 68 | resource "aws_internet_gateway" "gw" { 69 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 70 | vpc_id = aws_vpc.main[count.index].id 71 | 72 | tags = { 73 | Name = "${var.name}_iGW" 74 | } 75 | 76 | } 77 | 78 | ################ 79 | # Publiс routes 80 | ################ 81 | resource "aws_route_table" "public" { 82 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 83 | vpc_id = aws_vpc.main[count.index].id 84 | 85 | tags = { 86 | Name = "${var.name}-public_routes" 87 | } 88 | 89 | } 90 | 91 | resource "aws_route" "public_internet_gateway" { 92 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 93 | route_table_id = aws_route_table.public[count.index].id 94 | destination_cidr_block = "0.0.0.0/0" 95 | gateway_id = aws_internet_gateway.gw[count.index].id 96 | 97 | timeouts { 98 | create = "5m" 99 | } 100 | } 101 | 102 | ################# 103 | # Private routes A 104 | # There are as many routing tables as the number of NAT gateways 105 | ################# 106 | resource "aws_route_table" "private_A" { 107 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? 1 : 0 108 | vpc_id = aws_vpc.main[0].id 109 | 110 | tags = { 111 | Name = "${var.name}_private_routes_A" 112 | } 113 | } 114 | 115 | ################# 116 | # Private routes B 117 | # There are as many routing tables as the number of NAT gateways 118 | ################# 119 | resource "aws_route_table" "private_B" { 120 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 121 | vpc_id = aws_vpc.main[0].id 122 | 123 | tags = { 124 | Name = "${var.name}_private_routes_B" 125 | } 126 | } 127 | 128 | ################ 129 | # Public subnet 130 | ################ 131 | resource "aws_subnet" "public" { 132 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? length(var.public_subnets) : 0 133 | vpc_id = aws_vpc.main[0].id 134 | cidr_block = var.public_subnets[count.index] 135 | availability_zone = length(regexall("^[a-z]{2}-", element(data.aws_availability_zones.available.names, count.index))) > 0 ? element(data.aws_availability_zones.available.names, count.index) : null 136 | map_public_ip_on_launch = true 137 | 138 | tags = merge( 139 | var.tags, 140 | var.public_subnet_tags, 141 | ) 142 | 143 | } 144 | 145 | ################# 146 | # Private subnet A 147 | ################# 148 | resource "aws_subnet" "private_A" { 149 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? length(var.private_subnets_A) : 0 150 | vpc_id = aws_vpc.main[0].id 151 | cidr_block = var.private_subnets_A[count.index] 152 | availability_zone = length(regexall("^[a-z]{2}-", element(data.aws_availability_zones.available.names, count.index))) > 0 ? element(data.aws_availability_zones.available.names, count.index) : null 153 | 154 | tags = merge( 155 | var.tags, 156 | var.private_subnet_tags, 157 | ) 158 | } 159 | 160 | ################# 161 | # Private subnet B 162 | ################# 163 | resource "aws_subnet" "private_B" { 164 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? length(var.private_subnets_B) : 0 165 | vpc_id = aws_vpc.main[0].id 166 | cidr_block = var.private_subnets_B[count.index] 167 | #availability_zone = data.aws_availability_zones.available.names[length(data.aws_availability_zones.available.names)] 168 | availability_zone = length(regexall("^[a-z]{2}-", element(data.aws_availability_zones.available.names, count.index))) > 0 ? element(data.aws_availability_zones.available.names, count.index) : null 169 | 170 | tags = merge( 171 | var.tags, 172 | var.private_subnet_tags, 173 | ) 174 | } 175 | 176 | 177 | ######################## 178 | # Shared Default Network ACLs 179 | ######################## 180 | resource "aws_network_acl" "public" { 181 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 182 | vpc_id = aws_vpc.main[count.index].id 183 | subnet_ids = concat(aws_subnet.private_A.*.id, aws_subnet.public.*.id) 184 | 185 | tags = { 186 | Name = "${var.name}_shared_default_nework_acl" 187 | } 188 | } 189 | 190 | resource "aws_network_acl_rule" "public_inbound" { 191 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 192 | network_acl_id = aws_network_acl.public[0].id 193 | 194 | egress = false 195 | rule_number = var.public_inbound_acl_rules[0]["rule_number"] 196 | rule_action = var.public_inbound_acl_rules[0]["rule_action"] 197 | from_port = lookup(var.public_inbound_acl_rules[0], "from_port", null) 198 | to_port = lookup(var.public_inbound_acl_rules[0], "to_port", null) 199 | icmp_code = lookup(var.public_inbound_acl_rules[0], "icmp_code", null) 200 | icmp_type = lookup(var.public_inbound_acl_rules[0], "icmp_type", null) 201 | protocol = var.public_inbound_acl_rules[0]["protocol"] 202 | cidr_block = lookup(var.public_inbound_acl_rules[0], "cidr_block", null) 203 | } 204 | 205 | resource "aws_network_acl_rule" "public_outbound" { 206 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? 1 : 0 207 | network_acl_id = aws_network_acl.public[0].id 208 | 209 | egress = true 210 | rule_number = var.public_outbound_acl_rules[0]["rule_number"] 211 | rule_action = var.public_outbound_acl_rules[0]["rule_action"] 212 | from_port = lookup(var.public_outbound_acl_rules[0], "from_port", null) 213 | to_port = lookup(var.public_outbound_acl_rules[0], "to_port", null) 214 | icmp_code = lookup(var.public_outbound_acl_rules[0], "icmp_code", null) 215 | icmp_type = lookup(var.public_outbound_acl_rules[0], "icmp_type", null) 216 | protocol = var.public_outbound_acl_rules[0]["protocol"] 217 | cidr_block = lookup(var.public_outbound_acl_rules[0], "cidr_block", null) 218 | } 219 | 220 | 221 | ####################### 222 | # Dedicated Custom Network ACLs 223 | ####################### 224 | resource "aws_network_acl" "custom" { 225 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 226 | vpc_id = aws_vpc.main[count.index].id 227 | subnet_ids = aws_subnet.private_B.*.id 228 | 229 | tags = { 230 | Name = "${var.name}_dedicated_custom_nework_acl" 231 | } 232 | } 233 | 234 | resource "aws_network_acl_rule" "custom_inbound" { 235 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 236 | network_acl_id = aws_network_acl.custom[0].id 237 | 238 | egress = false 239 | rule_number = var.custom_inbound_acl_rules[0]["rule_number"] 240 | rule_action = var.custom_inbound_acl_rules[0]["rule_action"] 241 | from_port = lookup(var.custom_inbound_acl_rules[0], "from_port", null) 242 | to_port = lookup(var.custom_inbound_acl_rules[0], "to_port", null) 243 | icmp_code = lookup(var.custom_inbound_acl_rules[0], "icmp_code", null) 244 | icmp_type = lookup(var.custom_inbound_acl_rules[0], "icmp_type", null) 245 | protocol = var.custom_inbound_acl_rules[0]["protocol"] 246 | cidr_block = lookup(var.custom_inbound_acl_rules[0], "cidr_block", null) 247 | } 248 | 249 | resource "aws_network_acl_rule" "custom_outbound" { 250 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 251 | network_acl_id = aws_network_acl.custom[0].id 252 | 253 | egress = true 254 | rule_number = var.custom_outbound_acl_rules[0]["rule_number"] 255 | rule_action = var.custom_outbound_acl_rules[0]["rule_action"] 256 | from_port = lookup(var.custom_outbound_acl_rules[0], "from_port", null) 257 | to_port = lookup(var.custom_outbound_acl_rules[0], "to_port", null) 258 | icmp_code = lookup(var.custom_outbound_acl_rules[0], "icmp_code", null) 259 | icmp_type = lookup(var.custom_outbound_acl_rules[0], "icmp_type", null) 260 | protocol = var.custom_outbound_acl_rules[0]["protocol"] 261 | cidr_block = lookup(var.custom_outbound_acl_rules[0], "cidr_block", null) 262 | } 263 | 264 | ############## 265 | # NAT Gateway 266 | ############## 267 | 268 | resource "aws_eip" "nat" { 269 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? 1 : 0 270 | vpc = true 271 | 272 | tags = { 273 | Name = "${var.name}_EIP_nat" 274 | } 275 | } 276 | 277 | resource "aws_nat_gateway" "nat_gw" { 278 | #count = length(data.aws_availability_zones.available.names) 279 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? 1 : 0 280 | allocation_id = aws_eip.nat[count.index].id 281 | subnet_id = aws_subnet.public[count.index].id 282 | 283 | tags = { 284 | Name = "${var.name}_EIP_nat_gateway" 285 | } 286 | depends_on = [aws_internet_gateway.gw] 287 | } 288 | 289 | resource "aws_route" "private_A_nat_gateway" { 290 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? 1 : 0 291 | route_table_id = aws_route_table.private_A[count.index].id 292 | destination_cidr_block = "0.0.0.0/0" 293 | nat_gateway_id = aws_nat_gateway.nat_gw[count.index].id 294 | 295 | timeouts { 296 | create = "5m" 297 | } 298 | } 299 | 300 | resource "aws_route" "private_B_nat_gateway" { 301 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? 1 : 0 302 | route_table_id = aws_route_table.private_B[count.index].id 303 | destination_cidr_block = "0.0.0.0/0" 304 | nat_gateway_id = aws_nat_gateway.nat_gw[count.index].id 305 | 306 | timeouts { 307 | create = "5m" 308 | } 309 | } 310 | 311 | ########################## 312 | # Route table association 313 | ########################## 314 | resource "aws_route_table_association" "private_A" { 315 | count = var.create_vpc == true && length(var.private_subnets_A) > 0 ? length(var.private_subnets_A) : 0 316 | subnet_id = aws_subnet.private_A[count.index].id 317 | route_table_id = aws_route_table.private_A[0].id 318 | } 319 | 320 | resource "aws_route_table_association" "private_B" { 321 | count = var.create_vpc == true && length(var.private_subnets_B) > 0 ? length(var.private_subnets_B) : 0 322 | subnet_id = aws_subnet.private_B[count.index].id 323 | route_table_id = aws_route_table.private_B[0].id 324 | } 325 | 326 | resource "aws_route_table_association" "public" { 327 | count = var.create_vpc == true && length(var.public_subnets) > 0 ? length(var.public_subnets) : 0 328 | subnet_id = aws_subnet.public[count.index].id 329 | route_table_id = aws_route_table.public[0].id 330 | } 331 | -------------------------------------------------------------------------------- /modules/vpc/outputs.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | output "vpc_cidr" { 3 | description = "VPC_CIDR " 4 | #value = aws_vpc.main[count.index].cidr_block 5 | value = concat(aws_vpc.main.*.cidr_block, [""])[0] 6 | } 7 | output "vpc_id" { 8 | description = "The ID of the VPC" 9 | #value = aws_vpc.main[count.index].id 10 | value = concat(aws_vpc.main.*.id, [""])[0] 11 | } 12 | output "private_subnets_A" { 13 | description = "List of IDs of privateA subnets" 14 | value = aws_subnet.private_A.*.id 15 | } 16 | output "private_subnets_B" { 17 | description = "List of IDs of privateB subnets" 18 | value = aws_subnet.private_B.*.id 19 | } 20 | output "public_subnets" { 21 | description = "List of IDs of privateB subnets" 22 | value = aws_subnet.public.*.id 23 | } 24 | output "NAT1EIP" { 25 | description = "NAT 1 IP address" 26 | value = try(aws_eip.nat[0].public_ip, "") 27 | } 28 | 29 | output "NAT2EIP" { 30 | description = " NAT 2 IP address" 31 | value = try(aws_eip.nat[1].public_ip, "") 32 | } 33 | 34 | output "NAT3EIP" { 35 | description = " NAT 3 IP address" 36 | value = length(aws_eip.nat.*.public_ip) > 2 ? aws_eip.nat[2].public_ip : null 37 | } 38 | 39 | output "NAT4EIP" { 40 | description = " NAT 4 IP address" 41 | value = length(aws_eip.nat.*.public_ip) > 3 ? aws_eip.nat[3].public_ip : null 42 | } 43 | 44 | output "PrivateSubnet1ACIDR" { 45 | description = " Private subnet 1A CIDR in Availability Zone 1" 46 | value = try(aws_subnet.private_A[0].cidr_block, "") 47 | } 48 | 49 | output "PrivateSubnet1AID" { 50 | description = " Private subnet 1A ID in Availability Zone 1" 51 | value = try(aws_subnet.private_A[0].id, "") 52 | } 53 | 54 | output "PrivateSubnet1BCIDR" { 55 | description = " Private subnet 1B CIDR in Availability Zone 1" 56 | value = try(aws_subnet.private_B[0].cidr_block, "") 57 | } 58 | 59 | output "PrivateSubnet1BID" { 60 | description = " Private subnet 1B ID in Availability Zone 1" 61 | value = try(aws_subnet.private_B[0].id, "") 62 | } 63 | 64 | output "PrivateSubnet2ACIDR" { 65 | description = " Private subnet 2A CIDR in Availability Zone 2" 66 | value = try(aws_subnet.private_A[1].cidr_block, "") 67 | } 68 | 69 | output "PrivateSubnet2AID" { 70 | description = " Private subnet 2A ID in Availability Zone 2" 71 | value = try(aws_subnet.private_A[1].id, "") 72 | } 73 | 74 | output "PrivateSubnet2BCIDR" { 75 | description = " Private subnet 2B CIDR in Availability Zone 2" 76 | value = try(aws_subnet.private_B[1].cidr_block, "") 77 | } 78 | 79 | output "PrivateSubnet2BID" { 80 | description = " Private subnet 2B ID in Availability Zone 2" 81 | value = try(aws_subnet.private_B[1].id, "") 82 | } 83 | 84 | output "PrivateSubnet3ACIDR" { 85 | description = " Private subnet 3A CIDR in Availability Zone 3" 86 | value = length(aws_subnet.private_A.*.cidr_block) > 2 ? aws_subnet.private_A[2].cidr_block : null 87 | } 88 | 89 | output "PrivateSubnet3AID" { 90 | description = " Private subnet 3A ID in Availability Zone 3" 91 | value = length(aws_subnet.private_A.*.id) > 2 ? aws_subnet.private_A[2].id : null 92 | } 93 | 94 | output "PrivateSubnet3BCIDR" { 95 | description = " Private subnet 3B CIDR in Availability Zone 3" 96 | value = length(aws_subnet.private_B.*.cidr_block) > 2 ? aws_subnet.private_B[2].cidr_block : null 97 | } 98 | 99 | output "PrivateSubnet3BID" { 100 | description = " Private subnet 3B ID in Availability Zone 3" 101 | value = length(aws_subnet.private_B.*.id) > 2 ? aws_subnet.private_B[2].id : null 102 | } 103 | 104 | output "PrivateSubnet4ACIDR" { 105 | description = " Private subnet 4A CIDR in Availability Zone 4" 106 | value = length(aws_subnet.private_A.*.cidr_block) > 3 ? aws_subnet.private_A[3].cidr_block : null 107 | } 108 | 109 | output "PrivateSubnet4AID" { 110 | description = " Private subnet 4A ID in Availability Zone 4" 111 | value = length(aws_subnet.private_A.*.id) > 3 ? aws_subnet.private_A[3].id : null 112 | } 113 | 114 | output "PrivateSubnet4BCIDR" { 115 | description = " Private subnet 4B CIDR in Availability Zone 4" 116 | value = length(aws_subnet.private_B.*.cidr_block) > 3 ? aws_subnet.private_B[3].cidr_block : null 117 | } 118 | 119 | output "PrivateSubnet4BID" { 120 | description = " Private subnet 4B ID in Availability Zone 4" 121 | value = length(aws_subnet.private_B.*.id) > 3 ? aws_subnet.private_B[3].id : null 122 | } 123 | 124 | output "PublicSubnet1CIDR" { 125 | description = " Public subnet 1 CIDR in Availability Zone 1" 126 | value = try(aws_subnet.public[0].cidr_block, "") 127 | } 128 | 129 | output "PublicSubnet1ID" { 130 | description = " Public subnet 1 ID in Availability Zone 1" 131 | value = try(aws_subnet.public[0].id, "") 132 | } 133 | 134 | output "PublicSubnet2CIDR" { 135 | description = " Public subnet 2 CIDR in Availability Zone 2" 136 | value = try(aws_subnet.public[1].cidr_block, "") 137 | } 138 | 139 | output "PublicSubnet2ID" { 140 | description = " Public subnet 2 ID in Availability Zone 2" 141 | value = try(aws_subnet.public[1].id, "") 142 | } 143 | 144 | output "PublicSubnet3CIDR" { 145 | description = " Public subnet 3 CIDR in Availability Zone 3" 146 | value = length(aws_subnet.public.*.cidr_block) > 2 ? aws_subnet.public[2].cidr_block : null 147 | } 148 | 149 | output "PublicSubnet3ID" { 150 | description = " Public subnet 3 ID in Availability Zone 3" 151 | value = length(aws_subnet.public.*.id) > 2 ? aws_subnet.public[2].id : null 152 | } 153 | 154 | output "PublicSubnet4CIDR" { 155 | description = " Public subnet 4 CIDR in Availability Zone 4" 156 | value = length(aws_subnet.public.*.cidr_block) > 3 ? aws_subnet.public[3].cidr_block : null 157 | } 158 | 159 | output "PublicSubnet4ID" { 160 | description = " Public subnet 4 ID in Availability Zone 4" 161 | value = length(aws_subnet.public.*.id) > 3 ? aws_subnet.public[3].id : null 162 | } 163 | 164 | output "S3VPCEndpoint" { 165 | description = " S3 VPC Endpoint" 166 | value = aws_vpc_endpoint.s3.*.id 167 | } 168 | 169 | output "PrivateSubnet1ARouteTable" { 170 | description = " Private subnet 1A route table" 171 | value = try(aws_route_table.private_A[0].id, "") 172 | } 173 | 174 | output "PrivateSubnet1BRouteTable" { 175 | description = " Private subnet 1B route table" 176 | value = try(aws_route_table.private_B[0].id, "") 177 | } 178 | 179 | output "PrivateSubnet2ARouteTable" { 180 | description = " Private subnet 2A route table" 181 | value = try(aws_route_table.private_A[1].id, "") 182 | } 183 | 184 | output "PrivateSubnet2BRouteTable" { 185 | description = " Private subnet 2B route table" 186 | value = try(aws_route_table.private_B[1].id, "") 187 | } 188 | 189 | output "PrivateSubnet3ARouteTable" { 190 | description = " Private subnet 3A route table" 191 | value = length(aws_route_table.private_A.*.id) > 2 ? aws_route_table.private_A[2].id : null 192 | } 193 | 194 | output "PrivateSubnet3BRouteTable" { 195 | description = " Private subnet 3B route table" 196 | value = length(aws_route_table.private_B.*.id) > 2 ? aws_route_table.private_B[2].id : null 197 | } 198 | 199 | output "PrivateSubnet4ARouteTable" { 200 | description = " Private subnet 4A route table" 201 | value = length(aws_route_table.private_A.*.id) > 3 ? aws_route_table.private_A[3].id : null 202 | } 203 | 204 | output "PrivateSubnet4BRouteTable" { 205 | description = " Private subnet 4B route table" 206 | value = length(aws_route_table.private_B.*.id) > 3 ? aws_route_table.private_B[3].id : null 207 | } 208 | 209 | output "PublicSubnetRouteTable" { 210 | description = " Public subnet route table" 211 | value = aws_route_table.public.*.id 212 | } 213 | -------------------------------------------------------------------------------- /modules/vpc/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | # --------------------------------------------------------------------------------------------------------------------- 3 | # PARAMETERS 4 | # These parameters have reasonable defaults. 5 | # --------------------------------------------------------------------------------------------------------------------- 6 | variable "create_vpc" { 7 | description = "Controls if VPC should be created (it affects almost all resources)" 8 | type = bool 9 | default = true 10 | } 11 | 12 | variable "name" { 13 | type = string 14 | description = "The name of the resources" 15 | default = "tfm-aws" 16 | } 17 | 18 | variable "region" { 19 | type = string 20 | description = "The name of the region you wish to deploy into" 21 | default = "us-east-1" 22 | } 23 | 24 | variable "tags" { 25 | #type = map(string) 26 | default = {} 27 | description = "tags, which could be used for additional tags" 28 | } 29 | 30 | variable "private_subnet_tags" { 31 | description = "Additional tags for the private subnets" 32 | type = map(string) 33 | default = {} 34 | } 35 | 36 | variable "public_subnet_tags" { 37 | description = "Additional tags for the public subnets" 38 | type = map(string) 39 | default = {} 40 | } 41 | 42 | variable "enable_dns_hostnames" { 43 | description = "Should be true to enable DNS hostnames in the VPC" 44 | type = bool 45 | default = true 46 | } 47 | 48 | variable "enable_dns_support" { 49 | description = "Should be true to enable DNS support in the VPC" 50 | type = bool 51 | default = true 52 | } 53 | 54 | variable "instance_tenancy" { 55 | description = "A tenancy option for instances launched into the VPC" 56 | type = string 57 | default = "default" 58 | } 59 | 60 | variable "cidr" { 61 | description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden" 62 | type = string 63 | default = "10.0.0.0/16" 64 | } 65 | 66 | variable "public_subnets" { 67 | description = "A list of private subnets inside the VPC" 68 | type = list(string) 69 | # default = ["10.0.128.0/20", "10.0.144.0/20", "10.0.160.0/20", "10.0.176.0/20", "10.0.240.0/22", "10.0.244.0/22"] 70 | } 71 | 72 | variable "private_subnets_A" { 73 | description = "A list of private subnets inside the VPC" 74 | type = list(string) 75 | # default = ["10.0.0.0/19", "10.0.32.0/19", "10.0.64.0/19", "10.0.96.0/19", "10.0.232.0/22", "10.0.236.0/22"] 76 | } 77 | 78 | variable "private_subnets_B" { 79 | description = "A list of private subnets inside the VPC" 80 | type = list(string) 81 | # default = ["10.0.192.0/21", "10.0.200.0/21", "10.0.208.0/21", "10.0.216.0/21", "10.0.224.0/22", "10.0.228.0/22"] 82 | } 83 | 84 | variable "public_inbound_acl_rules" { 85 | description = "Public subnets inbound network ACLs" 86 | type = list(map(string)) 87 | 88 | default = [ 89 | { 90 | rule_number = 100 91 | rule_action = "allow" 92 | from_port = 0 93 | to_port = 0 94 | protocol = "-1" 95 | cidr_block = "0.0.0.0/0" 96 | }, 97 | ] 98 | } 99 | 100 | variable "public_outbound_acl_rules" { 101 | description = "Public subnets outbound network ACLs" 102 | type = list(map(string)) 103 | 104 | default = [ 105 | { 106 | rule_number = 100 107 | rule_action = "allow" 108 | from_port = 0 109 | to_port = 0 110 | protocol = "-1" 111 | cidr_block = "0.0.0.0/0" 112 | }, 113 | ] 114 | } 115 | 116 | variable "custom_inbound_acl_rules" { 117 | description = "Custom subnets inbound network ACLs" 118 | type = list(map(string)) 119 | 120 | default = [ 121 | { 122 | rule_number = 100 123 | rule_action = "allow" 124 | from_port = 0 125 | to_port = 0 126 | protocol = "-1" 127 | cidr_block = "0.0.0.0/0" 128 | }, 129 | ] 130 | } 131 | 132 | variable "custom_outbound_acl_rules" { 133 | description = "Custom subnets outbound network ACLs" 134 | type = list(map(string)) 135 | 136 | default = [ 137 | { 138 | rule_number = 100 139 | rule_action = "allow" 140 | from_port = 0 141 | to_port = 0 142 | protocol = "-1" 143 | cidr_block = "0.0.0.0/0" 144 | }, 145 | ] 146 | } 147 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | output "vpc_cidr" { 3 | description = "VPC_CIDR " 4 | value = module.tfm-aws-vpc.vpc_cidr 5 | } 6 | output "vpc_id" { 7 | description = "The ID of the VPC" 8 | value = module.tfm-aws-vpc.vpc_id 9 | } 10 | output "NAT1EIP" { 11 | description = "NAT 1 IP address" 12 | value = module.tfm-aws-vpc.NAT1EIP 13 | } 14 | 15 | output "NAT2EIP" { 16 | description = " NAT 2 IP address" 17 | value = module.tfm-aws-vpc.NAT2EIP 18 | } 19 | 20 | output "NAT3EIP" { 21 | description = " NAT 3 IP address" 22 | value = module.tfm-aws-vpc.NAT3EIP 23 | } 24 | 25 | output "NAT4EIP" { 26 | description = " NAT 4 IP address" 27 | value = module.tfm-aws-vpc.NAT4EIP 28 | } 29 | 30 | output "PrivateSubnet1ACIDR" { 31 | description = " Private subnet 1A CIDR in Availability Zone 1" 32 | value = module.tfm-aws-vpc.PrivateSubnet1ACIDR 33 | } 34 | 35 | output "PrivateSubnet1AID" { 36 | description = " Private subnet 1A ID in Availability Zone 1" 37 | value = module.tfm-aws-vpc.PrivateSubnet1AID 38 | } 39 | 40 | output "PrivateSubnet1BCIDR" { 41 | description = " Private subnet 1B CIDR in Availability Zone 1" 42 | value = module.tfm-aws-vpc.PrivateSubnet1BCIDR 43 | } 44 | 45 | output "PrivateSubnet1BID" { 46 | description = " Private subnet 1B ID in Availability Zone 1" 47 | value = module.tfm-aws-vpc.PrivateSubnet1BID 48 | } 49 | 50 | output "PrivateSubnet2ACIDR" { 51 | description = " Private subnet 2A CIDR in Availability Zone 2" 52 | value = module.tfm-aws-vpc.PrivateSubnet2ACIDR 53 | } 54 | 55 | output "PrivateSubnet2AID" { 56 | description = " Private subnet 2A ID in Availability Zone 2" 57 | value = module.tfm-aws-vpc.PrivateSubnet2AID 58 | } 59 | 60 | output "PrivateSubnet2BCIDR" { 61 | description = " Private subnet 2B CIDR in Availability Zone 2" 62 | value = module.tfm-aws-vpc.PrivateSubnet2BCIDR 63 | } 64 | 65 | output "PrivateSubnet2BID" { 66 | description = " Private subnet 2B ID in Availability Zone 2" 67 | value = module.tfm-aws-vpc.PrivateSubnet2BID 68 | } 69 | 70 | output "PrivateSubnet3ACIDR" { 71 | description = " Private subnet 3A CIDR in Availability Zone 3" 72 | value = module.tfm-aws-vpc.PrivateSubnet3ACIDR 73 | } 74 | 75 | output "PrivateSubnet3AID" { 76 | description = " Private subnet 3A ID in Availability Zone 3" 77 | value = module.tfm-aws-vpc.PrivateSubnet3AID 78 | } 79 | 80 | output "PrivateSubnet3BCIDR" { 81 | description = " Private subnet 3B CIDR in Availability Zone 3" 82 | value = module.tfm-aws-vpc.PrivateSubnet3BCIDR 83 | } 84 | 85 | output "PrivateSubnet3BID" { 86 | description = " Private subnet 3B ID in Availability Zone 3" 87 | value = module.tfm-aws-vpc.PrivateSubnet3BID 88 | } 89 | 90 | output "PrivateSubnet4ACIDR" { 91 | description = " Private subnet 4A CIDR in Availability Zone 4" 92 | value = module.tfm-aws-vpc.PrivateSubnet4ACIDR 93 | } 94 | 95 | output "PrivateSubnet4AID" { 96 | description = " Private subnet 4A ID in Availability Zone 4" 97 | value = module.tfm-aws-vpc.PrivateSubnet4AID 98 | } 99 | 100 | output "PrivateSubnet4BCIDR" { 101 | description = " Private subnet 4B CIDR in Availability Zone 4" 102 | value = module.tfm-aws-vpc.PrivateSubnet4BCIDR 103 | } 104 | 105 | output "PrivateSubnet4BID" { 106 | description = " Private subnet 4B ID in Availability Zone 4" 107 | value = module.tfm-aws-vpc.PrivateSubnet4BID 108 | } 109 | 110 | output "PublicSubnet1CIDR" { 111 | description = " Public subnet 1 CIDR in Availability Zone 1" 112 | value = module.tfm-aws-vpc.PublicSubnet1CIDR 113 | } 114 | 115 | output "PublicSubnet1ID" { 116 | description = " Public subnet 1 ID in Availability Zone 1" 117 | value = module.tfm-aws-vpc.PublicSubnet1ID 118 | } 119 | 120 | output "PublicSubnet2CIDR" { 121 | description = " Public subnet 2 CIDR in Availability Zone 2" 122 | value = module.tfm-aws-vpc.PublicSubnet2CIDR 123 | } 124 | 125 | output "PublicSubnet2ID" { 126 | description = " Public subnet 2 ID in Availability Zone 2" 127 | value = module.tfm-aws-vpc.PublicSubnet2ID 128 | } 129 | 130 | output "PublicSubnet3CIDR" { 131 | description = " Public subnet 3 CIDR in Availability Zone 3" 132 | value = module.tfm-aws-vpc.PublicSubnet3CIDR 133 | } 134 | 135 | output "PublicSubnet3ID" { 136 | description = " Public subnet 3 ID in Availability Zone 3" 137 | value = module.tfm-aws-vpc.PublicSubnet3ID 138 | } 139 | 140 | output "PublicSubnet4CIDR" { 141 | description = " Public subnet 4 CIDR in Availability Zone 4" 142 | value = module.tfm-aws-vpc.PublicSubnet4CIDR 143 | } 144 | 145 | output "PublicSubnet4ID" { 146 | description = " Public subnet 4 ID in Availability Zone 4" 147 | value = module.tfm-aws-vpc.PublicSubnet4ID 148 | } 149 | 150 | output "S3VPCEndpoint" { 151 | description = " S3 VPC Endpoint" 152 | value = module.tfm-aws-vpc.S3VPCEndpoint 153 | } 154 | 155 | output "PrivateSubnet1ARouteTable" { 156 | description = " Private subnet 1A route table" 157 | value = module.tfm-aws-vpc.PrivateSubnet1ARouteTable 158 | } 159 | 160 | output "PrivateSubnet1BRouteTable" { 161 | description = " Private subnet 1B route table" 162 | value = module.tfm-aws-vpc.PrivateSubnet1BRouteTable 163 | } 164 | 165 | output "PrivateSubnet2ARouteTable" { 166 | description = " Private subnet 2A route table" 167 | value = module.tfm-aws-vpc.PrivateSubnet2ARouteTable 168 | } 169 | 170 | output "PrivateSubnet2BRouteTable" { 171 | description = " Private subnet 2B route table" 172 | value = module.tfm-aws-vpc.PrivateSubnet2BRouteTable 173 | } 174 | 175 | output "PrivateSubnet3ARouteTable" { 176 | description = " Private subnet 3A route table" 177 | value = module.tfm-aws-vpc.PrivateSubnet3ARouteTable 178 | } 179 | 180 | output "PrivateSubnet3BRouteTable" { 181 | description = " Private subnet 3B route table" 182 | value = module.tfm-aws-vpc.PrivateSubnet3BRouteTable 183 | } 184 | 185 | output "PrivateSubnet4ARouteTable" { 186 | description = " Private subnet 4A route table" 187 | value = module.tfm-aws-vpc.PrivateSubnet4ARouteTable 188 | } 189 | 190 | output "PrivateSubnet4BRouteTable" { 191 | description = " Private subnet 4B route table" 192 | value = module.tfm-aws-vpc.PrivateSubnet4BRouteTable 193 | } 194 | 195 | output "PublicSubnetRouteTable" { 196 | description = " Public subnet route table" 197 | value = module.tfm-aws-vpc.PublicSubnetRouteTable 198 | } 199 | -------------------------------------------------------------------------------- /setup_workspace/variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_ACCESS_KEY_ID" { 2 | type = string 3 | } 4 | variable "AWS_SECRET_ACCESS_KEY" { 5 | type = string 6 | } 7 | variable "AWS_SESSION_TOKEN" { 8 | type = string 9 | default = "" 10 | } 11 | variable "tfe_organization" { 12 | type = string 13 | } 14 | variable "tfe_workspace" { 15 | type = string 16 | default = "" 17 | } 18 | variable "tfe_email" { 19 | type = string 20 | default = "someone@somewhere.resource" 21 | } 22 | variable "working_directory" { 23 | type = string 24 | default = "/deploy" 25 | } 26 | variable "region" { 27 | type = string 28 | default = "us-east-2" 29 | } 30 | 31 | -------------------------------------------------------------------------------- /setup_workspace/workspace.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.15.1" 3 | } 4 | 5 | resource "random_pet" "name" { 6 | prefix = "tfm-aws" 7 | length = 1 8 | } 9 | 10 | # Generate new terraform org and workspace 11 | 12 | module "tfc_workspace" { 13 | source = "aws-quickstart/tfc_workspace/aws" 14 | version = "0.0.12" 15 | tfe_email = var.tfe_email 16 | tfe_organization = var.tfe_organization 17 | tfe_workspace = var.tfe_workspace 18 | AWS_ACCESS_KEY_ID = var.AWS_ACCESS_KEY_ID 19 | AWS_SECRET_ACCESS_KEY = var.AWS_SECRET_ACCESS_KEY 20 | AWS_SESSION_TOKEN = var.AWS_SESSION_TOKEN 21 | working_directory = var.working_directory 22 | region = var.region 23 | } 24 | 25 | 26 | resource "null_resource" "setup_backend_file" { 27 | depends_on = [module.tfc_workspace] 28 | provisioner "local-exec" { 29 | command = "mv backend.hcl ../deploy" 30 | } 31 | } 32 | 33 | 34 | resource "null_resource" "remote_init" { 35 | depends_on = [null_resource.setup_backend_file] 36 | provisioner "local-exec" { 37 | working_dir = "../deploy" 38 | command = "terraform init -backend-config=backend.hcl" 39 | } 40 | } 41 | 42 | output "user_instructions" { 43 | value = <