commit 4fb43b5628dc90574ce1da4a031c1be935e4dcef Author: Patrick de Ruiter Date: Fri Jul 10 12:54:44 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c6f3c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +**/.idea +**/*.iml + +**/.build-harness +**/build-harness + +# vim editor +*.swp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..101fd7c --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2019 Cloud Posse, LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97d8087 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +SHELL := /bin/bash + +# List of targets the `readme` target should call before generating the readme +export README_DEPS ?= docs/targets.md docs/terraform.md + +-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness) + +## Lint terraform code +lint: + $(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate diff --git a/README.md b/README.md new file mode 100644 index 0000000..5012d88 --- /dev/null +++ b/README.md @@ -0,0 +1,425 @@ + +[![README Header][readme_header_img]][readme_header_link] + +[![Cloud Posse][logo]](https://cpco.io/homepage) + +# terraform-aws-eks-fargate-profile [![Codefresh Build Status](https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-aws-eks-fargate-profile?type=cf-1)](https://g.codefresh.io/public/accounts/cloudposse/pipelines/5deeced5158ef2a8061fe466) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-aws-eks-fargate-profile.svg)](https://github.com/cloudposse/terraform-aws-eks-fargate-profile/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) + + +Terraform module to provision an EKS Node Group for [Elastic Container Service for Kubernetes](https://aws.amazon.com/eks/). + +Instantiate it multiple times to create many EKS node groups with specific settings such as GPUs, EC2 instance types, or autoscale parameters. + + +--- + +This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps. +[][share_email] +[][share_googleplus] +[][share_facebook] +[][share_reddit] +[][share_linkedin] +[][share_twitter] + + +[![Terraform Open Source Modules](https://docs.cloudposse.com/images/terraform-open-source-modules.svg)][terraform_modules] + + + +It's 100% Open Source and licensed under the [APACHE2](LICENSE). + + + + + + + +We literally have [*hundreds of terraform modules*][terraform_modules] that are Open Source and well-maintained. Check them out! + + + + + + +## Introduction + + + +## Usage + + +**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases. +Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-aws-eks-fargate-profile/releases). + + + +For a complete example, see [examples/complete](examples/complete). + +For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) (which tests and deploys the example on AWS), see [test](test). + +```hcl + provider "aws" { + region = var.region + } + + module "label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + namespace = var.namespace + name = var.name + stage = var.stage + delimiter = var.delimiter + attributes = compact(concat(var.attributes, list("cluster"))) + tags = var.tags + } + + locals { + tags = merge(module.label.tags, map("kubernetes.io/cluster/${module.label.id}", "shared")) + } + + module "vpc" { + source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.8.1" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + cidr_block = var.vpc_cidr_block + tags = local.tags + } + + module "subnets" { + source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.18.1" + availability_zones = var.availability_zones + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + vpc_id = module.vpc.vpc_id + igw_id = module.vpc.igw_id + cidr_block = module.vpc.vpc_cidr_block + nat_gateway_enabled = true + nat_instance_enabled = false + tags = local.tags + } + + module "eks_cluster" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.13.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + region = var.region + vpc_id = module.vpc.vpc_id + subnet_ids = module.subnets.public_subnet_ids + kubernetes_version = var.kubernetes_version + kubeconfig_path = var.kubeconfig_path + oidc_provider_enabled = var.oidc_provider_enabled + + workers_role_arns = [module.eks_node_group.eks_node_group_role_arn, module.eks_fargate_profile.eks_fargate_profile_role_arn] + workers_security_group_ids = [] + } + + module "eks_node_group" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-node-group.git?ref=tags/0.1.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.public_subnet_ids + instance_types = var.instance_types + desired_size = var.desired_size + min_size = var.min_size + max_size = var.max_size + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_version = var.kubernetes_version + kubernetes_labels = var.kubernetes_labels + } + + module "eks_fargate_profile" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-fargate-profile.git?ref=master" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.private_subnet_ids + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_namespace = var.kubernetes_namespace + kubernetes_labels = var.kubernetes_labels + } +``` + + + + + + +## Makefile Targets +``` +Available targets: + + help Help screen + help/all Display help for all targets + help/short This help short screen + lint Lint terraform code + +``` +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| attributes | Additional attributes (e.g. `1`) | list(string) | `` | no | +| cluster_name | The name of the EKS cluster | string | - | yes | +| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no | +| enabled | Whether to create the resources. Set to `false` to prevent the module from creating any resources | bool | `true` | no | +| kubernetes_labels | Key-value mapping of Kubernetes labels for selection | map(string) | `` | no | +| kubernetes_namespace | Kubernetes namespace for selection | string | - | yes | +| name | Solution name, e.g. 'app' or 'cluster' | string | - | yes | +| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no | +| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | `` | no | +| subnet_ids | Identifiers of private EC2 Subnets to associate with the EKS Fargate Profile. These subnets must have the following resource tag: kubernetes.io/cluster/CLUSTER_NAME (where CLUSTER_NAME is replaced with the name of the EKS Cluster) | list(string) | - | yes | +| tags | Additional tags (e.g. `{ BusinessUnit = "XYZ" }` | map(string) | `` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| eks_fargate_profile_arn | Amazon Resource Name (ARN) of the EKS Fargate Profile | +| eks_fargate_profile_id | EKS Cluster name and EKS Fargate Profile name separated by a colon | +| eks_fargate_profile_role_arn | ARN of the EKS Fargate Profile IAM role | +| eks_fargate_profile_role_name | Name of the EKS Fargate Profile IAM role | +| eks_fargate_profile_status | Status of the EKS Fargate Profile | + + + + +## Share the Love + +Like this project? Please give it a ★ on [our GitHub](https://github.com/cloudposse/terraform-aws-eks-fargate-profile)! (it helps us **a lot**) + +Are you using this project or any of our other projects? Consider [leaving a testimonial][testimonial]. =) + + +## Related Projects + +Check out these related projects. + +- [terraform-aws-eks-cluster](https://github.com/cloudposse/terraform-aws-eks-cluster) - Terraform module to provision an EKS cluster on AWS +- [terraform-aws-eks-node-group](https://github.com/cloudposse/terraform-aws-eks-node-group) - Terraform module to provision an EKS Node Group +- [terraform-aws-eks-workers](https://github.com/cloudposse/terraform-aws-eks-workers) - Terraform module to provision an AWS AutoScaling Group, IAM Role, and Security Group for EKS Workers +- [terraform-aws-ec2-autoscale-group](https://github.com/cloudposse/terraform-aws-ec2-autoscale-group) - Terraform module to provision Auto Scaling Group and Launch Template on AWS +- [terraform-aws-ecs-container-definition](https://github.com/cloudposse/terraform-aws-ecs-container-definition) - Terraform module to generate well-formed JSON documents (container definitions) that are passed to the aws_ecs_task_definition Terraform resource +- [terraform-aws-ecs-alb-service-task](https://github.com/cloudposse/terraform-aws-ecs-alb-service-task) - Terraform module which implements an ECS service which exposes a web service via ALB +- [terraform-aws-ecs-web-app](https://github.com/cloudposse/terraform-aws-ecs-web-app) - Terraform module that implements a web app on ECS and supports autoscaling, CI/CD, monitoring, ALB integration, and much more +- [terraform-aws-ecs-codepipeline](https://github.com/cloudposse/terraform-aws-ecs-codepipeline) - Terraform module for CI/CD with AWS Code Pipeline and Code Build for ECS +- [terraform-aws-ecs-cloudwatch-autoscaling](https://github.com/cloudposse/terraform-aws-ecs-cloudwatch-autoscaling) - Terraform module to autoscale ECS Service based on CloudWatch metrics +- [terraform-aws-ecs-cloudwatch-sns-alarms](https://github.com/cloudposse/terraform-aws-ecs-cloudwatch-sns-alarms) - Terraform module to create CloudWatch Alarms on ECS Service level metrics +- [terraform-aws-ec2-instance](https://github.com/cloudposse/terraform-aws-ec2-instance) - Terraform module for providing a general purpose EC2 instance +- [terraform-aws-ec2-instance-group](https://github.com/cloudposse/terraform-aws-ec2-instance-group) - Terraform module for provisioning multiple general purpose EC2 hosts for stateful applications + + + +## Help + +**Got a question?** We got answers. + +File a GitHub [issue](https://github.com/cloudposse/terraform-aws-eks-fargate-profile/issues), send us an [email][email] or join our [Slack Community][slack]. + +[![README Commercial Support][readme_commercial_support_img]][readme_commercial_support_link] + +## DevOps Accelerator for Startups + + +We are a [**DevOps Accelerator**][commercial_support]. We'll help you build your cloud infrastructure from the ground up so you can own it. Then we'll show you how to operate it and stick around for as long as you need us. + +[![Learn More](https://img.shields.io/badge/learn%20more-success.svg?style=for-the-badge)][commercial_support] + +Work directly with our team of DevOps experts via email, slack, and video conferencing. + +We deliver 10x the value for a fraction of the cost of a full-time engineer. Our track record is not even funny. If you want things done right and you need it done FAST, then we're your best bet. + +- **Reference Architecture.** You'll get everything you need from the ground up built using 100% infrastructure as code. +- **Release Engineering.** You'll have end-to-end CI/CD with unlimited staging environments. +- **Site Reliability Engineering.** You'll have total visibility into your apps and microservices. +- **Security Baseline.** You'll have built-in governance with accountability and audit logs for all changes. +- **GitOps.** You'll be able to operate your infrastructure via Pull Requests. +- **Training.** You'll receive hands-on training so your team can operate what we build. +- **Questions.** You'll have a direct line of communication between our teams via a Shared Slack channel. +- **Troubleshooting.** You'll get help to triage when things aren't working. +- **Code Reviews.** You'll receive constructive feedback on Pull Requests. +- **Bug Fixes.** We'll rapidly work with you to fix any bugs in our projects. + +## Slack Community + +Join our [Open Source Community][slack] on Slack. It's **FREE** for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally *sweet* infrastructure. + +## Newsletter + +Sign up for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover. + +## Office Hours + +[Join us every Wednesday via Zoom][office_hours] for our weekly "Lunch & Learn" sessions. It's **FREE** for everyone! + +[![zoom](https://img.cloudposse.com/fit-in/200x200/https://cloudposse.com/wp-content/uploads/2019/08/Powered-by-Zoom.png")][office_hours] + +## Contributing + +### Bug Reports & Feature Requests + +Please use the [issue tracker](https://github.com/cloudposse/terraform-aws-eks-fargate-profile/issues) to report any bugs or file feature requests. + +### Developing + +If you are interested in being a contributor and want to get involved in developing this project or [help out](https://cpco.io/help-out) with our other projects, we would love to hear from you! Shoot us an [email][email]. + +In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. + + 1. **Fork** the repo on GitHub + 2. **Clone** the project to your own machine + 3. **Commit** changes to your own branch + 4. **Push** your work back up to your fork + 5. Submit a **Pull Request** so that we can review your changes + +**NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request! + + +## Copyright + +Copyright © 2017-2019 [Cloud Posse, LLC](https://cpco.io/copyright) + + + +## License + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +See [LICENSE](LICENSE) for full details. + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + + + + + + + + + +## Trademarks + +All other trademarks referenced herein are the property of their respective owners. + +## About + +This project is maintained and funded by [Cloud Posse, LLC][website]. Like it? Please let us know by [leaving a testimonial][testimonial]! + +[![Cloud Posse][logo]][website] + +We're a [DevOps Professional Services][hire] company based in Los Angeles, CA. We ❤️ [Open Source Software][we_love_open_source]. + +We offer [paid support][commercial_support] on all of our projects. + +Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation. + + + +### Contributors + +| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]
[Igor Rodionov][goruha_homepage] | +|---|---|---| + + [osterman_homepage]: https://github.com/osterman + [osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png + [aknysh_homepage]: https://github.com/aknysh + [aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png + [goruha_homepage]: https://github.com/goruha + [goruha_avatar]: https://img.cloudposse.com/150x150/https://github.com/goruha.png + +[![README Footer][readme_footer_img]][readme_footer_link] +[![Beacon][beacon]][website] + + [logo]: https://cloudposse.com/logo-300x69.svg + [docs]: https://cpco.io/docs?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=docs + [website]: https://cpco.io/homepage?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=website + [github]: https://cpco.io/github?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=github + [jobs]: https://cpco.io/jobs?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=jobs + [hire]: https://cpco.io/hire?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=hire + [slack]: https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=slack + [linkedin]: https://cpco.io/linkedin?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=linkedin + [twitter]: https://cpco.io/twitter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=twitter + [testimonial]: https://cpco.io/leave-testimonial?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=testimonial + [office_hours]: https://cloudposse.com/office-hours?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=office_hours + [newsletter]: https://cpco.io/newsletter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=newsletter + [email]: https://cpco.io/email?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=email + [commercial_support]: https://cpco.io/commercial-support?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=commercial_support + [we_love_open_source]: https://cpco.io/we-love-open-source?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=we_love_open_source + [terraform_modules]: https://cpco.io/terraform-modules?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=terraform_modules + [readme_header_img]: https://cloudposse.com/readme/header/img + [readme_header_link]: https://cloudposse.com/readme/header/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=readme_header_link + [readme_footer_img]: https://cloudposse.com/readme/footer/img + [readme_footer_link]: https://cloudposse.com/readme/footer/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=readme_footer_link + [readme_commercial_support_img]: https://cloudposse.com/readme/commercial-support/img + [readme_commercial_support_link]: https://cloudposse.com/readme/commercial-support/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-eks-fargate-profile&utm_content=readme_commercial_support_link + [share_twitter]: https://twitter.com/intent/tweet/?text=terraform-aws-eks-fargate-profile&url=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [share_linkedin]: https://www.linkedin.com/shareArticle?mini=true&title=terraform-aws-eks-fargate-profile&url=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [share_reddit]: https://reddit.com/submit/?url=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [share_facebook]: https://facebook.com/sharer/sharer.php?u=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [share_googleplus]: https://plus.google.com/share?url=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [share_email]: mailto:?subject=terraform-aws-eks-fargate-profile&body=https://github.com/cloudposse/terraform-aws-eks-fargate-profile + [beacon]: https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/terraform-aws-eks-fargate-profile?pixel&cs=github&cm=readme&an=terraform-aws-eks-fargate-profile diff --git a/README.yaml b/README.yaml new file mode 100644 index 0000000..5382f05 --- /dev/null +++ b/README.yaml @@ -0,0 +1,188 @@ +--- +# +# This is the canonical configuration for the `README.md` +# Run `make readme` to rebuild the `README.md` +# + +# Name of this project +name: terraform-aws-eks-fargate-profile + +# Logo for this project +#logo: docs/logo.png + +# License of this project +license: "APACHE2" + +# Canonical GitHub repo +github_repo: cloudposse/terraform-aws-eks-fargate-profile + +# Badges to display +badges: + - name: "Codefresh Build Status" + image: "https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-aws-eks-fargate-profile?type=cf-1" + url: "https://g.codefresh.io/public/accounts/cloudposse/pipelines/5deeced5158ef2a8061fe466" + - name: "Latest Release" + image: "https://img.shields.io/github/release/cloudposse/terraform-aws-eks-fargate-profile.svg" + url: "https://github.com/cloudposse/terraform-aws-eks-fargate-profile/releases/latest" + - name: "Slack Community" + image: "https://slack.cloudposse.com/badge.svg" + url: "https://slack.cloudposse.com" + +related: + - name: "terraform-aws-eks-cluster" + description: "Terraform module to provision an EKS cluster on AWS" + url: "https://github.com/cloudposse/terraform-aws-eks-cluster" + - name: "terraform-aws-eks-node-group" + description: "Terraform module to provision an EKS Node Group" + url: "https://github.com/cloudposse/terraform-aws-eks-node-group" + - name: "terraform-aws-eks-workers" + description: "Terraform module to provision an AWS AutoScaling Group, IAM Role, and Security Group for EKS Workers" + url: "https://github.com/cloudposse/terraform-aws-eks-workers" + - name: "terraform-aws-ec2-autoscale-group" + description: "Terraform module to provision Auto Scaling Group and Launch Template on AWS" + url: "https://github.com/cloudposse/terraform-aws-ec2-autoscale-group" + - name: "terraform-aws-ecs-container-definition" + description: "Terraform module to generate well-formed JSON documents (container definitions) that are passed to the aws_ecs_task_definition Terraform resource" + url: "https://github.com/cloudposse/terraform-aws-ecs-container-definition" + - name: "terraform-aws-ecs-alb-service-task" + description: "Terraform module which implements an ECS service which exposes a web service via ALB" + url: "https://github.com/cloudposse/terraform-aws-ecs-alb-service-task" + - name: "terraform-aws-ecs-web-app" + description: "Terraform module that implements a web app on ECS and supports autoscaling, CI/CD, monitoring, ALB integration, and much more" + url: "https://github.com/cloudposse/terraform-aws-ecs-web-app" + - name: "terraform-aws-ecs-codepipeline" + description: "Terraform module for CI/CD with AWS Code Pipeline and Code Build for ECS" + url: "https://github.com/cloudposse/terraform-aws-ecs-codepipeline" + - name: "terraform-aws-ecs-cloudwatch-autoscaling" + description: "Terraform module to autoscale ECS Service based on CloudWatch metrics" + url: "https://github.com/cloudposse/terraform-aws-ecs-cloudwatch-autoscaling" + - name: "terraform-aws-ecs-cloudwatch-sns-alarms" + description: "Terraform module to create CloudWatch Alarms on ECS Service level metrics" + url: "https://github.com/cloudposse/terraform-aws-ecs-cloudwatch-sns-alarms" + - name: "terraform-aws-ec2-instance" + description: "Terraform module for providing a general purpose EC2 instance" + url: "https://github.com/cloudposse/terraform-aws-ec2-instance" + - name: "terraform-aws-ec2-instance-group" + description: "Terraform module for provisioning multiple general purpose EC2 hosts for stateful applications" + url: "https://github.com/cloudposse/terraform-aws-ec2-instance-group" + +# Short description of this project +description: |- + Terraform module to provision an EKS Node Group for [Elastic Container Service for Kubernetes](https://aws.amazon.com/eks/). + + Instantiate it multiple times to create many EKS node groups with specific settings such as GPUs, EC2 instance types, or autoscale parameters. + +introduction: |- + +# How to use this project +usage: |- + + For a complete example, see [examples/complete](examples/complete). + + For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) (which tests and deploys the example on AWS), see [test](test). + + ```hcl + provider "aws" { + region = var.region + } + + module "label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + namespace = var.namespace + name = var.name + stage = var.stage + delimiter = var.delimiter + attributes = compact(concat(var.attributes, list("cluster"))) + tags = var.tags + } + + locals { + tags = merge(module.label.tags, map("kubernetes.io/cluster/${module.label.id}", "shared")) + } + + module "vpc" { + source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.8.1" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + cidr_block = var.vpc_cidr_block + tags = local.tags + } + + module "subnets" { + source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.18.1" + availability_zones = var.availability_zones + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + vpc_id = module.vpc.vpc_id + igw_id = module.vpc.igw_id + cidr_block = module.vpc.vpc_cidr_block + nat_gateway_enabled = true + nat_instance_enabled = false + tags = local.tags + } + + module "eks_cluster" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.13.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + region = var.region + vpc_id = module.vpc.vpc_id + subnet_ids = module.subnets.public_subnet_ids + kubernetes_version = var.kubernetes_version + kubeconfig_path = var.kubeconfig_path + oidc_provider_enabled = var.oidc_provider_enabled + + workers_role_arns = [module.eks_node_group.eks_node_group_role_arn, module.eks_fargate_profile.eks_fargate_profile_role_arn] + workers_security_group_ids = [] + } + + module "eks_node_group" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-node-group.git?ref=tags/0.1.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.public_subnet_ids + instance_types = var.instance_types + desired_size = var.desired_size + min_size = var.min_size + max_size = var.max_size + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_version = var.kubernetes_version + kubernetes_labels = var.kubernetes_labels + } + + module "eks_fargate_profile" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-fargate-profile.git?ref=master" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.private_subnet_ids + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_namespace = var.kubernetes_namespace + kubernetes_labels = var.kubernetes_labels + } + ``` + +include: + - "docs/targets.md" + - "docs/terraform.md" + +# Contributors to this project +contributors: + - name: "Erik Osterman" + github: "osterman" + - name: "Andriy Knysh" + github: "aknysh" + - name: "Igor Rodionov" + github: "goruha" diff --git a/codefresh/test.yml b/codefresh/test.yml new file mode 100644 index 0000000..69fab42 --- /dev/null +++ b/codefresh/test.yml @@ -0,0 +1,77 @@ +version: '1.0' + +stages: + - Prepare + - Test + +steps: + wait: + title: Wait + stage: Prepare + image: codefresh/cli:latest + commands: + - codefresh get builds --pipeline=${{CF_REPO_NAME}} --status running --limit 1000 -o json | jq --arg id ${{CF_BUILD_ID}} -ser 'flatten|.[-1].id==$id' + retry: + maxAttempts: 10 + delay: 20 + exponentialFactor: 1.1 + + main_clone: + title: "Clone repository" + type: git-clone + stage: Prepare + description: "Initialize" + repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}} + git: CF-default + revision: ${{CF_REVISION}} + + clean_init: + title: Prepare build-harness and test-harness + image: ${{TEST_IMAGE}} + stage: Prepare + commands: + - cf_export PATH="/usr/local/terraform/0.12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + - make init + - git -C build-harness checkout master + - make -C test/ clean init TEST_HARNESS_BRANCH=master + - make -C test/src clean init + - find . -type d -name '.terraform' | xargs rm -rf + - find . -type f -name 'terraform.tfstate*' -exec rm -f {} \; + + test: + type: "parallel" + title: "Run tests" + description: "Run all tests in parallel" + stage: Test + steps: + test_readme_lint: + title: "Test README.md updated" + stage: "Test" + image: ${{TEST_IMAGE}} + description: Test "readme/lint" + commands: + - make readme/lint + + test_module: + title: Test module with bats + image: ${{TEST_IMAGE}} + stage: Test + commands: + - make -C test/ module + + test_examples_complete: + title: Test "examples/complete" with bats + image: ${{TEST_IMAGE}} + stage: Test + commands: + - make -C test/ examples/complete + + test_examples_complete_terratest: + title: Test "examples/complete" with terratest + image: ${{TEST_IMAGE}} + stage: Test + commands: + - unset AWS_PROFILE + - unset AWS_DEFAULT_PROFILE + - unset AWS_MFA_PROFILE + - make -C test/src diff --git a/docs/targets.md b/docs/targets.md new file mode 100644 index 0000000..3d4be2a --- /dev/null +++ b/docs/targets.md @@ -0,0 +1,10 @@ +## Makefile Targets +``` +Available targets: + + help Help screen + help/all Display help for all targets + help/short This help short screen + lint Lint terraform code + +``` diff --git a/docs/terraform.md b/docs/terraform.md new file mode 100644 index 0000000..c2755f9 --- /dev/null +++ b/docs/terraform.md @@ -0,0 +1,26 @@ +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| attributes | Additional attributes (e.g. `1`) | list(string) | `` | no | +| cluster_name | The name of the EKS cluster | string | - | yes | +| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no | +| enabled | Whether to create the resources. Set to `false` to prevent the module from creating any resources | bool | `true` | no | +| kubernetes_labels | Key-value mapping of Kubernetes labels for selection | map(string) | `` | no | +| kubernetes_namespace | Kubernetes namespace for selection | string | - | yes | +| name | Solution name, e.g. 'app' or 'cluster' | string | - | yes | +| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | `` | no | +| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | `` | no | +| subnet_ids | Identifiers of private EC2 Subnets to associate with the EKS Fargate Profile. These subnets must have the following resource tag: kubernetes.io/cluster/CLUSTER_NAME (where CLUSTER_NAME is replaced with the name of the EKS Cluster) | list(string) | - | yes | +| tags | Additional tags (e.g. `{ BusinessUnit = "XYZ" }` | map(string) | `` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| eks_fargate_profile_arn | Amazon Resource Name (ARN) of the EKS Fargate Profile | +| eks_fargate_profile_id | EKS Cluster name and EKS Fargate Profile name separated by a colon | +| eks_fargate_profile_role_arn | ARN of the EKS Fargate Profile IAM role | +| eks_fargate_profile_role_name | Name of the EKS Fargate Profile IAM role | +| eks_fargate_profile_status | Status of the EKS Fargate Profile | + diff --git a/examples/complete/fixtures.us-east-2.tfvars b/examples/complete/fixtures.us-east-2.tfvars new file mode 100644 index 0000000..8f62765 --- /dev/null +++ b/examples/complete/fixtures.us-east-2.tfvars @@ -0,0 +1,27 @@ +region = "us-east-2" + +availability_zones = ["us-east-2a", "us-east-2b"] + +vpc_cidr_block = "172.16.0.0/16" + +namespace = "eg" + +stage = "test" + +name = "eks-fargate" + +instance_types = ["t3.small"] + +desired_size = 2 + +max_size = 3 + +min_size = 2 + +disk_size = 20 + +kubeconfig_path = "/.kube/config" + +kubernetes_namespace = "default" + +kubernetes_labels = {} diff --git a/examples/complete/main.tf b/examples/complete/main.tf new file mode 100644 index 0000000..628fa2b --- /dev/null +++ b/examples/complete/main.tf @@ -0,0 +1,90 @@ +provider "aws" { + region = var.region +} + +module "label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + namespace = var.namespace + name = var.name + stage = var.stage + delimiter = var.delimiter + attributes = compact(concat(var.attributes, list("cluster"))) + tags = var.tags +} + +locals { + tags = merge(module.label.tags, map("kubernetes.io/cluster/${module.label.id}", "shared")) +} + +module "vpc" { + source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.8.1" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + cidr_block = var.vpc_cidr_block + tags = local.tags +} + +module "subnets" { + source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.18.1" + availability_zones = var.availability_zones + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + vpc_id = module.vpc.vpc_id + igw_id = module.vpc.igw_id + cidr_block = module.vpc.vpc_cidr_block + nat_gateway_enabled = true + nat_instance_enabled = false + tags = local.tags +} + +module "eks_cluster" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.13.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + region = var.region + vpc_id = module.vpc.vpc_id + subnet_ids = module.subnets.public_subnet_ids + kubernetes_version = var.kubernetes_version + kubeconfig_path = var.kubeconfig_path + oidc_provider_enabled = var.oidc_provider_enabled + + workers_role_arns = [module.eks_node_group.eks_node_group_role_arn, module.eks_fargate_profile.eks_fargate_profile_role_arn] + workers_security_group_ids = [] +} + +module "eks_node_group" { + source = "git::https://github.com/cloudposse/terraform-aws-eks-node-group.git?ref=tags/0.1.0" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.public_subnet_ids + instance_types = var.instance_types + desired_size = var.desired_size + min_size = var.min_size + max_size = var.max_size + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_version = var.kubernetes_version + kubernetes_labels = var.kubernetes_labels +} + +module "eks_fargate_profile" { + source = "../../" + namespace = var.namespace + stage = var.stage + name = var.name + attributes = var.attributes + tags = var.tags + subnet_ids = module.subnets.private_subnet_ids + cluster_name = module.eks_cluster.eks_cluster_id + kubernetes_namespace = var.kubernetes_namespace + kubernetes_labels = var.kubernetes_labels +} diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf new file mode 100644 index 0000000..1c453e3 --- /dev/null +++ b/examples/complete/outputs.tf @@ -0,0 +1,94 @@ +output "public_subnet_cidrs" { + value = module.subnets.public_subnet_cidrs + description = "Public subnet CIDRs" +} + +output "private_subnet_cidrs" { + value = module.subnets.private_subnet_cidrs + description = "Private subnet CIDRs" +} + +output "vpc_cidr" { + value = module.vpc.vpc_cidr_block + description = "VPC ID" +} + +output "eks_cluster_id" { + description = "The name of the EKS cluster" + value = module.eks_cluster.eks_cluster_id +} + +output "eks_cluster_arn" { + description = "The Amazon Resource Name (ARN) of the EKS cluster" + value = module.eks_cluster.eks_cluster_arn +} + +output "eks_cluster_endpoint" { + description = "The endpoint for the Kubernetes API server" + value = module.eks_cluster.eks_cluster_endpoint +} + +output "eks_cluster_version" { + description = "The Kubernetes server version of the cluster" + value = module.eks_cluster.eks_cluster_version +} + +output "eks_cluster_identity_oidc_issuer" { + description = "The OIDC Identity issuer for the cluster" + value = module.eks_cluster.eks_cluster_identity_oidc_issuer +} + +output "eks_node_group_role_arn" { + description = "ARN of the worker nodes IAM role" + value = module.eks_node_group.eks_node_group_role_arn +} + +output "eks_node_group_role_name" { + description = "Name of the worker nodes IAM role" + value = module.eks_node_group.eks_node_group_role_name +} + +output "eks_node_group_id" { + description = "EKS Cluster name and EKS Node Group name separated by a colon" + value = module.eks_node_group.eks_node_group_id +} + +output "eks_node_group_arn" { + description = "Amazon Resource Name (ARN) of the EKS Node Group" + value = module.eks_node_group.eks_node_group_arn +} + +output "eks_node_group_resources" { + description = "List of objects containing information about underlying resources of the EKS Node Group" + value = module.eks_node_group.eks_node_group_resources +} + +output "eks_node_group_status" { + description = "Status of the EKS Node Group" + value = module.eks_node_group.eks_node_group_status +} + +output "eks_fargate_profile_role_arn" { + description = "ARN of the EKS Fargate Profile IAM role" + value = module.eks_fargate_profile.eks_fargate_profile_role_arn +} + +output "eks_fargate_profile_role_name" { + description = "Name of the EKS Fargate Profile IAM role" + value = module.eks_fargate_profile.eks_fargate_profile_role_name +} + +output "eks_fargate_profile_id" { + description = "EKS Cluster name and EKS Fargate Profile name separated by a colon" + value = module.eks_fargate_profile.eks_fargate_profile_id +} + +output "eks_fargate_profile_arn" { + description = "Amazon Resource Name (ARN) of the EKS Fargate Profile" + value = module.eks_fargate_profile.eks_fargate_profile_arn +} + +output "eks_fargate_profile_status" { + description = "Status of the EKS Fargate Profile" + value = module.eks_fargate_profile.eks_fargate_profile_status +} diff --git a/examples/complete/variables.tf b/examples/complete/variables.tf new file mode 100644 index 0000000..96bb15d --- /dev/null +++ b/examples/complete/variables.tf @@ -0,0 +1,99 @@ +variable "region" { + type = string + description = "AWS Region" +} + +variable "availability_zones" { + type = list(string) + description = "List of availability zones" +} + +variable "vpc_cidr_block" { + type = string + description = "VPC CIDR block" +} + +variable "namespace" { + type = string + description = "Namespace, which could be your organization name, e.g. 'eg' or 'cp'" +} + +variable "stage" { + type = string + description = "Stage, e.g. 'prod', 'staging', 'dev' or 'testing'" +} + +variable "name" { + type = string + description = "Solution name, e.g. 'app' or 'cluster'" +} + +variable "delimiter" { + type = string + default = "-" + description = "Delimiter to be used between `name`, `namespace`, `stage`, etc." +} + +variable "attributes" { + type = list(string) + default = [] + description = "Additional attributes (e.g. `1`)" +} + +variable "tags" { + type = map(string) + default = {} + description = "Additional tags (e.g. `map('BusinessUnit`,`XYZ`)" +} + +variable "kubernetes_version" { + type = string + default = null + description = "Desired Kubernetes master version. If you do not specify a value, the latest available version is used" +} + +variable "oidc_provider_enabled" { + type = bool + default = false + description = "Create an IAM OIDC identity provider for the cluster, then you can create IAM roles to associate with a service account in the cluster, instead of using kiam or kube2iam. For more information, see https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html" +} + +variable "kubeconfig_path" { + type = string + description = "The path to `kubeconfig` file" +} + +variable "kubernetes_namespace" { + type = string + description = "Kubernetes namespace for selection" +} + +variable "kubernetes_labels" { + type = map(string) + description = "Key-value mapping of Kubernetes labels for selection" +} + +variable "desired_size" { + type = number + description = "Desired number of worker nodes" +} + +variable "max_size" { + type = number + description = "The maximum size of the AutoScaling Group" +} + +variable "min_size" { + type = number + description = "The minimum size of the AutoScaling Group" +} + +variable "disk_size" { + type = number + description = "Disk size in GiB for worker nodes. Defaults to 20. Terraform will only perform drift detection if a configuration value is provided" +} + +variable "instance_types" { + type = list(string) + description = "Set of instance types associated with the EKS Node Group. Defaults to [\"t3.medium\"]. Terraform will only perform drift detection if a configuration value is provided" +} diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf new file mode 100644 index 0000000..9840ed7 --- /dev/null +++ b/examples/complete/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 0.12.0" + + required_providers { + aws = "~> 2.0" + template = "~> 2.0" + null = "~> 2.0" + local = "~> 1.3" + } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..67293db --- /dev/null +++ b/main.tf @@ -0,0 +1,60 @@ +locals { + tags = merge( + var.tags, + { + "kubernetes.io/cluster/${var.cluster_name}" = "owned" + } + ) +} + +module "label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + namespace = var.namespace + stage = var.stage + name = var.name + delimiter = var.delimiter + attributes = compact(concat(var.attributes, ["fargate"])) + tags = local.tags + enabled = var.enabled +} + +data "aws_iam_policy_document" "assume_role" { + count = var.enabled ? 1 : 0 + + statement { + effect = "Allow" + actions = ["sts:AssumeRole"] + + principals { + type = "Service" + identifiers = ["eks-fargate-pods.amazonaws.com"] + } + } +} + +resource "aws_iam_role" "default" { + count = var.enabled ? 1 : 0 + name = module.label.id + assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json) + tags = module.label.tags +} + +resource "aws_iam_role_policy_attachment" "amazon_eks_fargate_pod_execution_role_policy" { + count = var.enabled ? 1 : 0 + policy_arn = "arn:aws:iam::aws:policy/AmazonEKSFargatePodExecutionRolePolicy" + role = join("", aws_iam_role.default.*.name) +} + +resource "aws_eks_fargate_profile" "default" { + count = var.enabled ? 1 : 0 + cluster_name = var.cluster_name + fargate_profile_name = module.label.id + pod_execution_role_arn = join("", aws_iam_role.default.*.arn) + subnet_ids = var.subnet_ids + tags = module.label.tags + + selector { + namespace = var.kubernetes_namespace + labels = var.kubernetes_labels + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..89fd810 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,24 @@ +output "eks_fargate_profile_role_arn" { + description = "ARN of the EKS Fargate Profile IAM role" + value = join("", aws_iam_role.default.*.arn) +} + +output "eks_fargate_profile_role_name" { + description = "Name of the EKS Fargate Profile IAM role" + value = join("", aws_iam_role.default.*.name) +} + +output "eks_fargate_profile_id" { + description = "EKS Cluster name and EKS Fargate Profile name separated by a colon" + value = join("", aws_eks_fargate_profile.default.*.id) +} + +output "eks_fargate_profile_arn" { + description = "Amazon Resource Name (ARN) of the EKS Fargate Profile" + value = join("", aws_eks_fargate_profile.default.*.arn) +} + +output "eks_fargate_profile_status" { + description = "Status of the EKS Fargate Profile" + value = join("", aws_eks_fargate_profile.default.*.status) +} diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..442804a --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +.test-harness diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..17b2fe7 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,43 @@ +TEST_HARNESS ?= https://github.com/cloudposse/test-harness.git +TEST_HARNESS_BRANCH ?= master +TEST_HARNESS_PATH = $(realpath .test-harness) +BATS_ARGS ?= --tap +BATS_LOG ?= test.log + +# Define a macro to run the tests +define RUN_TESTS +@echo "Running tests in $(1)" +@cd $(1) && bats $(BATS_ARGS) $(addsuffix .bats,$(addprefix $(TEST_HARNESS_PATH)/test/terraform/,$(TESTS))) +endef + +default: all + +-include Makefile.* + +## Provision the test-harnesss +.test-harness: + [ -d $@ ] || git clone --depth=1 -b $(TEST_HARNESS_BRANCH) $(TEST_HARNESS) $@ + +## Initialize the tests +init: .test-harness + +## Install all dependencies (OS specific) +deps:: + @exit 0 + +## Clean up the test harness +clean: + [ "$(TEST_HARNESS_PATH)" == "/" ] || rm -rf $(TEST_HARNESS_PATH) + +## Run all tests +all: module examples/complete + +## Run basic sanity checks against the module itself +module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions +module: deps + $(call RUN_TESTS, ../) + +## Run tests against example +examples/complete: export TESTS ?= installed lint get-modules get-plugins validate +examples/complete: deps + $(call RUN_TESTS, ../$@) diff --git a/test/Makefile.alpine b/test/Makefile.alpine new file mode 100644 index 0000000..7925b18 --- /dev/null +++ b/test/Makefile.alpine @@ -0,0 +1,5 @@ +ifneq (,$(wildcard /sbin/apk)) +## Install all dependencies for alpine +deps:: init + @apk add --update terraform-docs@cloudposse json2hcl@cloudposse +endif diff --git a/test/src/.gitignore b/test/src/.gitignore new file mode 100644 index 0000000..31b0219 --- /dev/null +++ b/test/src/.gitignore @@ -0,0 +1,2 @@ +.gopath +vendor/ diff --git a/test/src/Gopkg.lock b/test/src/Gopkg.lock new file mode 100644 index 0000000..87bb6bd --- /dev/null +++ b/test/src/Gopkg.lock @@ -0,0 +1,92 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec" + name = "github.com/davecgh/go-spew" + packages = ["spew"] + pruneopts = "UT" + revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" + version = "v1.1.1" + +[[projects]] + digest = "1:75d6042fc66aebc974cc49b0c6c7cc3b9adb5f8130fbfa0dbec0820d990afa25" + name = "github.com/gruntwork-io/terratest" + packages = [ + "modules/collections", + "modules/customerrors", + "modules/files", + "modules/logger", + "modules/retry", + "modules/shell", + "modules/ssh", + "modules/terraform", + ] + pruneopts = "UT" + revision = "892abb2c35878d0808101bbfe6559e931dc2d354" + version = "v0.16.0" + +[[projects]] + digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + pruneopts = "UT" + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + digest = "1:5da8ce674952566deae4dbc23d07c85caafc6cfa815b0b3e03e41979cedb8750" + name = "github.com/stretchr/testify" + packages = [ + "assert", + "require", + ] + pruneopts = "UT" + revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" + version = "v1.3.0" + +[[projects]] + branch = "master" + digest = "1:831470c2758c8b733941144f2803a0ccad0632c5a767415b777ebd296b5f463e" + name = "golang.org/x/crypto" + packages = [ + "curve25519", + "ed25519", + "ed25519/internal/edwards25519", + "internal/chacha20", + "internal/subtle", + "poly1305", + "ssh", + "ssh/agent", + ] + pruneopts = "UT" + revision = "22d7a77e9e5f409e934ed268692e56707cd169e5" + +[[projects]] + branch = "master" + digest = "1:76ee51c3f468493aff39dbacc401e8831fbb765104cbf613b89bef01cf4bad70" + name = "golang.org/x/net" + packages = ["context"] + pruneopts = "UT" + revision = "f3200d17e092c607f615320ecaad13d87ad9a2b3" + +[[projects]] + branch = "master" + digest = "1:181f3fd33e620b958b5ab77da177cf775cdcccd7db82963607875fbd09ae995e" + name = "golang.org/x/sys" + packages = [ + "cpu", + "unix", + ] + pruneopts = "UT" + revision = "9cd6430ef91e39e1a0ec0470cf1321a33ef1b887" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = [ + "github.com/gruntwork-io/terratest/modules/terraform", + "github.com/stretchr/testify/assert", + ] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/test/src/Gopkg.toml b/test/src/Gopkg.toml new file mode 100644 index 0000000..995bac5 --- /dev/null +++ b/test/src/Gopkg.toml @@ -0,0 +1,7 @@ +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.2.2" + +[prune] + go-tests = true + unused-packages = true diff --git a/test/src/Makefile b/test/src/Makefile new file mode 100644 index 0000000..5709214 --- /dev/null +++ b/test/src/Makefile @@ -0,0 +1,50 @@ +PACKAGE = terraform-aws-eks-fargate-profile +GOEXE ?= /usr/bin/go +GOPATH = $(CURDIR)/.gopath +GOBIN = $(GOPATH)/bin +BASE = $(GOPATH)/src/$(PACKAGE) +PATH := $(PATH):$(GOBIN) + +export TF_DATA_DIR ?= $(CURDIR)/.terraform +export TF_CLI_ARGS_init ?= -get-plugins=true +export GOPATH + +.PHONY: all +## Default target +all: test + +ifneq (,$(wildcard /sbin/apk)) +## Install go, if not installed +$(GOEXE): + apk add --update go +endif + +ifeq ($(shell uname -s),Linux) +## Install all `dep`, if not installed +$(GOBIN)/dep: + @mkdir -p $(GOBIN) + @curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh +endif + +## Prepare the GOPATH +$(BASE): $(GOEXE) + @mkdir -p $(dir $@) + @ln -sf $(CURDIR) $@ + +## Download vendor dependencies to vendor/ +$(BASE)/vendor: $(BASE) $(GOBIN)/dep + cd $(BASE) && dep ensure + +.PHONY : init +## Initialize tests +init: $(BASE)/vendor + +.PHONY : test +## Run tests +test: init + cd $(BASE) && go test -v -timeout 90m -run TestExamplesComplete + +.PHONY : clean +## Clean up files +clean: + rm -rf .gopath/ vendor/ $(TF_DATA_DIR) diff --git a/test/src/examples_complete_test.go b/test/src/examples_complete_test.go new file mode 100644 index 0000000..6e17367 --- /dev/null +++ b/test/src/examples_complete_test.go @@ -0,0 +1,198 @@ +package test + +import ( + "fmt" + "k8s.io/apimachinery/pkg/util/runtime" + "sync/atomic" + "testing" + "time" + + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/assert" + appsv1 "k8s.io/api/apps/v1" + apiv1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/clientcmd" +) + +func int32Ptr(i int32) *int32 { return &i } + +// Test the Terraform module in examples/complete using Terratest. +func TestExamplesComplete(t *testing.T) { + t.Parallel() + + terraformOptions := &terraform.Options{ + // The path to where our Terraform code is located + TerraformDir: "../../examples/complete", + Upgrade: true, + // Variables to pass to our Terraform code using -var-file options + VarFiles: []string{"fixtures.us-east-2.tfvars"}, + } + + // At the end of the test, run `terraform destroy` to clean up any resources that were created + defer terraform.Destroy(t, terraformOptions) + + // If Go runtime crushes, run `terraform destroy` to clean up any resources that were created + defer runtime.HandleCrash(func(i interface{}) { + terraform.Destroy(t, terraformOptions) + }) + + // This will run `terraform init` and `terraform apply` and fail the test if there are any errors + terraform.InitAndApply(t, terraformOptions) + + // Run `terraform output` to get the value of an output variable + vpcCidr := terraform.Output(t, terraformOptions, "vpc_cidr") + // Verify we're getting back the outputs we expect + assert.Equal(t, "172.16.0.0/16", vpcCidr) + + // Run `terraform output` to get the value of an output variable + privateSubnetCidrs := terraform.OutputList(t, terraformOptions, "private_subnet_cidrs") + // Verify we're getting back the outputs we expect + assert.Equal(t, []string{"172.16.0.0/19", "172.16.32.0/19"}, privateSubnetCidrs) + + // Run `terraform output` to get the value of an output variable + publicSubnetCidrs := terraform.OutputList(t, terraformOptions, "public_subnet_cidrs") + // Verify we're getting back the outputs we expect + assert.Equal(t, []string{"172.16.96.0/19", "172.16.128.0/19"}, publicSubnetCidrs) + + // Run `terraform output` to get the value of an output variable + eksClusterId := terraform.Output(t, terraformOptions, "eks_cluster_id") + // Verify we're getting back the outputs we expect + assert.Equal(t, "eg-test-eks-fargate-cluster", eksClusterId) + + // Run `terraform output` to get the value of an output variable + eksNodeGroupId := terraform.Output(t, terraformOptions, "eks_node_group_id") + // Verify we're getting back the outputs we expect + assert.Equal(t, "eg-test-eks-fargate-cluster:eg-test-eks-fargate-workers", eksNodeGroupId) + + // Run `terraform output` to get the value of an output variable + eksNodeGroupRoleName := terraform.Output(t, terraformOptions, "eks_node_group_role_name") + // Verify we're getting back the outputs we expect + assert.Equal(t, "eg-test-eks-fargate-workers", eksNodeGroupRoleName) + + // Run `terraform output` to get the value of an output variable + eksNodeGroupStatus := terraform.Output(t, terraformOptions, "eks_node_group_status") + // Verify we're getting back the outputs we expect + assert.Equal(t, "ACTIVE", eksNodeGroupStatus) + + // Run `terraform output` to get the value of an output variable + eksFargateProfileId := terraform.Output(t, terraformOptions, "eks_fargate_profile_id") + // Verify we're getting back the outputs we expect + assert.Equal(t, "eg-test-eks-fargate-cluster:eg-test-eks-fargate-fargate", eksFargateProfileId) + + // Run `terraform output` to get the value of an output variable + eksFargateProfileRoleName := terraform.Output(t, terraformOptions, "eks_fargate_profile_role_name") + // Verify we're getting back the outputs we expect + assert.Equal(t, "eg-test-eks-fargate-fargate", eksFargateProfileRoleName) + + // Run `terraform output` to get the value of an output variable + eksFargateProfileStatus := terraform.Output(t, terraformOptions, "eks_fargate_profile_status") + // Verify we're getting back the outputs we expect + assert.Equal(t, "ACTIVE", eksFargateProfileStatus) + + // Wait for all nodes (two from the Node Group and one Fargate) to join the cluster + // https://github.com/kubernetes/client-go + // https://www.rushtehrani.com/post/using-kubernetes-api + // https://rancher.com/using-kubernetes-api-go-kubecon-2017-session-recap + // https://gianarb.it/blog/kubernetes-shared-informer + // https://medium.com/@muhammet.arslan/write-your-own-kubernetes-controller-with-informers-9920e8ab6f84 + kubeconfigPath := "/.kube/config" + config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) + assert.NoError(t, err) + clientset, err := kubernetes.NewForConfig(config) + assert.NoError(t, err) + + // Created Kubernetes deployment in the `default` namespace (for which we create a Fargate Profile) + // https://github.com/kubernetes/client-go/blob/master/examples/create-update-delete-deployment/main.go + fmt.Println("Creating Kubernetes deployment 'demo-deployment' in the 'default' namespace...") + deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault) + + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: apiv1.NamespaceDefault, + Name: "demo-deployment", + }, + Spec: appsv1.DeploymentSpec{ + Replicas: int32Ptr(1), + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "demo", + }, + }, + Template: apiv1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "app": "demo", + }, + }, + Spec: apiv1.PodSpec{ + Containers: []apiv1.Container{ + { + Name: "web", + Image: "nginx:1.17", + Ports: []apiv1.ContainerPort{ + { + Name: "http", + Protocol: apiv1.ProtocolTCP, + ContainerPort: 80, + }, + }, + }, + }, + }, + }, + }, + } + + result, err := deploymentsClient.Create(deployment) + assert.NoError(t, err) + fmt.Printf("Created Kubernetes deployment '%q'\n", result.GetObjectMeta().GetName()) + + fmt.Println("Waiting for worker nodes to join the EKS cluster...") + factory := informers.NewSharedInformerFactory(clientset, 0) + informer := factory.Core().V1().Nodes().Informer() + stopChannel := make(chan struct{}) + var countOfWorkerNodes uint64 = 0 + + informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + node := obj.(*corev1.Node) + fmt.Printf("Node %s has joined the EKS cluster at %s\n", node.Name, node.CreationTimestamp) + atomic.AddUint64(&countOfWorkerNodes, 1) + if countOfWorkerNodes == 3 { + close(stopChannel) + } + }, + }) + + go informer.Run(stopChannel) + + select { + case <-stopChannel: + fmt.Println("All nodes have joined the EKS cluster") + fmt.Printf("Listing deployments in namespace '%q':\n", apiv1.NamespaceDefault) + list, err := deploymentsClient.List(metav1.ListOptions{}) + assert.NoError(t, err) + + for _, d := range list.Items { + fmt.Printf("* Deployment '%s' has %d replica(s)\n", d.Name, *d.Spec.Replicas) + } + + fmt.Println("Deleting deployment 'demo-deployment' ...") + deletePolicy := metav1.DeletePropagationForeground + err = deploymentsClient.Delete("demo-deployment", &metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }) + assert.NoError(t, err) + fmt.Println("Deleted deployment 'demo-deployment'") + + case <-time.After(8 * time.Minute): + msg := "NOT all nodes have joined the EKS cluster" + fmt.Println(msg) + assert.Fail(t, msg) + } +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..fa2344e --- /dev/null +++ b/variables.tf @@ -0,0 +1,61 @@ +variable "namespace" { + type = string + description = "Namespace, which could be your organization name, e.g. 'eg' or 'cp'" + default = "" +} + +variable "stage" { + type = string + description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'" + default = "" +} + +variable "name" { + type = string + description = "Solution name, e.g. 'app' or 'cluster'" +} + +variable "delimiter" { + type = string + default = "-" + description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`" +} + +variable "attributes" { + type = list(string) + default = [] + description = "Additional attributes (e.g. `1`)" +} + +variable "tags" { + type = map(string) + default = {} + description = "Additional tags (e.g. `{ BusinessUnit = \"XYZ\" }`" +} + +variable "enabled" { + type = bool + description = "Whether to create the resources. Set to `false` to prevent the module from creating any resources" + default = true +} + +variable "cluster_name" { + type = string + description = "The name of the EKS cluster" +} + +variable "subnet_ids" { + description = "Identifiers of private EC2 Subnets to associate with the EKS Fargate Profile. These subnets must have the following resource tag: kubernetes.io/cluster/CLUSTER_NAME (where CLUSTER_NAME is replaced with the name of the EKS Cluster)" + type = list(string) +} + +variable "kubernetes_namespace" { + type = string + description = "Kubernetes namespace for selection" +} + +variable "kubernetes_labels" { + type = map(string) + description = "Key-value mapping of Kubernetes labels for selection" + default = {} +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..1db4048 --- /dev/null +++ b/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = "~> 0.12.0" + + required_providers { + aws = "~> 2.0" + template = "~> 2.0" + local = "~> 1.3" + } +}