├── LICENSE ├── README.md ├── clients.tf ├── experimental.tf ├── iam.tf ├── kms.tf ├── main.orig ├── main.tf ├── outputs.tf ├── server.tf ├── templates ├── OLD │ ├── server.sh.240102 │ ├── server.sh.240103 │ └── server.sh.bak ├── base.sh ├── client.sh ├── docker.sh ├── server.sh └── tfe.sh ├── tfe.tf ├── tls.tf └── variables.tf /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deploying a cluster that runs any combination of Vault, Consul, Nomad or Terraform (HashiStack) 2 | 3 | This repository is a one-size-fits-all approach to get easily started with the deployment of any of the above mentioned HashiCorp tools on AWS. The main focus is about simplicity and readability. It is purely based on Terraform IaC. The TLS certs are provided by hashicorp/terraform-provider-tls. The instances are configured via "user-data" scripts (bash). The "user-data" scripts are dynamically rendered (hashicorp/terraform-provider-template) based on the assigned values in variables.tf. 4 | 5 | --- 6 | > Its behavor can be customized by changing/overriding the defaults in variables.tf. (**take a look at the examples at the bottom**). 7 | > 8 | > Take a look at the **outputs** to find the IP addresses of the instances or the initial password to get access to the Terraform Enterpise admin page on port 8800. 9 | > 10 | > Use `apt-cache show vault-enterprise` to identiy a proper version string of a Enterprise version. 11 | --- 12 | 13 | ***Cluster Features*** 14 | - variable cluster size (typical 1,3, or 5) 15 | - auto_join of server nodes 16 | - raft-clustering 17 | - raft storage backend 18 | - configurable ingress CIDR (ACL) 19 | 20 | ***Vault Features*** 21 | - auto-unseal (AWS_KMS) 22 | - TLS encryption 23 | 24 | ***Consul Features*** 25 | - TLS encryption provided by hashicorp/terraform-provider-tls (certbot cannot be used to create necessary SANs) 26 | - Gossip encryption 27 | - ACL bootstrapping 28 | 29 | ***Nomad Features*** 30 | - configurable amount of worker nodes 31 | - auto bootstrapping 32 | 33 | ***Terraform Enterprise Features*** 34 | - letsenrypt (certbot) webbrowser friendly TLS certificates 35 | - airgapp license support 36 | - storage backend is based on "mounted disk" 37 | 38 | --- 39 | 40 | ## Content of the repository 41 | 42 | | File | Description | 43 | | - | :- | 44 | | README.md | This README | 45 | | templates/base.sh | user_data template for generic settings on the instance(s) | 46 | | templates/server.sh | user_data template to setup the service(s) on instance(s) | 47 | | templates/client.sh | user_data template to setup the Nomad worker node(s) | 48 | | templates/docker.sh | user_data template to setup Docker extension on Nomad worker node(s) | 49 | | templates/tfe.sh | user_data template to setup Terraform Enterprise on the instance | 50 | | main.tf | Provider related configurations and network configurations | 51 | | server.tf | Cluster instances include rendering of user_data templates | 52 | | clients.tf | Nomad worker instances include rendering of user_data templates | 53 | | tfe.tf | Terraform Enterprise instance include rendering of user_data templates | 54 | | iam.tf | AWS IAM roles and policy authorization for the instances (autojoin) | 55 | | kms.tf | Key Management Service that stores Vault unseal keys (auto unseal) | 56 | | tls.tf | TLS certificates for the services based on terraform_tls_provider | 57 | | variables.tf | Variables to customize the hashistack | 58 | | outputs.tf | Outputs and post build-time information | 59 | 60 | --- 61 | 62 | ## Variable Argument Reference 63 | 64 | | Key | Description | Default | 65 | | - | :- | :- | 66 | | **Global Settings** 67 | | aws_region | (required) The AWS region to be used | none | 68 | | name | (required) Environment name to be used as Tag | none | 69 | | server_count | (optional) Amount of cluster instances (odd number 1,3, max 5) | 3 | 70 | | instance_type | (optional) Type of EC2 cluster instance | t2.small | 71 | | server_name | (optional) Hostname prefix of the cluster instances | hc-stack-srv | 72 | | root_block_device_size | (optional) Size of the root filesystem | 80 | 73 | | auto_join_value | (optional) Server rejoin tag_value to identify cluster instances | joestack_hashistack_autojoin | 74 | | dns_domain | (required) The Route53 Zone to assign DNS records to | none | 75 | | key_name | (optional) SSH key_name to be used to access the instances | aws-hashistack-key | 76 | | aws_hashistack_key | (required if key_name default is not changed) The SSH public key to access any instance | NULL | 77 | | whitelist_ip | (optional) The allowed ingress IP CIDR assigned to the ASGs | 0.0.0.0/0 | 78 | | network_address_space | (optional) The CIDR to be used for the instances | 172.16.0.0/16 | 79 | | create_root_ca | (optional) Create a self-signed root ca based on hashicorp/terraform-provider-tls | true | 80 | | common_name | (optional) Common Name of the CA | hashistack | 81 | | organization | (optional) Organization of the CA | joestack | 82 | | **Vault Settings** 83 | | vault_enabled | (optional) Create a Vault cluster [true, false] | false | 84 | | vault_version | (required if vault_enabled) The Vault version to be used [1.9.3, 1.9.3+ent] | 1.9.3 | 85 | | vault_lic | (required if +ent) The Vault license in case of using Vault Enterprise | NULL | 86 | | vault_tls_enabled | (optional) Using Vault with TLS enabled [true, false] | true | 87 | | **Consul Settings** 88 | | consul_enabled | (optional) Create a Consul cluster [true, false] | false | 89 | | consul_version | (required if consul_enabled) The Consul version to be used [1.13.3, 1.13.3+ent-1] | 1.13.3 | 90 | | consul_lic | (required if +ent) The Consul license in case of using Consul Enterprise | NULL | 91 | | consul_tls_enabled | (optional) Using Consul with TLS enabled [true, false] | true | 92 | | **Nomad Settings** 93 | | nomad_enabled | (optional) Create a Nomad cluster [true, false] | false | 94 | | nomad_version | (required if nomad_enabled) The Nomad version to be used [1.2.5, 1.2.5+ent] | 1.2.5 | 95 | | nomad_lic | (required if +ent) The Nomad license in case of using Nomad Enterprise | NULL | 96 | | nomad_bootstrap | (optional) Automatically bootrstrap the Nomad cluster [true, false] | false | 97 | | datacenter | (optional) The name of the Datacenter | dc1 | 98 | | region | (optional) The name of the region | global | 99 | | client | (optional) Install Nomad clients/worker as well [true, false] | true | 100 | | client_count | (optional) The amount of Nomad clients | 3 | 101 | | client_name | (optional) Hostname prefix of Nomad clients | nmd-worker | 102 | | **Terraform Settings** 103 | | terraform_enabled | (optional) Create a Terraform Enterprise instance [true, false] | false | 104 | | tfe_lic | (required if terraform_enabled) The Terraform Enterprise license (must be base64 encoded in case of airgapped). | NULL | 105 | | tfe_airgapped | (optional) In case of using an airgap enabled license [true, false] | false | 106 | | tfe_auth_password | (optional) The initial authentication password. Will be created if NULL | NULL | 107 | | tfe_enc_password | (optional) The encryption key to be used to encrypt state and db. Will be created if NULL | NULL | 108 | | tfe_hostname | (optional) The hostname of the TFE instance | tfe-joestack | 109 | | tfe_cert_provider | (optional) TLS Certificate options [self-signed, certbot, tf-tls-provider] | certbot | 110 | | tfe_cert_email | (required if certbot) Certbot email address | none | 111 | | tfe_auto_install | (optional) Automatically install TFE on instance [true, false] | true | 112 | 113 | --- 114 | 115 | > # !!! Pro Tip !!! 116 | > A nice and secure way to inject variables into a Terraform [Cloud/Enterprise] Workspace can be found here: https://github.com/joestack/tfc-api-bootstrap-script 117 | > 118 | > * automating the setup of a Terraform Workspace 119 | > * connecting the Workspace with a VCS repository 120 | > * injecting sensitive and non-sensitive variables into the Workspace 121 | > * optional triggering a VCS driven run (plan and apply) 122 | 123 | --- 124 | 125 | ## Examples 126 | 127 | ### Terraform Enterprise only with Letsencrypt certificate 128 | 129 | aws_region = "eu-west-1" 130 | name = "my_hashistack" 131 | dns_domain = "hashidemos.io" 132 | aws_hashistack_key "ssh-rsa AA....." 133 | terraform_enabled = "true" 134 | tfe_lic = "ey......" 135 | tfe_cert_email = "foo@example.io" 136 | 137 | #### 3 Node Vault and Consul Cluster 138 | 139 | aws_region = "eu-west-1" 140 | name = "my_hashistack" 141 | dns_domain = "hashidemos.io" 142 | aws_hashistack_key "ssh-rsa AA....." 143 | vault_enabled = "true" 144 | vault_version = "1.9.3" 145 | consul_enabled = "true" 146 | consul_version = "1.13.3" 147 | 148 | #### 3 Node Vault Enterprise Cluster 149 | 150 | aws_region = "eu-west-1" 151 | name = "my_hashistack" 152 | dns_domain = "hashidemos.io" 153 | aws_hashistack_key "ssh-rsa AA....." 154 | vault_enabled = "true" 155 | vault_version = "1.9.3+ent" 156 | vault_lic = "02MV....." 157 | 158 | #### 5 Node Vault Ent, Consul Ent, Nomad Ent Cluster with 7 Worker Nodes and Terraform Enterprise 159 | 160 | aws_region = "eu-west-1" 161 | name = "my_hashistack" 162 | server_count = "5" 163 | dns_domain = "hashidemos.io" 164 | aws_hashistack_key "ssh-rsa AA....." 165 | vault_enabled = "true" 166 | vault_version = "1.12.2+ent-1" 167 | vault_lic = "02MV....." 168 | consul_enabled = "true" 169 | consul_version = "1.14.3+ent-1" 170 | consul_lic = "02MV....." 171 | nomad_enabled = "true" 172 | nomad_version = "1.2.5+ent" 173 | nomad_lic = "01MV....." 174 | client_count = "7" 175 | terraform_enabled = "true" 176 | tfe_lic = "ey......" 177 | -------------------------------------------------------------------------------- /clients.tf: -------------------------------------------------------------------------------- 1 | data "template_file" "client" { 2 | count = var.client_count 3 | template = (join("\n", tolist([ 4 | file("${path.root}/templates/base.sh"), 5 | file("${path.root}/templates/docker.sh"), 6 | file("${path.root}/templates/client.sh") 7 | ]))) 8 | vars = { 9 | client_count = var.client_count 10 | datacenter = var.datacenter 11 | region = var.region 12 | client = var.client 13 | auto_join_value = var.auto_join_value 14 | node_name = format("${var.name}-worker-%02d", count.index + 1) 15 | # node_name = format("${var.client_name}-%02d", count.index + 1) 16 | nomad_enabled = var.nomad_enabled 17 | nomad_version = var.nomad_version 18 | nomad_apt = local.nomad_apt 19 | consul_enabled = var.consul_enabled 20 | consul_version = var.consul_version 21 | consul_apt = local.consul_apt 22 | consul_lic = var.consul_lic 23 | consul_enabled = var.consul_enabled 24 | nomad_enabled = var.nomad_enabled 25 | } 26 | } 27 | 28 | data "template_cloudinit_config" "client" { 29 | count = var.client_count 30 | gzip = true 31 | base64_encode = true 32 | part { 33 | content_type = "text/x-shellscript" 34 | content = element(data.template_file.client.*.rendered, count.index) 35 | } 36 | } 37 | 38 | resource "aws_instance" "client" { 39 | count = var.nomad_enabled != "true" ? 0 : 1 * var.client_count 40 | ami = data.aws_ami.ubuntu.id 41 | instance_type = var.instance_type 42 | subnet_id = element(aws_subnet.hcstack_subnet.*.id, count.index) 43 | associate_public_ip_address = "true" 44 | vpc_security_group_ids = [aws_security_group.primary.id] 45 | key_name = var.key_name 46 | iam_instance_profile = aws_iam_instance_profile.hc-stack-client.name 47 | 48 | tags = { 49 | #Name = format("${var.client_name}-%02d", count.index + 1) 50 | Name = format("${var.name}-worker-%02d", count.index + 1) 51 | auto_join = var.auto_join_value 52 | } 53 | 54 | root_block_device { 55 | volume_type = "gp2" 56 | volume_size = var.root_block_device_size 57 | delete_on_termination = "true" 58 | } 59 | 60 | # ebs_block_device { 61 | # device_name = "/dev/xvdd" 62 | # volume_type = "gp2" 63 | # volume_size = var.ebs_block_device_size 64 | # delete_on_termination = "true" 65 | # } 66 | 67 | user_data = element(data.template_cloudinit_config.client.*.rendered, count.index) 68 | } -------------------------------------------------------------------------------- /experimental.tf: -------------------------------------------------------------------------------- 1 | ## Development section ## 2 | # injecting the initial Vault root token as secret zero into a TFC Workspace acting as secure K/V store 3 | # 4 | # Yes you are right! It is not the best methodoligy to store sensitive data into a TFC workspace. But it is the only one available without introducing further tooling. 5 | # I want to be able to deploy the entire environment in a programmatic fashion therefore I need programmatic access to Vault's ROOT_TOKEN! 6 | # The access to these sensitive data is at least encrypted and access controlled. 7 | # 8 | # vault_init= true to enable 9 | # tfc_var_set is currently unused 10 | # tfc_workspace= name of the workspace to be created if it doesn't already exist and to store the data in 11 | # tfc_token= TFC token to be used to authorize the creation of that workspace and variable injection 12 | # tfc_address= your Terraform endpoint usually app.terraform.io 13 | # tfc_org= your TF Org to be used 14 | 15 | 16 | 17 | variable "vault_init" { 18 | description = "auto unseal the cluster and store the root_token into a TF Var-Set" 19 | default = "false" 20 | } 21 | 22 | variable "tfc_var_set" { 23 | description = "The name of the Var-Set to be used to store the initial secret zero" 24 | default = "NULL" 25 | } 26 | 27 | variable "tfc_workspace" { 28 | default = "NULL" 29 | } 30 | 31 | variable "tfc_token" { 32 | default = "NULL" 33 | } 34 | 35 | variable "tfc_address" { 36 | default = "app.terraform.io" 37 | } 38 | 39 | variable "tfc_org" { 40 | default = "NULL" 41 | } -------------------------------------------------------------------------------- /iam.tf: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------- 2 | // IAM and Policy Resources 3 | 4 | ## Vault Server IAM Config 5 | resource "aws_iam_instance_profile" "hc-stack-server" { 6 | name = "${var.name}-hc-stack-server-instance-profile" 7 | role = aws_iam_role.hc-stack-server.name 8 | } 9 | 10 | resource "aws_iam_role" "hc-stack-server" { 11 | name = "${var.name}-hc-stack-server-role" 12 | assume_role_policy = data.aws_iam_policy_document.assume_role.json 13 | } 14 | 15 | resource "aws_iam_role_policy" "hc-stack-server" { 16 | name = "${var.name}-hc-stack-server-role-policy" 17 | role = aws_iam_role.hc-stack-server.id 18 | policy = data.aws_iam_policy_document.hc-stack-server.json 19 | } 20 | 21 | # Vault Client IAM Config 22 | resource "aws_iam_instance_profile" "hc-stack-client" { 23 | name = "${var.name}-hc-stack-client-instance-profile" 24 | role = aws_iam_role.hc-stack-client.name 25 | } 26 | 27 | resource "aws_iam_role" "hc-stack-client" { 28 | name = "${var.name}-hc-stack-client-role" 29 | assume_role_policy = data.aws_iam_policy_document.assume_role.json 30 | } 31 | 32 | resource "aws_iam_role_policy" "hc-stack-client" { 33 | name = "${var.name}-hc-stack-client-role-policy" 34 | role = aws_iam_role.hc-stack-client.id 35 | policy = data.aws_iam_policy_document.hc-stack-client.json 36 | } 37 | 38 | //-------------------------------------------------------------------- 39 | // Data Sources 40 | 41 | data "aws_iam_policy_document" "assume_role" { 42 | statement { 43 | effect = "Allow" 44 | actions = ["sts:AssumeRole"] 45 | 46 | principals { 47 | type = "Service" 48 | identifiers = ["ec2.amazonaws.com"] 49 | } 50 | } 51 | } 52 | 53 | data "aws_iam_policy_document" "hc-stack-server" { 54 | statement { 55 | sid = "RaftSingle" 56 | effect = "Allow" 57 | 58 | actions = ["ec2:DescribeInstances"] 59 | 60 | resources = ["*"] 61 | } 62 | 63 | statement { 64 | sid = "VaultAWSAuthMethod" 65 | effect = "Allow" 66 | actions = [ 67 | "ec2:DescribeInstances", 68 | "iam:GetInstanceProfile", 69 | "iam:GetUser", 70 | "iam:GetRole", 71 | ] 72 | resources = ["*"] 73 | } 74 | 75 | statement { 76 | sid = "VaultKMSUnseal" 77 | effect = "Allow" 78 | 79 | actions = [ 80 | "kms:Encrypt", 81 | "kms:Decrypt", 82 | "kms:DescribeKey", 83 | ] 84 | 85 | resources = ["*"] 86 | } 87 | } 88 | 89 | data "aws_iam_policy_document" "hc-stack-client" { 90 | statement { 91 | sid = "RaftSingle" 92 | effect = "Allow" 93 | 94 | actions = ["ec2:DescribeInstances"] 95 | 96 | resources = ["*"] 97 | } 98 | } -------------------------------------------------------------------------------- /kms.tf: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------- 2 | // KMS Resources for Vault auto-unsealing 3 | 4 | resource "aws_kms_key" "vault" { 5 | count = var.vault_enabled ? 1 : 0 6 | description = "Vault unseal key" 7 | deletion_window_in_days = 7 8 | 9 | tags = { 10 | Name = "${var.name}-vault-kms-unseal-key" 11 | } 12 | } 13 | 14 | resource "aws_kms_alias" "vault" { 15 | count = var.vault_enabled ? 1 : 0 16 | name = "alias/${var.name}-vault-kms-unseal-key" 17 | target_key_id = aws_kms_key.vault.*.key_id[count.index] 18 | } -------------------------------------------------------------------------------- /main.orig: -------------------------------------------------------------------------------- 1 | // provider and provider related or globally used data sources 2 | provider "aws" { 3 | region = var.aws_region 4 | } 5 | resource "aws_key_pair" "aws-hashistack-key" { 6 | count = var.key_name != "aws-hashistack-key" ? 0 : 1 7 | key_name = "aws-hashistack-key" 8 | public_key = var.aws_hashistack_key 9 | } 10 | 11 | data "aws_availability_zones" "available" {} 12 | 13 | data "aws_route53_zone" "selected" { 14 | name = "${var.dns_domain}." 15 | private_zone = false 16 | } 17 | 18 | data "aws_ami" "ubuntu" { 19 | most_recent = true 20 | filter { 21 | name = "name" 22 | #values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"] 23 | #values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] 24 | values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] 25 | } 26 | filter { 27 | name = "virtualization-type" 28 | values = ["hvm"] 29 | } 30 | owners = ["099720109477"] # Canonical 31 | } 32 | 33 | # data "aws_ami" "rhel" { 34 | # most_recent = true 35 | # owners = ["309956199498"] // Red Hat's Account ID 36 | # filter { 37 | # name = "name" 38 | # values = ["RHEL-8.5*"] 39 | # } 40 | # filter { 41 | # name = "architecture" 42 | # values = ["x86_64"] 43 | # } 44 | # filter { 45 | # name = "root-device-type" 46 | # values = ["ebs"] 47 | # } 48 | # filter { 49 | # name = "virtualization-type" 50 | # values = ["hvm"] 51 | # } 52 | # } 53 | 54 | # data "aws_ami" "fedora" { 55 | # most_recent = true 56 | 57 | # filter { 58 | # name = "name" 59 | # values = ["Fedora*"] 60 | # } 61 | 62 | # filter { 63 | # name = "virtualization-type" 64 | # values = [ "hvm"] 65 | # } 66 | 67 | # owners = ["125523088429"] 68 | # } 69 | 70 | // network and security 71 | 72 | resource "aws_vpc" "hashicorp_vpc" { 73 | cidr_block = var.network_address_space 74 | enable_dns_hostnames = "true" 75 | tags = { 76 | Name = "${var.name}-vpc" 77 | } 78 | } 79 | 80 | resource "aws_internet_gateway" "igw" { 81 | vpc_id = aws_vpc.hashicorp_vpc.id 82 | 83 | } 84 | 85 | resource "aws_route_table" "rtb" { 86 | vpc_id = aws_vpc.hashicorp_vpc.id 87 | route { 88 | cidr_block = "0.0.0.0/0" 89 | gateway_id = aws_internet_gateway.igw.id 90 | } 91 | tags = { 92 | Name = "${var.name}-IGW" 93 | } 94 | } 95 | 96 | resource "aws_route_table_association" "hcstack-rtb" { 97 | count = var.server_count 98 | subnet_id = element(aws_subnet.hcstack_subnet.*.id, count.index) 99 | route_table_id = aws_route_table.rtb.id 100 | } 101 | 102 | 103 | resource "aws_subnet" "hcstack_subnet" { 104 | count = var.server_count 105 | vpc_id = aws_vpc.hashicorp_vpc.id 106 | cidr_block = cidrsubnet(var.network_address_space, 8, count.index + 1) 107 | map_public_ip_on_launch = "true" 108 | availability_zone = element(data.aws_availability_zones.available.names, count.index) 109 | tags = { 110 | Name = "${var.name}-subnet" 111 | } 112 | } 113 | 114 | ############################### 115 | ####### ASG ######### 116 | 117 | resource "aws_security_group" "primary" { 118 | name = "${var.name}-primary-sg" 119 | description = "Primary ASG" 120 | vpc_id = aws_vpc.hashicorp_vpc.id 121 | } 122 | 123 | resource "aws_security_group_rule" "ssh" { 124 | security_group_id = aws_security_group.primary.id 125 | type = "ingress" 126 | from_port = 22 127 | to_port = 22 128 | protocol = "tcp" 129 | cidr_blocks = [var.whitelist_ip] 130 | } 131 | 132 | resource "aws_security_group_rule" "http" { 133 | security_group_id = aws_security_group.primary.id 134 | type = "ingress" 135 | from_port = 80 136 | to_port = 80 137 | protocol = "tcp" 138 | cidr_blocks = [var.whitelist_ip] 139 | } 140 | 141 | resource "aws_security_group_rule" "https" { 142 | security_group_id = aws_security_group.primary.id 143 | type = "ingress" 144 | from_port = 443 145 | to_port = 443 146 | protocol = "tcp" 147 | cidr_blocks = [var.whitelist_ip] 148 | } 149 | 150 | resource "aws_security_group_rule" "egress" { 151 | security_group_id = aws_security_group.primary.id 152 | type = "egress" 153 | from_port = 0 154 | to_port = 0 155 | protocol = "-1" 156 | cidr_blocks = ["0.0.0.0/0"] 157 | } 158 | 159 | resource "aws_security_group_rule" "nomad-1" { 160 | count = var.nomad_enabled ? 1 : 0 161 | security_group_id = aws_security_group.primary.id 162 | type = "ingress" 163 | from_port = 4646 164 | to_port = 4648 165 | protocol = "tcp" 166 | cidr_blocks = [var.whitelist_ip] 167 | } 168 | 169 | resource "aws_security_group_rule" "nomad-2" { 170 | count = var.nomad_enabled ? 1 : 0 171 | security_group_id = aws_security_group.primary.id 172 | type = "ingress" 173 | from_port = 4646 174 | to_port = 4648 175 | protocol = "udp" 176 | cidr_blocks = [var.whitelist_ip] 177 | } 178 | 179 | resource "aws_security_group_rule" "vault-1" { 180 | count = var.vault_enabled ? 1 : 0 181 | security_group_id = aws_security_group.primary.id 182 | type = "ingress" 183 | from_port = 8200 184 | to_port = 8202 185 | protocol = "tcp" 186 | cidr_blocks = [var.whitelist_ip] 187 | } 188 | 189 | resource "aws_security_group_rule" "server-rpc" { 190 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 191 | security_group_id = aws_security_group.primary.id 192 | type = "ingress" 193 | from_port = 8300 194 | to_port = 8300 195 | protocol = "tcp" 196 | cidr_blocks = [var.whitelist_ip] 197 | } 198 | 199 | resource "aws_security_group_rule" "serf-tcp" { 200 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 201 | security_group_id = aws_security_group.primary.id 202 | type = "ingress" 203 | from_port = 8301 204 | to_port = 8302 205 | protocol = "tcp" 206 | cidr_blocks = [var.whitelist_ip] 207 | } 208 | 209 | resource "aws_security_group_rule" "serf-udp" { 210 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 211 | security_group_id = aws_security_group.primary.id 212 | type = "ingress" 213 | from_port = 8301 214 | to_port = 8302 215 | protocol = "udp" 216 | cidr_blocks = [var.whitelist_ip] 217 | } 218 | 219 | resource "aws_security_group_rule" "vault-4" { 220 | count = var.vault_enabled ? 1 : 0 221 | security_group_id = aws_security_group.primary.id 222 | type = "ingress" 223 | from_port = 8400 224 | to_port = 8400 225 | protocol = "tcp" 226 | cidr_blocks = [var.whitelist_ip] 227 | } 228 | 229 | resource "aws_security_group_rule" "consul-api" { 230 | count = var.consul_enabled ? 1 : 0 231 | security_group_id = aws_security_group.primary.id 232 | type = "ingress" 233 | from_port = 8500 234 | to_port = 8503 235 | protocol = "tcp" 236 | cidr_blocks = [var.whitelist_ip] 237 | } 238 | 239 | resource "aws_security_group_rule" "consul-dns-tcp" { 240 | count = var.consul_enabled ? 1 : 0 241 | security_group_id = aws_security_group.primary.id 242 | type = "ingress" 243 | from_port = 8600 244 | to_port = 8600 245 | protocol = "tcp" 246 | cidr_blocks = [var.whitelist_ip] 247 | } 248 | 249 | resource "aws_security_group_rule" "consul-dns-udp" { 250 | count = var.consul_enabled ? 1 : 0 251 | security_group_id = aws_security_group.primary.id 252 | type = "ingress" 253 | from_port = 8600 254 | to_port = 8600 255 | protocol = "udp" 256 | cidr_blocks = [var.whitelist_ip] 257 | } 258 | 259 | resource "aws_security_group_rule" "consul-sidecar" { 260 | count = var.consul_enabled ? 1 : 0 261 | security_group_id = aws_security_group.primary.id 262 | type = "ingress" 263 | from_port = 21000 264 | to_port = 21255 265 | protocol = "tcp" 266 | cidr_blocks = [var.whitelist_ip] 267 | } 268 | 269 | # resource "aws_security_group_rule" "consul-5" { 270 | # count = var.consul_enabled ? 1 : 0 271 | # security_group_id = aws_security_group.primary.id 272 | # type = "ingress" 273 | # from_port = 30000 274 | # to_port = 39999 275 | # protocol = "tcp" 276 | # cidr_blocks = [var.whitelist_ip] 277 | # } 278 | 279 | // TFE 280 | 281 | resource "aws_security_group" "tfe" { 282 | count = var.terraform_enabled ? 1 : 0 283 | name = "${var.name}-tfe-sg" 284 | description = "TFE ASG" 285 | vpc_id = aws_vpc.hashicorp_vpc.id 286 | } 287 | 288 | resource "aws_security_group_rule" "tfe-ssh" { 289 | count = var.terraform_enabled ? 1 : 0 290 | security_group_id = aws_security_group.tfe[count.index].id 291 | type = "ingress" 292 | from_port = 22 293 | to_port = 22 294 | protocol = "tcp" 295 | cidr_blocks = [var.whitelist_ip] 296 | } 297 | 298 | resource "aws_security_group_rule" "tfe-http" { 299 | count = var.terraform_enabled ? 1 : 0 300 | security_group_id = aws_security_group.tfe[count.index].id 301 | type = "ingress" 302 | from_port = 80 303 | to_port = 80 304 | protocol = "tcp" 305 | cidr_blocks = [var.whitelist_ip] 306 | } 307 | 308 | resource "aws_security_group_rule" "tfe-https" { 309 | count = var.terraform_enabled ? 1 : 0 310 | security_group_id = aws_security_group.tfe[count.index].id 311 | type = "ingress" 312 | from_port = 443 313 | to_port = 443 314 | protocol = "tcp" 315 | cidr_blocks = [var.whitelist_ip] 316 | } 317 | 318 | resource "aws_security_group_rule" "tfe-admin" { 319 | count = var.terraform_enabled ? 1 : 0 320 | security_group_id = aws_security_group.tfe[count.index].id 321 | type = "ingress" 322 | from_port = 8800 323 | to_port = 8800 324 | protocol = "tcp" 325 | cidr_blocks = [var.whitelist_ip] 326 | } 327 | 328 | resource "aws_security_group_rule" "tfe-egress" { 329 | count = var.terraform_enabled ? 1 : 0 330 | security_group_id = aws_security_group.tfe[count.index].id 331 | type = "egress" 332 | from_port = 0 333 | to_port = 0 334 | protocol = "-1" 335 | cidr_blocks = ["0.0.0.0/0"] 336 | } 337 | 338 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | // provider and provider related or globally used data sources 2 | provider "aws" { 3 | region = var.aws_region 4 | } 5 | resource "aws_key_pair" "aws-hashistack-key" { 6 | count = var.key_name != "aws-hashistack-key" ? 0 : 1 7 | key_name = "aws-hashistack-key" 8 | public_key = var.aws_hashistack_key 9 | } 10 | 11 | data "aws_availability_zones" "available" {} 12 | 13 | data "aws_route53_zone" "selected" { 14 | name = "${var.dns_domain}." 15 | private_zone = false 16 | } 17 | 18 | data "aws_ami" "ubuntu" { 19 | most_recent = true 20 | filter { 21 | name = "name" 22 | #values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"] 23 | #values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] 24 | values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] 25 | } 26 | filter { 27 | name = "virtualization-type" 28 | values = ["hvm"] 29 | } 30 | owners = ["099720109477"] # Canonical 31 | } 32 | 33 | # data "aws_ami" "rhel" { 34 | # most_recent = true 35 | # owners = ["309956199498"] // Red Hat's Account ID 36 | # filter { 37 | # name = "name" 38 | # values = ["RHEL-8.5*"] 39 | # } 40 | # filter { 41 | # name = "architecture" 42 | # values = ["x86_64"] 43 | # } 44 | # filter { 45 | # name = "root-device-type" 46 | # values = ["ebs"] 47 | # } 48 | # filter { 49 | # name = "virtualization-type" 50 | # values = ["hvm"] 51 | # } 52 | # } 53 | 54 | # data "aws_ami" "fedora" { 55 | # most_recent = true 56 | 57 | # filter { 58 | # name = "name" 59 | # values = ["Fedora*"] 60 | # } 61 | 62 | # filter { 63 | # name = "virtualization-type" 64 | # values = [ "hvm"] 65 | # } 66 | 67 | # owners = ["125523088429"] 68 | # } 69 | 70 | // network and security 71 | 72 | resource "aws_vpc" "hashicorp_vpc" { 73 | cidr_block = var.network_address_space 74 | enable_dns_hostnames = "true" 75 | tags = { 76 | Name = "${var.name}-vpc" 77 | } 78 | } 79 | 80 | resource "aws_internet_gateway" "igw" { 81 | vpc_id = aws_vpc.hashicorp_vpc.id 82 | 83 | } 84 | 85 | resource "aws_route_table" "rtb" { 86 | vpc_id = aws_vpc.hashicorp_vpc.id 87 | route { 88 | cidr_block = "0.0.0.0/0" 89 | gateway_id = aws_internet_gateway.igw.id 90 | } 91 | tags = { 92 | Name = "${var.name}-IGW" 93 | } 94 | } 95 | 96 | resource "aws_route_table_association" "hcstack-rtb" { 97 | count = var.server_count 98 | subnet_id = element(aws_subnet.hcstack_subnet.*.id, count.index) 99 | route_table_id = aws_route_table.rtb.id 100 | } 101 | 102 | 103 | resource "aws_subnet" "hcstack_subnet" { 104 | count = var.server_count 105 | vpc_id = aws_vpc.hashicorp_vpc.id 106 | cidr_block = cidrsubnet(var.network_address_space, 8, count.index + 1) 107 | map_public_ip_on_launch = "true" 108 | availability_zone = element(data.aws_availability_zones.available.names, count.index) 109 | tags = { 110 | Name = "${var.name}-subnet" 111 | } 112 | } 113 | 114 | resource "aws_subnet" "web_subnet" { 115 | #count = var.server_count 116 | vpc_id = aws_vpc.hashicorp_vpc.id 117 | cidr_block = cidrsubnet(var.network_address_space, 8, 10) 118 | #map_public_ip_on_launch = "true" 119 | #availability_zone = element(data.aws_availability_zones.available.names, count.index) 120 | tags = { 121 | Name = "web-subnet" 122 | } 123 | } 124 | 125 | 126 | resource "aws_subnet" "db_subnet" { 127 | #count = var.server_count 128 | vpc_id = aws_vpc.hashicorp_vpc.id 129 | cidr_block = cidrsubnet(var.network_address_space, 8, 20) 130 | #map_public_ip_on_launch = "true" 131 | #availability_zone = element(data.aws_availability_zones.available.names, count.index) 132 | tags = { 133 | Name = "db-subnet" 134 | } 135 | } 136 | 137 | resource "aws_route_table_association" "web-rtb" { 138 | #count = var.server_count 139 | subnet_id = aws_subnet.web_subnet.id 140 | route_table_id = aws_route_table.rtb.id 141 | } 142 | 143 | resource "aws_route_table_association" "db-rtb" { 144 | #count = var.server_count 145 | subnet_id = aws_subnet.db_subnet.id 146 | route_table_id = aws_route_table.rtb.id 147 | } 148 | 149 | ############################### 150 | ####### ASG ######### 151 | 152 | resource "aws_security_group" "primary" { 153 | name = "${var.name}-primary-sg" 154 | description = "Primary ASG" 155 | vpc_id = aws_vpc.hashicorp_vpc.id 156 | } 157 | 158 | resource "aws_security_group_rule" "ssh" { 159 | security_group_id = aws_security_group.primary.id 160 | type = "ingress" 161 | from_port = 22 162 | to_port = 22 163 | protocol = "tcp" 164 | cidr_blocks = [var.whitelist_ip] 165 | } 166 | 167 | resource "aws_security_group_rule" "http" { 168 | security_group_id = aws_security_group.primary.id 169 | type = "ingress" 170 | from_port = 80 171 | to_port = 80 172 | protocol = "tcp" 173 | cidr_blocks = [var.whitelist_ip] 174 | } 175 | 176 | resource "aws_security_group_rule" "https" { 177 | security_group_id = aws_security_group.primary.id 178 | type = "ingress" 179 | from_port = 443 180 | to_port = 443 181 | protocol = "tcp" 182 | cidr_blocks = [var.whitelist_ip] 183 | } 184 | 185 | resource "aws_security_group_rule" "egress" { 186 | security_group_id = aws_security_group.primary.id 187 | type = "egress" 188 | from_port = 0 189 | to_port = 0 190 | protocol = "-1" 191 | cidr_blocks = ["0.0.0.0/0"] 192 | } 193 | 194 | resource "aws_security_group_rule" "nomad-1" { 195 | count = var.nomad_enabled ? 1 : 0 196 | security_group_id = aws_security_group.primary.id 197 | type = "ingress" 198 | from_port = 4646 199 | to_port = 4648 200 | protocol = "tcp" 201 | cidr_blocks = [var.whitelist_ip] 202 | } 203 | 204 | resource "aws_security_group_rule" "nomad-2" { 205 | count = var.nomad_enabled ? 1 : 0 206 | security_group_id = aws_security_group.primary.id 207 | type = "ingress" 208 | from_port = 4646 209 | to_port = 4648 210 | protocol = "udp" 211 | cidr_blocks = [var.whitelist_ip] 212 | } 213 | 214 | resource "aws_security_group_rule" "vault-1" { 215 | count = var.vault_enabled ? 1 : 0 216 | security_group_id = aws_security_group.primary.id 217 | type = "ingress" 218 | from_port = 8200 219 | to_port = 8202 220 | protocol = "tcp" 221 | cidr_blocks = [var.whitelist_ip] 222 | } 223 | 224 | resource "aws_security_group_rule" "server-rpc" { 225 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 226 | security_group_id = aws_security_group.primary.id 227 | type = "ingress" 228 | from_port = 8300 229 | to_port = 8300 230 | protocol = "tcp" 231 | cidr_blocks = [var.whitelist_ip] 232 | } 233 | 234 | resource "aws_security_group_rule" "serf-tcp" { 235 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 236 | security_group_id = aws_security_group.primary.id 237 | type = "ingress" 238 | from_port = 8301 239 | to_port = 8302 240 | protocol = "tcp" 241 | cidr_blocks = [var.whitelist_ip] 242 | } 243 | 244 | resource "aws_security_group_rule" "serf-udp" { 245 | count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? 1 : 0 246 | security_group_id = aws_security_group.primary.id 247 | type = "ingress" 248 | from_port = 8301 249 | to_port = 8302 250 | protocol = "udp" 251 | cidr_blocks = [var.whitelist_ip] 252 | } 253 | 254 | resource "aws_security_group_rule" "vault-4" { 255 | count = var.vault_enabled ? 1 : 0 256 | security_group_id = aws_security_group.primary.id 257 | type = "ingress" 258 | from_port = 8400 259 | to_port = 8400 260 | protocol = "tcp" 261 | cidr_blocks = [var.whitelist_ip] 262 | } 263 | 264 | resource "aws_security_group_rule" "consul-api" { 265 | count = var.consul_enabled ? 1 : 0 266 | security_group_id = aws_security_group.primary.id 267 | type = "ingress" 268 | from_port = 8500 269 | to_port = 8503 270 | protocol = "tcp" 271 | cidr_blocks = [var.whitelist_ip] 272 | } 273 | 274 | resource "aws_security_group_rule" "consul-dns-tcp" { 275 | count = var.consul_enabled ? 1 : 0 276 | security_group_id = aws_security_group.primary.id 277 | type = "ingress" 278 | from_port = 8600 279 | to_port = 8600 280 | protocol = "tcp" 281 | cidr_blocks = [var.whitelist_ip] 282 | } 283 | 284 | resource "aws_security_group_rule" "consul-dns-udp" { 285 | count = var.consul_enabled ? 1 : 0 286 | security_group_id = aws_security_group.primary.id 287 | type = "ingress" 288 | from_port = 8600 289 | to_port = 8600 290 | protocol = "udp" 291 | cidr_blocks = [var.whitelist_ip] 292 | } 293 | 294 | resource "aws_security_group_rule" "consul-sidecar" { 295 | count = var.consul_enabled ? 1 : 0 296 | security_group_id = aws_security_group.primary.id 297 | type = "ingress" 298 | from_port = 21000 299 | to_port = 21255 300 | protocol = "tcp" 301 | cidr_blocks = [var.whitelist_ip] 302 | } 303 | 304 | # resource "aws_security_group_rule" "consul-5" { 305 | # count = var.consul_enabled ? 1 : 0 306 | # security_group_id = aws_security_group.primary.id 307 | # type = "ingress" 308 | # from_port = 30000 309 | # to_port = 39999 310 | # protocol = "tcp" 311 | # cidr_blocks = [var.whitelist_ip] 312 | # } 313 | 314 | // TFE 315 | 316 | resource "aws_security_group" "tfe" { 317 | count = var.terraform_enabled ? 1 : 0 318 | name = "${var.name}-tfe-sg" 319 | description = "TFE ASG" 320 | vpc_id = aws_vpc.hashicorp_vpc.id 321 | } 322 | 323 | resource "aws_security_group_rule" "tfe-ssh" { 324 | count = var.terraform_enabled ? 1 : 0 325 | security_group_id = aws_security_group.tfe[count.index].id 326 | type = "ingress" 327 | from_port = 22 328 | to_port = 22 329 | protocol = "tcp" 330 | cidr_blocks = [var.whitelist_ip] 331 | } 332 | 333 | resource "aws_security_group_rule" "tfe-http" { 334 | count = var.terraform_enabled ? 1 : 0 335 | security_group_id = aws_security_group.tfe[count.index].id 336 | type = "ingress" 337 | from_port = 80 338 | to_port = 80 339 | protocol = "tcp" 340 | cidr_blocks = [var.whitelist_ip] 341 | } 342 | 343 | resource "aws_security_group_rule" "tfe-https" { 344 | count = var.terraform_enabled ? 1 : 0 345 | security_group_id = aws_security_group.tfe[count.index].id 346 | type = "ingress" 347 | from_port = 443 348 | to_port = 443 349 | protocol = "tcp" 350 | cidr_blocks = [var.whitelist_ip] 351 | } 352 | 353 | resource "aws_security_group_rule" "tfe-admin" { 354 | count = var.terraform_enabled ? 1 : 0 355 | security_group_id = aws_security_group.tfe[count.index].id 356 | type = "ingress" 357 | from_port = 8800 358 | to_port = 8800 359 | protocol = "tcp" 360 | cidr_blocks = [var.whitelist_ip] 361 | } 362 | 363 | resource "aws_security_group_rule" "tfe-egress" { 364 | count = var.terraform_enabled ? 1 : 0 365 | security_group_id = aws_security_group.tfe[count.index].id 366 | type = "egress" 367 | from_port = 0 368 | to_port = 0 369 | protocol = "-1" 370 | cidr_blocks = ["0.0.0.0/0"] 371 | } 372 | 373 | // Consul Workload 374 | 375 | resource "aws_security_group" "consul-workload" { 376 | count = var.consul_enabled? 1 : 0 377 | name = "${var.name}-consul-workload" 378 | description = "Consul Workload ASG" 379 | vpc_id = aws_vpc.hashicorp_vpc.id 380 | } 381 | 382 | resource "aws_security_group_rule" "consul-1" { 383 | count = var.consul_enabled ? 1 : 0 384 | security_group_id = aws_security_group.consul-workload[count.index].id 385 | type = "ingress" 386 | from_port = 21000 387 | to_port = 21255 388 | protocol = "tcp" 389 | cidr_blocks = [var.whitelist_ip] 390 | } 391 | 392 | resource "aws_security_group_rule" "consul-ssh" { 393 | count = var.consul_enabled ? 1 : 0 394 | security_group_id = aws_security_group.consul-workload[count.index].id 395 | type = "ingress" 396 | from_port = 22 397 | to_port = 22 398 | protocol = "tcp" 399 | cidr_blocks = [var.whitelist_ip] 400 | } 401 | 402 | 403 | resource "aws_security_group_rule" "egress-workload" { 404 | count = var.consul_enabled ? 1 : 0 405 | security_group_id = aws_security_group.consul-workload[count.index].id 406 | type = "egress" 407 | from_port = 0 408 | to_port = 5 409 | protocol = "-1" 410 | cidr_blocks = ["0.0.0.0/0"] 411 | } -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "cluster_private_ips" { 2 | value = aws_instance.server.*.private_ip 3 | } 4 | 5 | output "cluster_public_ips" { 6 | value = aws_instance.server[*].public_ip 7 | } 8 | 9 | output "nomad_client_private_ips" { 10 | value = aws_instance.client.*.private_ip 11 | } 12 | 13 | output "nomad_client_public_ips" { 14 | value = aws_instance.client[*].public_ip 15 | } 16 | 17 | output "tfe_pub_ip" { 18 | value = aws_instance.tfe[*].public_ip 19 | } 20 | 21 | output "tfe_prv_ip" { 22 | value = aws_instance.tfe[*].private_ip 23 | } 24 | 25 | output "cluster_server_count" { 26 | value = local.server_count 27 | } 28 | 29 | output "tfe_enc_password" { 30 | value = var.terraform_enabled ? local.tfe_enc_password : "" 31 | } 32 | 33 | output "tfe_auth_passord" { 34 | value = var.terraform_enabled ? local.tfe_auth_password : "" 35 | } 36 | 37 | output "consul_init_token" { 38 | value = var.consul_enabled ? local.consul_init_token : "" 39 | } 40 | 41 | output "cluster_fqdn" { 42 | value = aws_route53_record.server.*.fqdn 43 | } 44 | 45 | output "consul_datacenter" { 46 | value = var.datacenter 47 | } 48 | 49 | output "cluster_url" { 50 | value = aws_route53_record.server.*.fqdn[0] 51 | } 52 | 53 | output "vpc_cidr" { 54 | value = aws_vpc.hashicorp_vpc.cidr_block 55 | } 56 | 57 | output "vpc_id" { 58 | value = aws_vpc.hashicorp_vpc.id 59 | } 60 | 61 | output "hashistack_subnet" { 62 | value = aws_subnet.hcstack_subnet.*.id 63 | } 64 | 65 | output "consul_vpc_security_id" { 66 | value = aws_security_group.primary.id 67 | } 68 | 69 | output "consul_ca_file" { 70 | value = base64encode(tls_self_signed_cert.ca.*.cert_pem[0]) 71 | } 72 | 73 | output "consul_gossip_key" { 74 | value = local.consul_gossip_key 75 | } 76 | 77 | output "consul_version" { 78 | value = var.consul_version 79 | } 80 | 81 | output "aws_region" { 82 | value = var.aws_region 83 | } 84 | 85 | # //To be deleted - redundant 86 | # output "tls_self_signed_cert" { 87 | # value = tls_self_signed_cert.ca.*.cert_pem[0] 88 | # } 89 | 90 | output "web_subnet_cidr" { 91 | value = aws_subnet.web_subnet.cidr_block 92 | } 93 | 94 | 95 | output "db_subnet_cidr" { 96 | value = aws_subnet.db_subnet.cidr_block 97 | } 98 | 99 | output "web_subnet" { 100 | value = aws_subnet.web_subnet.id 101 | } 102 | 103 | output "db_subnet" { 104 | value = aws_subnet.db_subnet.id 105 | } 106 | 107 | output "workload_vpc_security_id" { 108 | value = var.consul_enabled ? aws_security_group.consul-workload[0].id : "NULL" 109 | 110 | } 111 | -------------------------------------------------------------------------------- /server.tf: -------------------------------------------------------------------------------- 1 | # locals are used to add a little more magic, dynamic, or circumstances to the vars 2 | # used by the template data source to render the user_data scripts 3 | locals { 4 | vault_apt = length(split("+", var.vault_version)) == 2 ? "vault-enterprise" : "vault" 5 | consul_apt = length(split("+", var.consul_version)) == 2 ? "consul-enterprise" : "consul" 6 | nomad_apt = length(split("+", var.nomad_version)) == 2 ? "nomad-enterprise" : "nomad" 7 | kms_key_id = var.vault_enabled ? aws_kms_key.vault.0.key_id : "NULL" 8 | ca_cert = var.create_root_ca ? tls_private_key.ca.0.public_key_pem : "NULL" 9 | fqdn_tls = [for i in range(local.server_count) : format("%v-srv-%02d.%v", var.name, i + 1, var.dns_domain)] 10 | #fqdn_tls = [for i in range(local.server_count) : format("%v-%02d.%v", var.server_name, i + 1, var.dns_domain)] 11 | vault_protocol = var.vault_tls_enabled ? "https" : "http" 12 | vault_tls_disable = var.vault_tls_enabled ? "false" : "true" 13 | consul_fqdn_tls = formatlist("server.%s.consul", [var.datacenter]) 14 | server_ca = var.consul_tls_enabled ? tls_self_signed_cert.ca.0.cert_pem : "NULL" 15 | consul_gossip_key = random_id.gossip.b64_std 16 | consul_protocol = var.consul_tls_enabled ? "https" : "http" 17 | consul_init_token = random_uuid.consul_init_token.id 18 | server_count = anytrue([var.vault_enabled, var.consul_enabled, var.nomad_enabled]) ? var.server_count : 0 19 | } 20 | 21 | data "template_file" "server" { 22 | count = local.server_count 23 | template = (join("\n", tolist([ 24 | file("${path.root}/templates/base.sh"), 25 | file("${path.root}/templates/server.sh") 26 | ]))) 27 | vars = { 28 | server_count = local.server_count 29 | aws_region = var.aws_region 30 | datacenter = var.datacenter 31 | region = var.region 32 | auto_join_value = var.auto_join_value 33 | node_name = format("${var.name}-srv-%02d", count.index + 1) 34 | # node_name = format("${var.server_name}-%02d", count.index + 1) 35 | ca_cert = local.ca_cert 36 | server_cert = tls_locally_signed_cert.server-node[count.index].cert_pem 37 | server_key = tls_private_key.server-node[count.index].private_key_pem 38 | server_ca = local.server_ca 39 | dns_domain = var.dns_domain 40 | vault_enabled = var.vault_enabled 41 | vault_version = var.vault_version 42 | vault_apt = local.vault_apt 43 | vault_lic = var.vault_lic 44 | kms_key_id = local.kms_key_id 45 | vault_protocol = local.vault_protocol 46 | vault_tls_disable = local.vault_tls_disable 47 | consul_enabled = var.consul_enabled 48 | consul_version = var.consul_version 49 | consul_apt = local.consul_apt 50 | consul_lic = var.consul_lic 51 | consul_gossip_key = local.consul_gossip_key 52 | consul_protocol = local.consul_protocol 53 | consul_env_addr = upper(local.consul_protocol) 54 | consul_init_token = local.consul_init_token 55 | nomad_enabled = var.nomad_enabled 56 | nomad_version = var.nomad_version 57 | nomad_apt = local.nomad_apt 58 | nomad_lic = var.nomad_lic 59 | nomad_bootstrap = var.nomad_bootstrap 60 | ## dev area 61 | vault_init = var.vault_init 62 | bastion_host = format("%v-srv-01", var.name) 63 | tfc_token = var.tfc_token 64 | tfc_addr = var.tfc_address 65 | tfc_org = var.tfc_org 66 | tfc_var_set = var.tfc_var_set 67 | tfc_workspace = var.tfc_workspace 68 | } 69 | } 70 | 71 | data "template_cloudinit_config" "server" { 72 | count = local.server_count 73 | gzip = true 74 | base64_encode = true 75 | part { 76 | content_type = "text/x-shellscript" 77 | content = element(data.template_file.server.*.rendered, count.index) 78 | } 79 | } 80 | 81 | resource "aws_instance" "server" { 82 | count = local.server_count 83 | ami = data.aws_ami.ubuntu.id 84 | instance_type = var.instance_type 85 | subnet_id = element(aws_subnet.hcstack_subnet.*.id, count.index) 86 | associate_public_ip_address = "true" 87 | vpc_security_group_ids = [aws_security_group.primary.id] 88 | key_name = var.key_name 89 | iam_instance_profile = aws_iam_instance_profile.hc-stack-server.name 90 | 91 | tags = { 92 | #Name = format("${var.server_name}-%02d", count.index + 1) 93 | Name = format("${var.name}-srv-%02d", count.index + 1) 94 | auto_join = var.auto_join_value 95 | } 96 | 97 | root_block_device { 98 | volume_type = "gp2" 99 | volume_size = var.root_block_device_size 100 | delete_on_termination = "true" 101 | } 102 | 103 | user_data = element(data.template_cloudinit_config.server.*.rendered, count.index) 104 | } 105 | 106 | resource "random_id" "gossip" { 107 | byte_length = 32 108 | } 109 | 110 | resource "random_uuid" "consul_init_token" { 111 | } 112 | 113 | resource "aws_route53_record" "server" { 114 | count = local.server_count 115 | zone_id = data.aws_route53_zone.selected.zone_id 116 | name = lookup(aws_instance.server.*.tags[count.index], "Name") 117 | type = "A" 118 | ttl = "300" 119 | records = [element(aws_instance.server.*.public_ip, count.index)] 120 | } -------------------------------------------------------------------------------- /templates/OLD/server.sh.240102: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ######################## 4 | ### COMMON BLOCK ### 5 | ######################## 6 | common() { 7 | sudo echo "${server_cert}" > /etc/ssl/certs/hashistack_fullchain.pem 8 | sudo echo "${server_key}" > /etc/ssl/certs/hashistack_privkey.key 9 | sudo echo "${server_ca}" > /etc/ssl/certs/hashistack_ca.pem 10 | } 11 | 12 | ######################## 13 | ### VAULT BLOCK ### 14 | ######################## 15 | install_vault_apt() { 16 | 17 | sudo apt-get -y install ${vault_apt}=${vault_version} 18 | sudo echo ${vault_lic} > /opt/vault/license.hclic 19 | sudo chown -R vault:vault /opt/vault/ 20 | 21 | sudo chown -R vault:vault /opt/vault/ 22 | 23 | sudo tee /etc/vault.d/vault.hcl > /dev/null < /dev/null < /dev/null < /dev/null < /opt/consul/license.hclic 108 | sudo chown -R consul:consul /opt/consul/ 109 | 110 | 111 | sudo tee /etc/consul.d/consul.hcl > /dev/null < /dev/null < Writing profile" 182 | sudo tee /etc/profile.d/consul.sh > /dev/null < Generating systemd configuration" 190 | sudo tee /etc/systemd/system/consul.service > /dev/null < Starting consul" 212 | sudo systemctl enable consul 213 | sudo systemctl start consul 214 | sleep 2 215 | 216 | } 217 | 218 | ######################## 219 | ### NOMAD BLOCK ### 220 | ######################## 221 | install_nomad_apt() { 222 | 223 | sudo apt-get install -y ${nomad_apt}=${nomad_version} 224 | sudo echo ${nomad_lic} > /opt/nomad/license.hclic 225 | sudo chown -R nomad:nomad /opt/nomad/ 226 | 227 | sudo tee /etc/nomad.d/nomad.hcl > /dev/null < /dev/null < Writing profile" 275 | sudo tee /etc/profile.d/nomad.sh > /dev/null <<"EOF" 276 | #export NOMAD_ADDR="http://${node_name}.node.consul:4646" 277 | export NOMAD_ADDR=http://127.0.0.1:4646 278 | EOF 279 | 280 | source /etc/profile.d/nomad.sh 281 | 282 | echo "--> Generating systemd configuration" 283 | sudo tee /etc/systemd/system/nomad.service > /dev/null < Starting nomad" 303 | sudo systemctl enable nomad 304 | sudo systemctl start nomad 305 | sleep 2 306 | 307 | echo "--> Waiting for all Nomad servers" 308 | while [ "$(nomad server members 2>&1 | grep "alive" | wc -l)" -lt "${server_count}" ]; do 309 | sleep 5 310 | done 311 | 312 | echo "--> Waiting for Nomad leader" 313 | while [ -z "$(curl -s http://localhost:4646/v1/status/leader)" ]; do 314 | sleep 5 315 | done 316 | 317 | echo "==> Nomad Server is Installed!" 318 | 319 | } 320 | 321 | 322 | add_consul_to_vault() { 323 | 324 | sudo tee /etc/vault.d/service-consul.hcl > /dev/null < /dev/null < $env 363 | echo address=${tfc_addr} >> $env 364 | echo organization=${tfc_org} >> $env 365 | echo tfc_var_set=${tfc_var_set} >> $env 366 | echo workspace=${tfc_workspace} >> $env 367 | 368 | 369 | # wait until vault service is in active state 370 | x=1 371 | while [[ `systemctl is-active vault` != "active" ]] && [ $x -le 30 ] 372 | do 373 | sleep 6 374 | x=$(( $x +1 )) 375 | done 376 | 377 | if [[ `systemctl is-active vault` == "active" ]] 378 | then 379 | vault operator init > /root/vault_init.txt 380 | else 381 | exit 1 382 | fi 383 | 384 | if [[ -e /root/vault_init.txt ]] 385 | then 386 | tf_vars.sh -W 387 | else 388 | exit 1 389 | fi 390 | 391 | } 392 | 393 | #################### 394 | ##### MAIN ##### 395 | #################### 396 | 397 | common 398 | [[ ${vault_enabled} = "true" ]] && install_vault_apt 399 | [[ ${consul_enabled} = "true" ]] && install_consul_apt 400 | #[[ ${vault_enabled} = "true" ]] && add_consul_to_vault 401 | [[ ${nomad_enabled} = "true" ]] && install_nomad_apt 402 | additionals 403 | tutorial 404 | [[ ${vault_init} = "true" ]] && [[ `hostname` == "${bastion_host}" ]] && vault_init -------------------------------------------------------------------------------- /templates/OLD/server.sh.240103: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ######################## 4 | ### COMMON BLOCK ### 5 | ######################## 6 | common() { 7 | sudo echo "${server_cert}" > /etc/ssl/certs/hashistack_fullchain.pem 8 | sudo echo "${server_key}" > /etc/ssl/certs/hashistack_privkey.key 9 | sudo echo "${server_ca}" > /etc/ssl/certs/hashistack_ca.pem 10 | } 11 | 12 | ######################## 13 | ### VAULT BLOCK ### 14 | ######################## 15 | install_vault_apt() { 16 | 17 | sudo apt-get -y install ${vault_apt}=${vault_version} 18 | sudo echo ${vault_lic} > /opt/vault/license.hclic 19 | sudo chown -R vault:vault /opt/vault/ 20 | 21 | sudo chown -R vault:vault /opt/vault/ 22 | 23 | sudo tee /etc/vault.d/vault.hcl > /dev/null < /dev/null < /dev/null < /dev/null < /opt/consul/license.hclic 108 | sudo chown -R consul:consul /opt/consul/ 109 | 110 | 111 | sudo tee /etc/consul.d/consul.hcl > /dev/null < /dev/null < /dev/null < Writing profile" 196 | sudo tee /etc/profile.d/consul.sh > /dev/null < Generating systemd configuration" 204 | sudo tee /etc/systemd/system/consul.service > /dev/null < Starting consul" 226 | sudo systemctl enable consul 227 | sudo systemctl start consul 228 | sleep 2 229 | 230 | } 231 | 232 | ######################## 233 | ### NOMAD BLOCK ### 234 | ######################## 235 | install_nomad_apt() { 236 | 237 | sudo apt-get install -y ${nomad_apt}=${nomad_version} 238 | sudo echo ${nomad_lic} > /opt/nomad/license.hclic 239 | sudo chown -R nomad:nomad /opt/nomad/ 240 | 241 | sudo tee /etc/nomad.d/nomad.hcl > /dev/null < /dev/null < Writing profile" 289 | sudo tee /etc/profile.d/nomad.sh > /dev/null <<"EOF" 290 | #export NOMAD_ADDR="http://${node_name}.node.consul:4646" 291 | export NOMAD_ADDR=http://127.0.0.1:4646 292 | EOF 293 | 294 | source /etc/profile.d/nomad.sh 295 | 296 | echo "--> Generating systemd configuration" 297 | sudo tee /etc/systemd/system/nomad.service > /dev/null < Starting nomad" 317 | sudo systemctl enable nomad 318 | sudo systemctl start nomad 319 | sleep 2 320 | 321 | echo "--> Waiting for all Nomad servers" 322 | while [ "$(nomad server members 2>&1 | grep "alive" | wc -l)" -lt "${server_count}" ]; do 323 | sleep 5 324 | done 325 | 326 | echo "--> Waiting for Nomad leader" 327 | while [ -z "$(curl -s http://localhost:4646/v1/status/leader)" ]; do 328 | sleep 5 329 | done 330 | 331 | echo "==> Nomad Server is Installed!" 332 | 333 | } 334 | 335 | 336 | add_consul_to_vault() { 337 | 338 | sudo tee /etc/vault.d/service-consul.hcl > /dev/null < /dev/null < $env 377 | echo address=${tfc_addr} >> $env 378 | echo organization=${tfc_org} >> $env 379 | echo tfc_var_set=${tfc_var_set} >> $env 380 | echo workspace=${tfc_workspace} >> $env 381 | 382 | 383 | # wait until vault service is in active state 384 | x=1 385 | while [[ `systemctl is-active vault` != "active" ]] && [ $x -le 30 ] 386 | do 387 | sleep 6 388 | x=$(( $x +1 )) 389 | done 390 | 391 | if [[ `systemctl is-active vault` == "active" ]] 392 | then 393 | vault operator init > /root/vault_init.txt 394 | else 395 | exit 1 396 | fi 397 | 398 | if [[ -e /root/vault_init.txt ]] 399 | then 400 | tf_vars.sh -W 401 | else 402 | exit 1 403 | fi 404 | 405 | } 406 | 407 | #################### 408 | ##### MAIN ##### 409 | #################### 410 | 411 | common 412 | [[ ${vault_enabled} = "true" ]] && install_vault_apt 413 | [[ ${consul_enabled} = "true" ]] && install_consul_apt 414 | #[[ ${vault_enabled} = "true" ]] && add_consul_to_vault 415 | [[ ${nomad_enabled} = "true" ]] && install_nomad_apt 416 | additionals 417 | tutorial 418 | [[ ${vault_init} = "true" ]] && [[ `hostname` == "${bastion_host}" ]] && vault_init -------------------------------------------------------------------------------- /templates/OLD/server.sh.bak: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ######################## 4 | ### COMMON BLOCK ### 5 | ######################## 6 | common() { 7 | sudo echo "${server_cert}" > /etc/ssl/certs/hashistack_fullchain.pem 8 | sudo echo "${server_key}" > /etc/ssl/certs/hashistack_privkey.key 9 | sudo echo "${server_ca}" > /etc/ssl/certs/hashistack_ca.pem 10 | } 11 | 12 | ######################## 13 | ### VAULT BLOCK ### 14 | ######################## 15 | install_vault_apt() { 16 | 17 | sudo apt-get -y install ${vault_apt}=${vault_version} 18 | sudo echo ${vault_lic} > /opt/vault/license.hclic 19 | sudo chown -R vault:vault /opt/vault/ 20 | 21 | sudo chown -R vault:vault /opt/vault/ 22 | 23 | sudo tee /etc/vault.d/vault.hcl > /dev/null < /dev/null < /dev/null < /dev/null < /opt/consul/license.hclic 108 | sudo chown -R consul:consul /opt/consul/ 109 | 110 | 111 | sudo tee /etc/consul.d/consul.hcl > /dev/null < /dev/null < Writing profile" 179 | sudo tee /etc/profile.d/consul.sh > /dev/null < Generating systemd configuration" 187 | sudo tee /etc/systemd/system/consul.service > /dev/null < Starting consul" 209 | sudo systemctl enable consul 210 | sudo systemctl start consul 211 | sleep 2 212 | 213 | } 214 | 215 | ######################## 216 | ### NOMAD BLOCK ### 217 | ######################## 218 | install_nomad_apt() { 219 | 220 | sudo apt-get install -y ${nomad_apt}=${nomad_version} 221 | sudo echo ${nomad_lic} > /opt/nomad/license.hclic 222 | sudo chown -R nomad:nomad /opt/nomad/ 223 | 224 | sudo tee /etc/nomad.d/nomad.hcl > /dev/null < /dev/null < Writing profile" 272 | sudo tee /etc/profile.d/nomad.sh > /dev/null <<"EOF" 273 | #export NOMAD_ADDR="http://${node_name}.node.consul:4646" 274 | export NOMAD_ADDR=http://127.0.0.1:4646 275 | EOF 276 | 277 | source /etc/profile.d/nomad.sh 278 | 279 | echo "--> Generating systemd configuration" 280 | sudo tee /etc/systemd/system/nomad.service > /dev/null < Starting nomad" 300 | sudo systemctl enable nomad 301 | sudo systemctl start nomad 302 | sleep 2 303 | 304 | echo "--> Waiting for all Nomad servers" 305 | while [ "$(nomad server members 2>&1 | grep "alive" | wc -l)" -lt "${server_count}" ]; do 306 | sleep 5 307 | done 308 | 309 | echo "--> Waiting for Nomad leader" 310 | while [ -z "$(curl -s http://localhost:4646/v1/status/leader)" ]; do 311 | sleep 5 312 | done 313 | 314 | echo "==> Nomad Server is Installed!" 315 | 316 | } 317 | 318 | 319 | add_consul_to_vault() { 320 | 321 | sudo tee /etc/vault.d/service-consul.hcl > /dev/null < /dev/null < $env 360 | echo address=${tfc_addr} >> $env 361 | echo organization=${tfc_org} >> $env 362 | echo tfc_var_set=${tfc_var_set} >> $env 363 | echo workspace=${tfc_workspace} >> $env 364 | 365 | 366 | # wait until vault service is in active state 367 | x=1 368 | while [[ `systemctl is-active vault` != "active" ]] && [ $x -le 30 ] 369 | do 370 | sleep 6 371 | x=$(( $x +1 )) 372 | done 373 | 374 | if [[ `systemctl is-active vault` == "active" ]] 375 | then 376 | vault operator init > /root/vault_init.txt 377 | else 378 | exit 1 379 | fi 380 | 381 | if [[ -e /root/vault_init.txt ]] 382 | then 383 | tf_vars.sh -W 384 | else 385 | exit 1 386 | fi 387 | 388 | } 389 | 390 | #################### 391 | ##### MAIN ##### 392 | #################### 393 | 394 | common 395 | [[ ${vault_enabled} = "true" ]] && install_vault_apt 396 | [[ ${consul_enabled} = "true" ]] && install_consul_apt 397 | #[[ ${vault_enabled} = "true" ]] && add_consul_to_vault 398 | [[ ${nomad_enabled} = "true" ]] && install_nomad_apt 399 | additionals 400 | tutorial 401 | [[ ${vault_init} = "true" ]] && [[ `hostname` == "${bastion_host}" ]] && vault_init -------------------------------------------------------------------------------- /templates/base.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "--> Adding helper for IP retrieval" 3 | sudo tee /etc/profile.d/ips.sh > /dev/null < Updating apt-cache" 15 | apt-get -y update 16 | 17 | echo "--> Installing common dependencies" 18 | apt-get -y install \ 19 | unzip ntp dnsmasq dnsmasq-base dnsutils jq 20 | 21 | echo "--> Setting hostname..." 22 | echo "${node_name}" | sudo tee /etc/hostname 23 | sudo hostname -F /etc/hostname 24 | 25 | echo "--> Adding hostname to /etc/hosts" 26 | sudo tee -a /etc/hosts > /dev/null < /opt/consul/license.hclic 8 | sudo chown -R consul:consul /opt/consul/ 9 | 10 | sudo tee /etc/consul.d/consul.hcl > /dev/null < /dev/null < Writing profile" 50 | sudo tee /etc/profile.d/consul.sh > /dev/null <<"EOF" 51 | export CONSUL_HTTP_ADDR=http://127.0.0.1:8500 52 | EOF 53 | 54 | source /etc/profile.d/consul.sh 55 | 56 | echo "--> Generating systemd configuration" 57 | sudo tee /etc/systemd/system/consul.service > /dev/null < Starting consul" 81 | sudo systemctl enable consul 82 | sudo systemctl start consul 83 | sleep 2 84 | } 85 | 86 | #################################### 87 | install_nomad_apt() { 88 | #################################### 89 | sudo apt-get install -y ${nomad_apt}=${nomad_version} 90 | sudo chown -R nomad:nomad /opt/nomad/ 91 | 92 | sudo tee /etc/nomad.d/nomad.hcl > /dev/null < /dev/null < Writing profile" 149 | sudo tee /etc/profile.d/nomad.sh > /dev/null <<"EOF" 150 | #export NOMAD_ADDR="http://${node_name}.node.consul:4646" 151 | export NOMAD_ADDR=http://127.0.0.1:4646 152 | EOF 153 | 154 | source /etc/profile.d/nomad.sh 155 | 156 | echo "--> Generating systemd configuration" 157 | sudo tee /etc/systemd/system/nomad.service > /dev/null < Starting nomad" 179 | sudo systemctl enable nomad 180 | sudo systemctl start nomad 181 | sleep 2 182 | 183 | echo "--> Waiting for Nomad leader" 184 | while [ -z "$(curl -s http://localhost:4646/v1/status/leader)" ]; do 185 | sleep 5 186 | done 187 | 188 | echo "==> Nomad Client is Installed!" 189 | } 190 | 191 | additionals() { 192 | 193 | sudo apt-get -y install consul-template vault 194 | 195 | } 196 | 197 | #################### 198 | ##### MAIN ##### 199 | #################### 200 | 201 | [[ ${consul_enabled} = "true" ]] && install_consul_apt 202 | [[ ${nomad_enabled} = "true" ]] && install_nomad_apt 203 | additionals -------------------------------------------------------------------------------- /templates/docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "==> Docker" 3 | 4 | echo "--> Adding keyserver" 5 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &>/dev/null 6 | 7 | echo "--> Adding repo" 8 | sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" 9 | 10 | echo "--> Updating cache" 11 | apt-get update 12 | 13 | echo "--> Installing" 14 | apt-get -y install docker-ce 15 | 16 | echo "--> Allowing docker without sudo" 17 | sudo usermod -aG docker "$(whoami)" 18 | 19 | echo "==> Docker is done!" -------------------------------------------------------------------------------- /templates/server.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ######################## 4 | ### COMMON BLOCK ### 5 | ######################## 6 | common() { 7 | sudo echo "${server_cert}" > /etc/ssl/certs/hashistack_fullchain.pem 8 | sudo echo "${server_key}" > /etc/ssl/certs/hashistack_privkey.key 9 | sudo echo "${server_ca}" > /etc/ssl/certs/hashistack_ca.pem 10 | } 11 | 12 | ######################## 13 | ### VAULT BLOCK ### 14 | ######################## 15 | install_vault_apt() { 16 | 17 | sudo apt-get -y install ${vault_apt}=${vault_version} 18 | sudo echo ${vault_lic} > /opt/vault/license.hclic 19 | sudo chown -R vault:vault /opt/vault/ 20 | 21 | sudo chown -R vault:vault /opt/vault/ 22 | 23 | sudo tee /etc/vault.d/vault.hcl > /dev/null < /dev/null < /dev/null < /dev/null < /opt/consul/license.hclic 108 | sudo chown -R consul:consul /opt/consul/ 109 | 110 | 111 | sudo tee /etc/consul.d/consul.hcl > /dev/null < /dev/null < /dev/null < Writing profile" 199 | sudo tee /etc/profile.d/consul.sh > /dev/null < Generating systemd configuration" 207 | sudo tee /etc/systemd/system/consul.service > /dev/null < Starting consul" 229 | sudo systemctl enable consul 230 | sudo systemctl start consul 231 | sleep 2 232 | 233 | } 234 | 235 | ######################## 236 | ### NOMAD BLOCK ### 237 | ######################## 238 | install_nomad_apt() { 239 | 240 | sudo apt-get install -y ${nomad_apt}=${nomad_version} 241 | sudo echo ${nomad_lic} > /opt/nomad/license.hclic 242 | sudo chown -R nomad:nomad /opt/nomad/ 243 | 244 | sudo tee /etc/nomad.d/nomad.hcl > /dev/null < /dev/null < Writing profile" 292 | sudo tee /etc/profile.d/nomad.sh > /dev/null <<"EOF" 293 | #export NOMAD_ADDR="http://${node_name}.node.consul:4646" 294 | export NOMAD_ADDR=http://127.0.0.1:4646 295 | EOF 296 | 297 | source /etc/profile.d/nomad.sh 298 | 299 | echo "--> Generating systemd configuration" 300 | sudo tee /etc/systemd/system/nomad.service > /dev/null < Starting nomad" 320 | sudo systemctl enable nomad 321 | sudo systemctl start nomad 322 | sleep 2 323 | 324 | echo "--> Waiting for all Nomad servers" 325 | while [ "$(nomad server members 2>&1 | grep "alive" | wc -l)" -lt "${server_count}" ]; do 326 | sleep 5 327 | done 328 | 329 | echo "--> Waiting for Nomad leader" 330 | while [ -z "$(curl -s http://localhost:4646/v1/status/leader)" ]; do 331 | sleep 5 332 | done 333 | 334 | echo "==> Nomad Server is Installed!" 335 | 336 | } 337 | 338 | 339 | additionals() { 340 | sudo apt-get -y install consul-template jq 341 | } 342 | 343 | tutorial() { 344 | sudo tee ~/readme.txt > /dev/null < $env 370 | echo address=${tfc_addr} >> $env 371 | echo organization=${tfc_org} >> $env 372 | echo tfc_var_set=${tfc_var_set} >> $env 373 | echo workspace=${tfc_workspace} >> $env 374 | 375 | 376 | # wait until vault service is in active state 377 | x=1 378 | while [[ `systemctl is-active vault` != "active" ]] && [ $x -le 30 ] 379 | do 380 | sleep 6 381 | x=$(( $x +1 )) 382 | done 383 | 384 | if [[ `systemctl is-active vault` == "active" ]] 385 | then 386 | vault operator init > /root/vault_init.txt 387 | else 388 | exit 1 389 | fi 390 | 391 | if [[ -e /root/vault_init.txt ]] 392 | then 393 | tf_vars.sh -W 394 | else 395 | exit 1 396 | fi 397 | 398 | } 399 | 400 | #################### 401 | ##### MAIN ##### 402 | #################### 403 | 404 | common 405 | [[ ${vault_enabled} = "true" ]] && install_vault_apt 406 | [[ ${consul_enabled} = "true" ]] && install_consul_apt 407 | [[ ${nomad_enabled} = "true" ]] && install_nomad_apt 408 | additionals 409 | tutorial 410 | [[ ${vault_init} = "true" ]] && [[ `hostname` == "${bastion_host}" ]] && vault_init -------------------------------------------------------------------------------- /templates/tfe.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | prerequisites_orig() { 4 | sudo mkdir -p ${tfe_disk_path} 5 | sudo echo "${tfe_lic}" > ${tfe_disk_path}/license.rli 6 | 7 | curl https://install.terraform.io/ptfe/stable > ${tfe_disk_path}/install.sh 8 | curl https://install.terraform.io/tfe/uninstall > ${tfe_disk_path}/uninstall.sh 9 | } 10 | 11 | prerequisites() { 12 | mkdir -p ${tfe_disk_path} 13 | 14 | if [[ ${tfe_airgapped} = "true" ]] 15 | then 16 | echo "${tfe_lic}" | base64 -d > ${tfe_disk_path}/license.rli 17 | else 18 | echo "${tfe_lic}" > ${tfe_disk_path}/license.rli 19 | fi 20 | 21 | curl https://install.terraform.io/ptfe/stable > ${tfe_disk_path}/install.sh 22 | curl https://install.terraform.io/tfe/uninstall > ${tfe_disk_path}/uninstall.sh 23 | } 24 | 25 | cert_is_tf_tls_provider() { 26 | # FIXME: need to be tested 27 | echo "${tfe_tls_cert}" > /etc/ssl/certs/tfe_fullchain.pem 28 | echo "${tfe_tls_key}" > /etc/ssl/certs/tfe_privkey.pem 29 | echo "${tfe_tls_ca}" > /etc/ssl/certs/tfe_ca.pem 30 | 31 | tee /etc/replicated.conf > /dev/null < /dev/null < /dev/null < /dev/null <