├── .circleci └── config.yml ├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── alb_external.tf ├── alb_external_route53.tf ├── alb_internal.tf ├── alb_internal_route53.tf ├── examples ├── 01-env.sh ├── 02-bootstrap.sh ├── 10-init.sh ├── README.md ├── echoserver.yaml ├── helm-service-account.yaml ├── helmfile.yaml ├── managed-services │ ├── README.md │ ├── efs.tf │ ├── helmfile.yaml │ └── rds.tf └── oidc │ ├── README.md │ └── helmfile.yaml ├── images └── aws-diagram.png ├── main.tf ├── outputs.tf └── variables.tf /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | jobs: 3 | build: 4 | docker: 5 | - image: hashicorp/terraform:light 6 | environment: 7 | AWS_REGION: us-west-2 8 | steps: 9 | - checkout 10 | - run: terraform init 11 | - run: terraform fmt -check=true -diff=true 12 | - run: terraform validate -var kops_cluster_name=hello.k8s.local -var kubernetes_ingress_domain=hello.example.com 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | indent_style = space 3 | end_of_line = lf 4 | charset = utf-8 5 | indent_size = 2 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform/ 2 | -------------------------------------------------------------------------------- /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 2017 Hidetake Iwata 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 | # terraform-aws-kops-alb [![CircleCI](https://circleci.com/gh/int128/terraform-aws-kops-alb.svg?style=shield)](https://circleci.com/gh/int128/terraform-aws-kops-alb) 2 | 3 | This is a Terraform module which provides AWS ALB and Route53 for publishing services via nginx-ingress on kops. 4 | 5 | It provides the following stack: 6 | 7 | ![aws-diagram.png](images/aws-diagram.png) 8 | 9 | HTTPS requests are transfered to a pod via the external ALB and the node port of nginx-ingress (e.g. tcp/30080). 10 | 11 | This is an open source software licensed under Apache License 2.0. 12 | Feel free to bring up issues and pull requests. 13 | 14 | ## Example 15 | 16 | Bootstrap a Kubernetes cluster: 17 | 18 | ```sh 19 | export AWS_DEFAULT_REGION="us-west-2" 20 | export KOPS_CLUSTER_NAME="hello.k8s.local" 21 | kubernetes_ingress_domain="hello.example.com" 22 | state_store_bucket_name="state.hello.k8s.local" 23 | 24 | # Create a public hosted zone for the domain 25 | aws route53 create-hosted-zone --name "$kubernetes_ingress_domain" --caller-reference "$(date)" 26 | 27 | # Request a certificate for the wildcard domain 28 | aws acm request-certificate --domain-name "*.$kubernetes_ingress_domain" --validation-method DNS 29 | 30 | # Create a bucket for the state store of kops and Terraform 31 | aws s3api create-bucket \ 32 | --bucket "$state_store_bucket_name" \ 33 | --region "$AWS_DEFAULT_REGION" \ 34 | --create-bucket-configuration "LocationConstraint=$AWS_DEFAULT_REGION" 35 | aws s3api put-bucket-versioning \ 36 | --bucket "$state_store_bucket_name" \ 37 | --versioning-configuration "Status=Enabled" 38 | 39 | # Create a cluster 40 | kops create cluster --name "$KOPS_CLUSTER_NAME" --zones "${AWS_DEFAULT_REGION}a" 41 | kops update cluster --yes 42 | ``` 43 | 44 | Load the module: 45 | 46 | ```hcl 47 | module "kops_alb" { 48 | source = "int128/kops-alb/aws" 49 | kops_cluster_name = "hello.k8s.local" 50 | kubernetes_ingress_domain = "hello.example.com" 51 | } 52 | ``` 53 | 54 | Run Terraform and then install nginx-ingress: 55 | 56 | ```sh 57 | # Create AWS resources 58 | terraform init 59 | terraform apply 60 | 61 | # Install Helm 62 | kubectl create -f helm-service-account.yaml 63 | helm init --service-account tiller --history-max 100 64 | 65 | # Install nginx-ingress 66 | helmfile sync 67 | ``` 68 | 69 | See [examples](examples) for more. 70 | 71 | ## Prerequisite 72 | 73 | This module assumes the following resources exist: 74 | 75 | - Route53 hosted zone of the domain `kubernetes_ingress_domain` 76 | - ACM certificate of the wildcard domain `*.kubernetes_ingress_domain` 77 | - VPC for the cluster `kops_cluster_name` 78 | - Subnets for the cluster `kops_cluster_name` 79 | - Auto scaling group for the nodes of the cluster `kops_cluster_name` 80 | - Security group for the masters of the cluster `kops_cluster_name` 81 | - Security group for the nodes of the cluster `kops_cluster_name` 82 | 83 | ## Inputs 84 | 85 | | Name | Description | Type | Default | Required | 86 | |------|-------------|:----:|:-----:|:-----:| 87 | | alb\_external\_allow\_ip | List of IP addresses to allow to the external ALB | list | `` | no | 88 | | alb\_internal\_enabled | Enable the internal ALB (needed if the external ALB is not public) | string | `"false"` | no | 89 | | kops\_cluster\_name | Kubernetes cluster name | string | n/a | yes | 90 | | kops\_ig\_nodes\_names | List of name of instance-group nodes managed by kops | list | `` | no | 91 | | kubernetes\_ingress\_domain | Domain name for the external/internal ALB | string | n/a | yes | 92 | | kubernetes\_ingress\_port | Node port of the ingress controller | string | `"30080"` | no | 93 | 94 | ## Outputs 95 | 96 | | Name | Description | 97 | |------|-------------| 98 | | kops\_cluster\_name | Kubernetes cluster name | 99 | | kops\_subnet\_ids | IDs of subnets managed by kops | 100 | | kops\_vpc\_id | ID of VPC managed by kops | 101 | | sgid\_allow\_from\_nodes | ID of security group which is allowed from Kubernetes nodes | 102 | -------------------------------------------------------------------------------- /alb_external.tf: -------------------------------------------------------------------------------- 1 | ## External ALB for Kubernetes services. 2 | 3 | resource "aws_lb" "alb_external" { 4 | name = "alb-ext-${local.cluster_name_hash}" 5 | load_balancer_type = "application" 6 | internal = false 7 | idle_timeout = 180 8 | subnets = ["${data.aws_subnet_ids.kops_subnets.ids}"] 9 | security_groups = ["${aws_security_group.alb_external.id}"] 10 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 11 | } 12 | 13 | resource "aws_security_group" "alb_external" { 14 | name = "alb.ext.nodes.${var.kops_cluster_name}" 15 | description = "Security group for external ALB" 16 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 17 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 18 | 19 | ingress { 20 | from_port = 443 21 | to_port = 443 22 | protocol = "tcp" 23 | cidr_blocks = "${var.alb_external_allow_ip}" 24 | } 25 | 26 | egress { 27 | from_port = 0 28 | to_port = 0 29 | protocol = "-1" 30 | cidr_blocks = ["0.0.0.0/0"] 31 | } 32 | } 33 | 34 | resource "aws_security_group_rule" "alb_external" { 35 | description = "Allow from external ALB" 36 | type = "ingress" 37 | from_port = 0 38 | to_port = 0 39 | protocol = "-1" 40 | source_security_group_id = "${aws_security_group.alb_external.id}" 41 | security_group_id = "${data.aws_security_group.kops_nodes.id}" 42 | } 43 | 44 | resource "aws_lb_listener" "alb_external" { 45 | load_balancer_arn = "${aws_lb.alb_external.arn}" 46 | port = 443 47 | protocol = "HTTPS" 48 | certificate_arn = "${data.aws_acm_certificate.service.arn}" 49 | ssl_policy = "ELBSecurityPolicy-2016-08" 50 | 51 | default_action { 52 | target_group_arn = "${aws_lb_target_group.alb_external.arn}" 53 | type = "forward" 54 | } 55 | } 56 | 57 | resource "aws_lb_target_group" "alb_external" { 58 | name = "alb-ext-${local.cluster_name_hash}" 59 | port = "${var.kubernetes_ingress_port}" 60 | protocol = "HTTP" 61 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 62 | deregistration_delay = 30 63 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 64 | 65 | health_check { 66 | path = "/healthz" 67 | } 68 | } 69 | 70 | resource "aws_autoscaling_attachment" "alb_external" { 71 | autoscaling_group_name = "${join(",", data.aws_autoscaling_groups.kops_nodes.names)}" 72 | alb_target_group_arn = "${aws_lb_target_group.alb_external.arn}" 73 | } 74 | -------------------------------------------------------------------------------- /alb_external_route53.tf: -------------------------------------------------------------------------------- 1 | ## Route53 for the external ALB. 2 | 3 | resource "aws_route53_record" "alb_external" { 4 | zone_id = "${data.aws_route53_zone.service.zone_id}" 5 | name = "*.${var.kubernetes_ingress_domain}" 6 | type = "A" 7 | 8 | alias { 9 | name = "${aws_lb.alb_external.dns_name}" 10 | zone_id = "${aws_lb.alb_external.zone_id}" 11 | evaluate_target_health = false 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /alb_internal.tf: -------------------------------------------------------------------------------- 1 | ## Internal ALB for Kubernetes services. 2 | ## This is needed if the security group of the external ALB is not open, 3 | ## because k8s nodes can not access to services via the external ALB. 4 | 5 | resource "aws_lb" "alb_internal" { 6 | count = "${var.alb_internal_enabled}" 7 | name = "alb-int-${local.cluster_name_hash}" 8 | load_balancer_type = "application" 9 | internal = true 10 | idle_timeout = 180 11 | subnets = ["${data.aws_subnet_ids.kops_subnets.ids}"] 12 | security_groups = ["${aws_security_group.alb_internal.id}"] 13 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 14 | } 15 | 16 | resource "aws_security_group" "alb_internal" { 17 | count = "${var.alb_internal_enabled}" 18 | name = "alb.int.nodes.${var.kops_cluster_name}" 19 | description = "Security group for internal ALB" 20 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 21 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 22 | 23 | ingress { 24 | description = "Allow from Kubernetes masters and nodes" 25 | from_port = 0 26 | to_port = 0 27 | protocol = "-1" 28 | 29 | security_groups = [ 30 | "${data.aws_security_group.kops_masters.id}", 31 | "${data.aws_security_group.kops_nodes.id}", 32 | ] 33 | } 34 | 35 | egress { 36 | from_port = 0 37 | to_port = 0 38 | protocol = "-1" 39 | cidr_blocks = ["0.0.0.0/0"] 40 | } 41 | } 42 | 43 | resource "aws_security_group_rule" "alb_internal" { 44 | count = "${var.alb_internal_enabled}" 45 | description = "Allow from internal ALB" 46 | type = "ingress" 47 | from_port = 0 48 | to_port = 0 49 | protocol = "-1" 50 | source_security_group_id = "${aws_security_group.alb_internal.id}" 51 | security_group_id = "${data.aws_security_group.kops_nodes.id}" 52 | } 53 | 54 | resource "aws_lb_listener" "alb_internal" { 55 | count = "${var.alb_internal_enabled}" 56 | load_balancer_arn = "${aws_lb.alb_internal.arn}" 57 | port = 443 58 | protocol = "HTTPS" 59 | certificate_arn = "${data.aws_acm_certificate.service.arn}" 60 | ssl_policy = "ELBSecurityPolicy-2016-08" 61 | 62 | default_action { 63 | target_group_arn = "${aws_lb_target_group.alb_internal.arn}" 64 | type = "forward" 65 | } 66 | } 67 | 68 | resource "aws_lb_target_group" "alb_internal" { 69 | count = "${var.alb_internal_enabled}" 70 | name = "alb-int-${local.cluster_name_hash}" 71 | port = "${var.kubernetes_ingress_port}" 72 | protocol = "HTTP" 73 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 74 | deregistration_delay = 30 75 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 76 | 77 | health_check { 78 | path = "/healthz" 79 | } 80 | } 81 | 82 | resource "aws_autoscaling_attachment" "alb_internal" { 83 | count = "${var.alb_internal_enabled}" 84 | autoscaling_group_name = "${join(",", data.aws_autoscaling_groups.kops_nodes.names)}" 85 | alb_target_group_arn = "${aws_lb_target_group.alb_internal.arn}" 86 | } 87 | -------------------------------------------------------------------------------- /alb_internal_route53.tf: -------------------------------------------------------------------------------- 1 | ## Route53 for the internal ALB. 2 | 3 | resource "aws_route53_zone" "alb_internal" { 4 | count = "${var.alb_internal_enabled}" 5 | name = "${var.kubernetes_ingress_domain}" 6 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 7 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 8 | } 9 | 10 | resource "aws_route53_record" "alb_internal" { 11 | count = "${var.alb_internal_enabled}" 12 | zone_id = "${aws_route53_zone.alb_internal.zone_id}" 13 | name = "*.${var.kubernetes_ingress_domain}" 14 | type = "A" 15 | 16 | alias { 17 | name = "${aws_lb.alb_internal.dns_name}" 18 | zone_id = "${aws_lb.alb_internal.zone_id}" 19 | evaluate_target_health = false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /examples/01-env.sh: -------------------------------------------------------------------------------- 1 | export AWS_PROFILE=example 2 | export KOPS_CLUSTER_NAME=hello.k8s.local 3 | export kubernetes_ingress_domain=hello.example.com 4 | export state_store_bucket_name="state.$KOPS_CLUSTER_NAME" 5 | -------------------------------------------------------------------------------- /examples/02-bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Bootstrap the Kubernetes cluster and AWS resources. 4 | # See README. 5 | # 6 | if [ -z "$KOPS_CLUSTER_NAME" ]; then 7 | echo "Run the following command before running $0" 8 | echo ' source 01-env.sh' 9 | exit 1 10 | fi 11 | 12 | set -e 13 | set -o pipefail 14 | set -x 15 | cd "$(dirname "$0")" 16 | 17 | # Show versions 18 | kops version 19 | terraform version 20 | helm version -c 21 | helmfile -v 22 | 23 | # Make sure you can access to the cluster 24 | while ! kops validate cluster; do 25 | echo "Waiting until the cluster is available..." 26 | sleep 10 27 | done 28 | 29 | # Create AWS resources 30 | terraform apply 31 | 32 | # Reload the environment variable for the EFS filesystem ID. 33 | source 01-env.sh 34 | 35 | # Initialize Helm 36 | kubectl create -f helm-service-account.yaml 37 | helm init --service-account tiller --history-max 100 38 | while ! helm version; do 39 | echo "Waiting until the helm tiller is available..." 40 | sleep 10 41 | done 42 | 43 | # Install Helm charts 44 | helmfile sync 45 | 46 | # Switch default storage class to EFS 47 | kubectl patch storageclass gp2 -p '{"metadata": {"annotations": {"storageclass.beta.kubernetes.io/is-default-class": null}}}' 48 | -------------------------------------------------------------------------------- /examples/10-init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Bootstrap the Kubernetes cluster and AWS resources. 4 | # See README. 5 | # 6 | if [ -z "$KOPS_CLUSTER_NAME" ]; then 7 | echo "Run the following command before running $0" 8 | echo ' source 01-env.sh' 9 | exit 1 10 | fi 11 | 12 | set -e 13 | set -o pipefail 14 | set -x 15 | cd "$(dirname "$0")" 16 | 17 | # Show versions 18 | kops version 19 | terraform version 20 | helm version -c 21 | helmfile -v 22 | 23 | # Initialize kubecontext 24 | kops export kubecfg 25 | kops validate cluster 26 | 27 | # Initialize Terraform 28 | terraform init -backend-config="bucket=$state_store_bucket_name" 29 | 30 | # Initialize Helm 31 | helm init --client-only 32 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Kubernetes on AWS with kops and Terraform 2 | 3 | This bootstraps the following stack in a few minutes: 4 | 5 | ![aws-diagram.png](../images/aws-diagram.png) 6 | 7 | 8 | ## Goals 9 | 10 | - Expose services via HTTPS using [nginx-ingress](https://github.com/kubernetes/charts/tree/master/stable/nginx-ingress), NodePort, ALB, ACM and Route53. 11 | - Manage the cluster using `kubectl`, `helmfile`, `kops` and `terraform`. 12 | 13 | 14 | ## Build a new cluster 15 | 16 | ### 1. Setup your environment 17 | 18 | Make sure you have the following items: 19 | 20 | - An AWS account 21 | - An IAM user with [these permissions](https://github.com/kubernetes/kops/blob/master/docs/aws.md) 22 | - A domain or subdomain, e.g. `dev.example.com` 23 | 24 | Install the following tools: 25 | 26 | ```sh 27 | brew install awscli kubectl kops terraform helm helmfile 28 | ``` 29 | 30 | Create a private repository for configuration management. 31 | 32 | Create `01-env.sh` with the following variables: 33 | 34 | ```sh 35 | export AWS_PROFILE=example 36 | export KOPS_CLUSTER_NAME=hello.k8s.local 37 | export kubernetes_ingress_domain=hello.example.com 38 | export state_store_bucket_name="state.$KOPS_CLUSTER_NAME" 39 | ``` 40 | 41 | Load the values: 42 | 43 | ```sh 44 | source 01-env.sh 45 | ``` 46 | 47 | Configure your AWS credentials: 48 | 49 | ```sh 50 | aws configure --profile "$AWS_PROFILE" 51 | ``` 52 | 53 | 54 | ### 2. Create base components 55 | 56 | #### 2-1. Route53 57 | 58 | Create a public hosted zone for the domain: 59 | 60 | ```sh 61 | aws route53 create-hosted-zone --name "$kubernetes_ingress_domain" --caller-reference "$(date)" 62 | ``` 63 | 64 | You may need to add the NS records to the parent zone. 65 | 66 | 67 | #### 2-2. ACM 68 | 69 | Request a certificate for the wildcard domain: 70 | 71 | ```sh 72 | aws acm request-certificate --domain-name "*.$kubernetes_ingress_domain" --validation-method DNS 73 | ``` 74 | 75 | You need to approve the DNS validation. 76 | Open https://console.aws.amazon.com/acm/home and click the "Create record in Route53" button. 77 | See [AWS User Guide](https://docs.aws.amazon.com/acm/latest/userguide/gs-acm-validate-dns.html) for more. 78 | 79 | 80 | #### 2-3. S3 81 | 82 | Create a bucket for the state store of kops and Terraform. 83 | You must enable bucket versioning. 84 | 85 | ```sh 86 | aws s3api create-bucket \ 87 | --bucket "$state_store_bucket_name" \ 88 | --region "$AWS_DEFAULT_REGION" \ 89 | --create-bucket-configuration "LocationConstraint=$AWS_DEFAULT_REGION" 90 | aws s3api put-bucket-versioning \ 91 | --bucket "$state_store_bucket_name" \ 92 | --versioning-configuration "Status=Enabled" 93 | ``` 94 | 95 | Initialize the state store of Terraform. 96 | 97 | ```sh 98 | terraform init -backend-config="bucket=$state_store_bucket_name" 99 | ``` 100 | 101 | 102 | ### 3. Create a cluster 103 | 104 | Create a cluster configuration: 105 | 106 | ```sh 107 | kops create cluster \ 108 | --name "$KOPS_CLUSTER_NAME" \ 109 | --zones "${AWS_DEFAULT_REGION}a,${AWS_DEFAULT_REGION}c" \ 110 | --ssh-public-key ~/.ssh/id_rsa 111 | 112 | # recreate the instance group for single AZ 113 | kops create instancegroup "nodes-${AWS_DEFAULT_REGION}a" --name "$KOPS_CLUSTER_NAME" --subnet "${AWS_DEFAULT_REGION}a" --edit=false 114 | kops delete instancegroup nodes --name "$KOPS_CLUSTER_NAME" --yes 115 | ``` 116 | 117 | You can change the configuration: 118 | 119 | ```sh 120 | kops edit cluster 121 | kops edit instancegroup "master-${AWS_DEFAULT_REGION}a" 122 | kops edit instancegroup "nodes-${AWS_DEFAULT_REGION}a" 123 | ``` 124 | 125 | Finally create actual resources for the cluster: 126 | 127 | ```sh 128 | kops update cluster --yes 129 | ``` 130 | 131 | #### Minimum instance type and volume size 132 | 133 | Here is an example of minimum size. 134 | You can use spot instances of `m3.medium`. 135 | As well as you can reduce size of root volumes. 136 | This is not for production. 137 | 138 | ```yaml 139 | # kops edit cluster 140 | spec: 141 | etcdClusters: 142 | - etcdMembers: 143 | - instanceGroup: master-us-west-2a 144 | name: a 145 | volumeSize: 1 146 | name: main 147 | - etcdMembers: 148 | - instanceGroup: master-us-west-2a 149 | name: a 150 | volumeSize: 1 151 | name: events 152 | ``` 153 | 154 | ```yaml 155 | # kops edit instancegroup 156 | spec: 157 | machineType: m3.medium 158 | maxPrice: "0.02" 159 | rootVolumeSize: 10 160 | ``` 161 | 162 | 163 | ### 4. Create additional components 164 | 165 | Run the script: 166 | 167 | ```sh 168 | ./02-bootstrap.sh 169 | ``` 170 | 171 | By default the script will create the following components: 172 | 173 | - Terraform 174 | - An internet-facing ALB 175 | - A Route53 record for the internet-facing ALB 176 | - A security group for the internet-facing ALB 177 | - An EFS filesystem for Persistent Volumes 178 | - kubectl 179 | - Create `ServiceAccount` and `ClusterRoleBinding` for the Helm tiller 180 | - Patch `StorageClass/gp2` to remove the default storage class 181 | - Helm 182 | - [`stable/nginx-ingress`](https://github.com/kubernetes/charts/tree/master/stable/nginx-ingress) 183 | - [`stable/kubernetes-dashboard`](https://github.com/kubernetes/charts/tree/master/stable/kubernetes-dashboard) 184 | - [`int128.github.io/kubernetes-dashboard-proxy`](https://github.com/int128/kubernetes-dashboard-proxy) 185 | - [`stable/heapster`](https://github.com/kubernetes/charts/tree/master/stable/heapster) 186 | - [`stable/efs-provisioner`](https://github.com/helm/charts/tree/master/stable/efs-provisioner) 187 | 188 | 189 | ## Manage the cluster 190 | 191 | ### Operation 192 | 193 | Tell the following steps to your team members. 194 | 195 | Onboarding: 196 | 197 | ```sh 198 | source 01-env.sh 199 | 200 | # Configure your AWS credentials. 201 | aws configure --profile "$AWS_PROFILE" 202 | 203 | # Initialize kubectl and Terraform. 204 | ./10-init.sh 205 | ``` 206 | 207 | Daily operation: 208 | 209 | ```sh 210 | source 01-env.sh 211 | 212 | # Now you can execute the following tools. 213 | kops 214 | terraform 215 | helmfile 216 | ``` 217 | 218 | ### Customizing 219 | 220 | #### Restrict access to Kubernetes API and SSH 221 | 222 | To change access control for the Kubernetes API and SSH: 223 | 224 | ```sh 225 | kops edit cluster 226 | ``` 227 | 228 | ```yaml 229 | spec: 230 | kubernetesApiAccess: 231 | - xxx.xxx.xxx.xxx/32 232 | sshAccess: 233 | - xxx.xxx.xxx.xxx/32 234 | ``` 235 | 236 | Apply the changes for the Kubernetes API and SSH: 237 | 238 | ```sh 239 | kops update cluster 240 | kops update cluster --yes 241 | ``` 242 | 243 | 244 | #### Restrict access to internet-facing ALB 245 | 246 | The following resources are needed so that the masters and nodes can access to services in the VPC: 247 | 248 | - An internal ALB 249 | - A Route53 private hosted zone for the internal ALB 250 | - A Route53 record for the internal ALB 251 | - A security group for the internal ALB 252 | 253 | To change access control for the internet-facing ALB, edit `tf_config.tf`: 254 | 255 | ```tf 256 | variable "alb_external_allow_ip" { 257 | default = [ 258 | "xxx.xxx.xxx.xxx/32", 259 | "xxx.xxx.xxx.xxx/32", 260 | ] 261 | } 262 | 263 | variable "alb_internal_enabled" { 264 | default = true 265 | } 266 | ``` 267 | 268 | Apply the changes for the internet-facing ALB: 269 | 270 | ```sh 271 | terraform apply 272 | ``` 273 | 274 | 275 | #### OIDC authentication 276 | 277 | See [oidc](oidc). 278 | 279 | 280 | #### Working with managed services 281 | 282 | Terraform creates the security group `allow-from-nodes.hello.k8s.local` which allows access from the Kubernetes nodes. 283 | You can attach the security group to managed services such as RDS or Elasticsearch. 284 | 285 | See [managed-services](managed-services). 286 | 287 | 288 | ## Destroy the cluster 289 | 290 | **WARNING:** `kops delete cluster` command will delete all EBS volumes with a tag. 291 | You should take snapshots before destroying. 292 | 293 | ```sh 294 | terraform destroy 295 | kops delete cluster --name "$KOPS_CLUSTER_NAME" --yes 296 | ``` 297 | 298 | 299 | ## Cost 300 | 301 | Running cost depends on number of masters and nodes. 302 | 303 | ### Minimize cost for testing 304 | 305 | Here is a minimum configuration with AWS Free Tier (first 1 year): 306 | 307 | Role | Kind | Spec | Monthly Cost 308 | -----|------|------|------------- 309 | Master | EC2 | m3.medium spot | $5 310 | Master | EBS | gp2 10GB | free 311 | Master | EBS for etcd | gp2 5GB x2 | free 312 | Node | EC2 | m3.medium spot | $5 313 | Node | EBS | gp2 10GB | free 314 | Cluster | EBS for PVs | gp2 | $0.1/GB 315 | Cluster | ALB | - | free 316 | Cluster | Route53 Hosted Zone | - | $0.5 317 | Cluster | S3 | - | free 318 | Managed | EFS | General Purpose up to 5GB | free 319 | Managed | RDS | t2.micro gp2 20GB | free 320 | 321 | The cluster name must be a domain name in order to reduce an ELB for masters. 322 | 323 | ```sh 324 | # 01-env.sh 325 | kubernetes_cluster_name=dev.example.com 326 | ``` 327 | -------------------------------------------------------------------------------- /examples/echoserver.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Namespace 3 | metadata: 4 | name: echoserver 5 | --- 6 | apiVersion: extensions/v1beta1 7 | kind: Deployment 8 | metadata: 9 | name: echoserver 10 | namespace: echoserver 11 | spec: 12 | replicas: 1 13 | template: 14 | metadata: 15 | labels: 16 | app: echoserver 17 | spec: 18 | containers: 19 | - image: gcr.io/google_containers/echoserver:1.8 20 | imagePullPolicy: Always 21 | name: echoserver 22 | ports: 23 | - containerPort: 8080 24 | --- 25 | apiVersion: v1 26 | kind: Service 27 | metadata: 28 | name: echoserver 29 | namespace: echoserver 30 | spec: 31 | type: ClusterIP 32 | ports: 33 | - protocol: TCP 34 | port: 8080 35 | targetPort: 8080 36 | selector: 37 | app: echoserver 38 | --- 39 | apiVersion: extensions/v1beta1 40 | kind: Ingress 41 | metadata: 42 | name: echoserver 43 | namespace: echoserver 44 | spec: 45 | rules: 46 | - host: echoserver.kubernetes_ingress_domain 47 | http: 48 | paths: 49 | - path: / 50 | backend: 51 | serviceName: echoserver 52 | servicePort: 8080 53 | -------------------------------------------------------------------------------- /examples/helm-service-account.yaml: -------------------------------------------------------------------------------- 1 | # https://helm.sh/docs/using_helm/#role-based-access-control 2 | apiVersion: v1 3 | kind: ServiceAccount 4 | metadata: 5 | name: tiller 6 | namespace: kube-system 7 | --- 8 | apiVersion: rbac.authorization.k8s.io/v1 9 | kind: ClusterRoleBinding 10 | metadata: 11 | name: tiller 12 | roleRef: 13 | apiGroup: rbac.authorization.k8s.io 14 | kind: ClusterRole 15 | name: cluster-admin 16 | subjects: 17 | - kind: ServiceAccount 18 | name: tiller 19 | namespace: kube-system 20 | -------------------------------------------------------------------------------- /examples/helmfile.yaml: -------------------------------------------------------------------------------- 1 | releases: 2 | # https://github.com/kubernetes/charts/tree/master/stable/nginx-ingress 3 | - name: nginx-ingress 4 | namespace: kube-system 5 | chart: stable/nginx-ingress 6 | values: 7 | - rbac: 8 | create: true 9 | controller: 10 | replicaCount: 2 11 | resources: 12 | limits: 13 | memory: 128Mi 14 | requests: 15 | memory: 128Mi 16 | service: 17 | type: NodePort 18 | nodePorts: 19 | http: 30080 20 | stats: 21 | enabled: true 22 | config: 23 | # https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/configmap.md 24 | proxy-read-timeout: "180" 25 | proxy-send-timeout: "180" 26 | # Large request header (e.g. OIDC proxy) 27 | proxy-buffer-size: "64k" 28 | # Large request body (e.g. file upload) 29 | proxy-body-size: "512m" 30 | server-tokens: "false" 31 | defaultBackend: 32 | replicaCount: 2 33 | resources: 34 | limits: 35 | memory: 16Mi 36 | requests: 37 | memory: 16Mi 38 | 39 | # https://github.com/kubernetes/charts/tree/master/stable/kubernetes-dashboard 40 | - name: kubernetes-dashboard 41 | namespace: kube-system 42 | chart: stable/kubernetes-dashboard 43 | 44 | # https://github.com/kubernetes/charts/tree/master/stable/heapster 45 | - name: heapster 46 | namespace: kube-system 47 | chart: stable/heapster 48 | -------------------------------------------------------------------------------- /examples/managed-services/README.md: -------------------------------------------------------------------------------- 1 | # Working with AWS managed services 2 | 3 | This contains examples of the following resources: 4 | 5 | - RDS 6 | - EFS 7 | -------------------------------------------------------------------------------- /examples/managed-services/efs.tf: -------------------------------------------------------------------------------- 1 | resource "aws_efs_file_system" "efs_provisioner" { 2 | tags = "${merge( 3 | map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned"), 4 | map("Name", "efs.${var.kops_cluster_name}") 5 | )}" 6 | } 7 | 8 | resource "aws_efs_mount_target" "efs_provisioner" { 9 | count = "${length(data.aws_subnet_ids.kops_subnets.ids)}" 10 | file_system_id = "${aws_efs_file_system.efs_provisioner.id}" 11 | subnet_id = "${data.aws_subnet_ids.kops_subnets.ids[count.index]}" 12 | security_groups = ["${aws_security_group.allow_from_k8s_nodes.id}"] 13 | } 14 | -------------------------------------------------------------------------------- /examples/managed-services/helmfile.yaml: -------------------------------------------------------------------------------- 1 | releases: 2 | # https://github.com/helm/charts/tree/master/stable/efs-provisioner 3 | - name: efs-provisioner 4 | namespace: kube-system 5 | chart: stable/efs-provisioner 6 | values: 7 | - efsProvisioner: 8 | efsFileSystemId: {{ requiredEnv "efs_provisoner_file_system_id" }} 9 | awsRegion: {{ requiredEnv "AWS_DEFAULT_REGION" }} 10 | path: / 11 | storageClass: 12 | name: efs 13 | isDefault: true 14 | -------------------------------------------------------------------------------- /examples/managed-services/rds.tf: -------------------------------------------------------------------------------- 1 | resource "aws_db_subnet_group" "allow_from_k8s_nodes" { 2 | name = "allow-from-nodes.${var.kops_cluster_name}" 3 | subnet_ids = ["${data.aws_subnet_ids.kops_subnets.ids}"] 4 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 5 | } 6 | 7 | resource "aws_db_instance" "rds_for_k8s_nodes" { 8 | # Network 9 | availability_zone = "${data.aws_availability_zones.available.names[0]}" 10 | vpc_security_group_ids = ["${aws_security_group.allow_from_k8s_nodes.id}"] 11 | db_subnet_group_name = "${aws_db_subnet_group.rds_for_k8s_nodes.name}" 12 | publicly_accessible = false 13 | 14 | # Spec 15 | identifier = "kubernetes" 16 | instance_class = "db.t2.micro" 17 | engine = "postgresql" 18 | engine_version = "9.6" 19 | allocated_storage = 20 20 | storage_type = "gp2" 21 | username = "${var.database_admin_username}" 22 | password = "${var.database_admin_password}" 23 | parameter_group_name = "default.postgres9.6" 24 | skip_final_snapshot = true 25 | backup_retention_period = 7 26 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 27 | 28 | lifecycle { 29 | ignore_changes = [ 30 | "password", 31 | "allocated_storage", 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /examples/oidc/README.md: -------------------------------------------------------------------------------- 1 | # OIDC authentication 2 | 3 | You can setup OIDC authentication. 4 | This example uses Keycloak and Kubernetes Dashboard Proxy. 5 | 6 | 7 | ## Keycloak 8 | 9 | ```sh 10 | export keycloak_postgres_host=xxx.xxx.rds.amazonaws.com 11 | ``` 12 | 13 | 14 | ## Kubernetes Dashboard Proxy 15 | 16 | ```sh 17 | export oidc_discovery_url=https://accounts.google.com 18 | export oidc_kubernetes_dashboard_client_id=xxx-xxx.apps.googleusercontent.com 19 | export oidc_kubernetes_dashboard_client_secret=xxxxxx 20 | ``` 21 | 22 | See also the tutorial at [int128/kubernetes-dashboard-proxy](https://github.com/int128/kubernetes-dashboard-proxy). 23 | -------------------------------------------------------------------------------- /examples/oidc/helmfile.yaml: -------------------------------------------------------------------------------- 1 | releases: 2 | # https://github.com/codecentric/helm-charts/tree/master/charts/keycloak 3 | - name: keycloak 4 | namespace: oidc 5 | chart: codecentric/keycloak 6 | values: 7 | - keycloak: 8 | ingress: 9 | enabled: true 10 | path: /auth 11 | hosts: 12 | - keycloak.{{ requiredEnv "kubernetes_ingress_domain" }} 13 | persistence: 14 | dbVendor: postgres 15 | dbHost: {{ requiredEnv "keycloak_postgres_host" }} 16 | dbUser: keycloak 17 | dbPassword: keycloak 18 | resources: 19 | limits: 20 | memory: 512Mi 21 | requests: 22 | memory: 512Mi 23 | 24 | # https://github.com/int128/kubernetes-dashboard-proxy 25 | - name: kubernetes-dashboard-proxy 26 | namespace: oidc 27 | chart: int128.github.io/kubernetes-dashboard-proxy 28 | values: 29 | - ingress: 30 | enabled: true 31 | hosts: 32 | - kubernetes-dashboard.{{ requiredEnv "kubernetes_ingress_domain" }} 33 | proxy: 34 | oidc: 35 | discoveryURL: {{ requiredEnv "oidc_discovery_url" }} 36 | redirectURL: https://kubernetes-dashboard.{{ requiredEnv "kubernetes_ingress_domain" }} 37 | clientID: {{ requiredEnv "oidc_kubernetes_dashboard_client_id" }} 38 | clientSecret: {{ requiredEnv "oidc_kubernetes_dashboard_client_secret" }} 39 | resources: 40 | limits: 41 | memory: 32Mi 42 | requests: 43 | memory: 32Mi 44 | 45 | repositories: 46 | - name: codecentric 47 | url: https://codecentric.github.io/helm-charts 48 | - name: int128.github.io 49 | url: https://int128.github.io/helm-charts 50 | -------------------------------------------------------------------------------- /images/aws-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/int128/terraform-aws-kops-alb/cc586b5414617bce78afd704d37289b1476b7933/images/aws-diagram.png -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | ## References to self/kops managed resources. 2 | 3 | data "aws_availability_zones" "available" {} 4 | 5 | ## 6 | ## SELF MANAGED RESOURCES 7 | ## 8 | 9 | # Route53 Hosted Zone of the domain for services 10 | data "aws_route53_zone" "service" { 11 | name = "${var.kubernetes_ingress_domain}." 12 | } 13 | 14 | # Certificate of the domain for services 15 | data "aws_acm_certificate" "service" { 16 | domain = "*.${var.kubernetes_ingress_domain}" 17 | } 18 | 19 | ## 20 | ## KOPS MANAGED RESOURCES 21 | ## 22 | 23 | # VPC for the Kubernetes cluster 24 | data "aws_vpc" "kops_vpc" { 25 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 26 | } 27 | 28 | # Subnets for the Kubernetes cluster 29 | data "aws_subnet_ids" "kops_subnets" { 30 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 31 | } 32 | 33 | # Auto Scaling Group for the Kubernetes nodes 34 | data "aws_autoscaling_groups" "kops_nodes" { 35 | filter { 36 | name = "key" 37 | values = ["Name"] 38 | } 39 | 40 | filter { 41 | name = "value" 42 | values = ["${var.kops_ig_nodes_names}"] 43 | } 44 | } 45 | 46 | # Security Group for the Kubernetes masters 47 | data "aws_security_group" "kops_masters" { 48 | tags { 49 | Name = "masters.${var.kops_cluster_name}" 50 | } 51 | } 52 | 53 | # Security Group for the Kubernetes nodes 54 | data "aws_security_group" "kops_nodes" { 55 | tags { 56 | Name = "nodes.${var.kops_cluster_name}" 57 | } 58 | } 59 | 60 | resource "aws_security_group" "allow_from_k8s_nodes" { 61 | name = "allow-from-nodes.${var.kops_cluster_name}" 62 | description = "Security group for managed services accessed from k8s nodes" 63 | vpc_id = "${data.aws_vpc.kops_vpc.id}" 64 | tags = "${map("kubernetes.io/cluster/${var.kops_cluster_name}", "owned")}" 65 | 66 | ingress { 67 | description = "Allow from Kubernetes nodes" 68 | from_port = 0 69 | to_port = 0 70 | protocol = "-1" 71 | security_groups = ["${data.aws_security_group.kops_nodes.id}"] 72 | } 73 | 74 | egress { 75 | from_port = 0 76 | to_port = 0 77 | protocol = "-1" 78 | cidr_blocks = ["0.0.0.0/0"] 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "kops_cluster_name" { 2 | description = "Kubernetes cluster name" 3 | value = "${var.kops_cluster_name}" 4 | } 5 | 6 | output "sgid_allow_from_nodes" { 7 | description = "ID of security group which is allowed from Kubernetes nodes" 8 | value = "${aws_security_group.allow_from_k8s_nodes.id}" 9 | } 10 | 11 | output "kops_vpc_id" { 12 | description = "ID of VPC managed by kops" 13 | value = "${data.aws_vpc.kops_vpc.id}" 14 | } 15 | 16 | output "kops_subnet_ids" { 17 | description = "IDs of subnets managed by kops" 18 | value = "${data.aws_subnet_ids.kops_subnets.ids}" 19 | } 20 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "kops_cluster_name" { 2 | type = "string" 3 | description = "Kubernetes cluster name" 4 | } 5 | 6 | variable "kops_ig_nodes_names" { 7 | type = "list" 8 | description = "List of name of instance-group nodes managed by kops" 9 | default = ["nodes"] 10 | } 11 | 12 | variable "kubernetes_ingress_domain" { 13 | type = "string" 14 | description = "Domain name for the external/internal ALB" 15 | } 16 | 17 | variable "kubernetes_ingress_port" { 18 | type = "string" 19 | description = "Node port of the ingress controller" 20 | default = 30080 21 | } 22 | 23 | variable "alb_external_allow_ip" { 24 | type = "list" 25 | description = "List of IP addresses to allow to the external ALB" 26 | 27 | default = [ 28 | "0.0.0.0/0", # all 29 | ] 30 | } 31 | 32 | variable "alb_internal_enabled" { 33 | type = "string" 34 | description = "Enable the internal ALB (needed if the external ALB is not public)" 35 | default = false 36 | } 37 | 38 | locals { 39 | # Hash of kops_cluster_name and kubernetes_ingress_domain 40 | cluster_name_hash = "${substr(sha256("${var.kops_cluster_name}/${var.kubernetes_ingress_domain}"), 0, 16)}" 41 | } 42 | --------------------------------------------------------------------------------