├── .github ├── CODE_OF_CONDUCT.md └── CONTRIBUTING.md ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── assets └── public-api-distributed-model.png ├── examples ├── terraform │ ├── README.md │ ├── api_policy.json.tpl │ ├── log_format.json.tpl │ ├── main.tf │ └── openapi.yaml └── terragrunt │ ├── README.md │ ├── api_policy.json.tpl │ ├── log_format.json.tpl │ ├── openapi.yaml │ └── terragrunt.hcl ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at cloudarmy.io@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 49 | 50 | [homepage]: https://www.contributor-covenant.org 51 | 52 | For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq 53 | 54 | 55 | ## Licensing 56 | 57 | See the LICENSE file for our project's licensing. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local VSCode settings 2 | .vscode/* 3 | 4 | # Local .terraform directories 5 | **/.terraform/* 6 | **/data/* 7 | 8 | # .tfstate files 9 | *.tfstate 10 | *.tfstate.* 11 | 12 | # Terragrunt Files 13 | **/.terragrunt-cache 14 | **/hcl.swp 15 | backend.tf 16 | provider.tf 17 | 18 | # Crash log files 19 | crash.log 20 | 21 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 22 | # .tfvars files are managed as part of configuration and so should be included in 23 | # version control. 24 | # 25 | # example.tfvars 26 | 27 | # Unencrypted Secrets 28 | **/*decrypted*.yaml 29 | 30 | # Ignore override files as they are usually used to override resources locally and so 31 | # are not checked in 32 | override.tf 33 | override.tf.json 34 | *_override.tf 35 | *_override.tf.json 36 | 37 | # Include override files you do wish to add to version control using negated pattern 38 | # 39 | # !example_override.tf 40 | 41 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 42 | # example: *tfplan* 43 | 44 | # Kube Control 45 | kubeconfig 46 | 47 | # OSx Files 48 | **/*.DS_Store* 49 | 50 | # IntelliJ Files 51 | **/*.idea* 52 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.4.0 (May 19, 2025) 2 | 3 | FEATURE: 4 | * Add base path mapping attribute for `aws_api_gateway_base_path_mapping` resource. 5 | * Add support for existing domains 6 | 7 | CHORE: 8 | * Cleanup comments in main.tf 9 | * Reorganize base_path_mapping resources for readability. 10 | 11 | ## 1.3.0 (April 10, 2025) 12 | 13 | FEATURE: 14 | * Supports many method settings & paths for each stage 15 | * Supports models 16 | * Add output for API Gateway domain name 17 | 18 | CHORE: 19 | * Add tags to the API 20 | * Remove method path variable 21 | * Fix existing errors in examples & update examples 22 | 23 | BUG: 24 | * Limit hashicorp/aws provider to `5.75.0` due to breaking changes using `5.76.0` 25 | * Error found for aws_api_gateway_stage: The argument "deployment_id" is required, but no definition was found. 26 | 27 | ## 1.2.0 (November 17, 2023) 28 | 29 | FEATURE: 30 | * Supports custom domains for REGIONAL & EDGE endpoints 31 | * Supports optional resource policies 32 | 33 | ## 1.1.0 (November 10, 2023) 34 | 35 | BUG: 36 | * Fixed issue invalid_index error for new deploys 37 | 38 | ## 1.0.8 (July 20, 2023) 39 | 40 | CHORE: 41 | * Added sample architecture diagram for distributed model 42 | 43 | ## 1.3.0 (November 20, 2022) 44 | 45 | BUG: 46 | * Fixed issue where canary couldn't be destroyed after being created. 47 | * Now we create the stage before it's destroyed. 48 | 49 | FEATURE: 50 | * Added "Managed by Terraform" description to deployments. 51 | * Append "Deployed on {timestamp}" to stage descriptions. 52 | 53 | CHORE: 54 | * Improved notes for the `aws_api_gateway_deployment` resource. 55 | * Updated README about `put_rest_api_mode` usage & known issues. 56 | 57 | ## 1.0.6 (November 1, 2022) 58 | 59 | CHORE: 60 | * Removed Beta-Limited Support header from readme. 61 | 62 | ## 1.0.5 (November 1, 2022) 63 | 64 | BUG: 65 | * Added option to disable canary given the current open issue. 66 | * Fixed usage plans and api key variables so the maps can be ommitted from input blocks if set to false. 67 | 68 | CHORE: 69 | * Updated output descriptions for WAF and stage 70 | * Added examples for terraform and improved existing examples 71 | 72 | ## 1.0.4 (October 7, 2022) 73 | 74 | BUG: 75 | 76 | * Fixed race condition with deployment vs stage under certain conditions. 77 | 78 | ## 1.0.3 (September 22, 2022) 79 | 80 | CHORE: 81 | 82 | * Added all inputs/outputs in README 83 | * Set deployment to redeploy on timestamp 84 | * Removed unused variables 85 | * Updated example 86 | 87 | ## 1.0.2 (September 21, 2022) 88 | 89 | CHORE: 90 | 91 | * Added support document links 92 | * Added special notes for merge mode 93 | * Expanded documentation on OpenAPI spec 94 | * Improved example 95 | 96 | ## 1.0.1 (September 19, 2022) 97 | 98 | CHORE: 99 | 100 | * Improved README 101 | * Improved example 102 | 103 | ## 1.0.0 (September 17, 2022) 104 | 105 | INITIAL: 106 | 107 | * Initial module creation 108 | * Updated README 109 | 110 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md) 2 | 3 | ![Terraform](https://cloudarmy.io/tldr/images/tf_aws.jpg) 4 |
5 |
6 |
7 |
8 | ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/adamwshero/terraform-aws-api-gateway?color=lightgreen&label=latest%20tag%3A&style=for-the-badge) 9 |
10 |
11 | # terraform-aws-api-gateway (V1) 12 | 13 | 14 | Terraform module to create [Amazon API Gateway (v1)](https://aws.amazon.com/api-gateway/) resources. 15 | 16 | Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the "front door" for applications to access data, business logic, or functionality from your backend services. Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications. 17 |
18 | 19 | Build RESTful APIs optimized for serverless workloads and HTTP backends using HTTP APIs. HTTP APIs are the best choice for building APIs that only require API proxy functionality. If your APIs require API proxy functionality and API management features in a single solution, API Gateway also offers REST APIs. 20 |
21 | 22 | ## Module Capabilities 23 | * Uses OpenAPI 3.x Specification 24 | * Deploy REST API to many stages 25 | * Supports many method settings & paths for each stage 26 | * Supports models 27 | * Supports creation of many API Keys 28 | * Supports creation of many stages 29 | * Supports stage canaries. 30 | * Supports creation of domain name and its mapping 31 | * Supports assigning many API Keys to many usage plans 32 | * Supports assigning many usage plans to many stages 33 | * Supports WAF integration for stages 34 | * Supports VPC Link for `EDGE` & `REGIONAL` type API's 35 | * Supports VPC Endpoints for `PRIVATE` type API's 36 |
37 | 38 | ## Distributed Architecture Example Using PrivateLink (Preferred for public API's) 39 | ![](assets/public-api-distributed-model.png) 40 | 41 | ## Distributed Architecture Example Without PrivateLink (Preferred for private API's) 42 | - COMING SOON... 43 | 44 | 45 | ## Assumptions 46 | * Public API Scenario 47 | * You already have Network Load Balancer (NLB) with an IP type target group created if you are creating an API using the `regional` or `edge` deployment type. 48 | * You already have VPC Link setup and configured to point to your internal Network Load Balancer (NLB) if you are creating an API using the `regional` or `edge` deployment type. 49 | * You have already configured a VPC endpoint(s) that your NLB is using as targets if you are creating an API using the `regional` or `edge` deployment type. That VPC endpoint is connected to a VPC Endpoint Service in the same account or another account. (see architecture diagram) 50 | * All Scenarios 51 | * You already have created the CloudWatch Log Group for your access logging. This is different from execution log groups which is created automatically by API Gateway and is not manageable by Terraform. 52 | * You already have created the IAM role and policy for API Gateway execution. This role is needed so that it can create the CloudWatch Log Group and push log streams to it. 53 |
54 | 55 | ## Usage 56 | * The definition of the API is managed using the OpenAPI 3.x standard and is contained in a openapi.yaml file. This file contains the paths, authorization integrations, VPC Link integrations, responses, authorizers, etc. The goal is to configure as much of the API in this .yaml as possible and use Terragrunt to manage the broader context of API Gateway. This approach also reduces greatly the amount of Terraform resources that need to be created/managed. 57 | * The resources abstracted from Terraform and into OpenAPI spec are listed below. Because of this, there are no inputs in Terraform for the REST API itself except for the `name`, `description`, `body`, and `put_rest_api_mode`. To provide inputs for these resources, you must do so in the openapi.yaml file using the [AWS OpenAPI Extensions from the developer guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html). 58 | ``` 59 | aws_api_gateway_resource 60 | aws_api_gateway_method 61 | aws_api_gateway_method_response 62 | aws_api_gateway_method_settings 63 | aws_api_gateway_integration 64 | aws_api_gateway_integration_response 65 | aws_api_gateway_gateway_response 66 | aws_api_gateway_model 67 | ``` 68 | 69 | * The API is deployed every time a change is made in Terraform. This is done to ensure that the deploy being used matches with the most recent deploy. Otherwise, this can be unexpectedly out-of-sync. 70 | * This module was initially built to suit a public REST API over Private Link and as such, depended on a Network Load Balancer (NLB) to function with VPC Link. You can choose to remove this dependency for private API's not using the `regional` or `edge` deployment type. 71 |
72 | 73 | ## Special Notes 74 | * (Merge Mode) 75 | * When importing Open API Specifications with the `body` argument, by default the API Gateway REST API will be replaced with the Open API Specification thus removing any existing methods, resources, integrations, or endpoints. Endpoint mutations are asynchronous operations, and race conditions with DNS are possible. To overcome this limitation, use the `put_rest_api_mode` attribute and set it to `merge`. 76 | * Using `put_rest_api_mode` = `merge` when importing the OpenAPI Specification, the AWS control plane WILL NOT delete all existing literal properties that are not explicitly set in the OpenAPI definition. Impacted API Gateway properties: ApiKeySourceType, BinaryMediaTypes, Description, EndpointConfiguration, MinimumCompressionSize, Name, Policy). 77 | * When using `put_rest_api_mode` = `merge`, and you rename/remove a resource in the body, that resource change WILL NOT be reflected in the console. 78 | * When using `put_rest_api_mode` = `overwrite`, the AWS APIGW console reflects all changes you make accurately. However, be aware of the warning issued by the provider about using `overwrite` mode. We have not experienced issues using it so far with this module and implementation but in tests, we have only toggled this mode when we are renaming resource paths or removing resources from the body altogether. 79 | * (PRIVATE Type API Endpoint) 80 | * When a REST API type of `PRIVATE` is needed, the VPC endpoints must be specified in the `openapi.yaml` definition. This is especially called out since it is not obvious as most modules allow setting this in the Terraform input block. 81 | ``` 82 | x-amazon-apigateway-endpoint-configuration: 83 | vpcEndpointIds: [""] 84 | disableExecuteApiEndpoint: true 85 | ``` 86 | * (Deployments History vs. Deployment being used) 87 | * We deploy every time using `(timestamp()}` in the `aws_api_gateway_deployment` resource. If we do not, sometimes the 88 | deployment history has new deployments but the actual deployment in-use by the stage might be an older one. 89 | * Currently there is almost always one deployment, which is the most recent one. When there are multiple deployments in history, only the most recent will be used by default. 90 | * Domain names supports only one ACM Certificate for all the domain names, so the certificate must be valid for all the chosen domain names. 91 | * (Using Existing Domains) 92 | * In the case where you need to use an existing domain (e.g. `create_api_domain_name = false`), ensure that both `domain_names` and `domain_base_path` are set to the desired values. 93 |
94 | 95 | ## Upcoming/Recent Improvements 96 | * Want `aws_api_gateway_method_settings` to allow us to apply different method settings by stage and by method instead of choosing between the full override `*/*` or only a single method to manage (e.g. `{resource_path}/{http_method}`). Currently whatever the path is dictates all method settings for the stages that have been deployed. Method settings would be represented as a `map` just as we already do with api keys and usage plans. 97 | * Fixed in 1.3.0 release 98 | * Want a usage_plan to accept many API keys as a `list(string)`. Currenly a usage plan has a 1:1 relationship with API keys. This should be expanded so that many API keys can be associated with a single usage plan in the event multiple external consumers have similar API needs. This will reduce the number of usage plans needed. 99 | * Want the ability to create/enable VPC Link in this module since we're already consuming the Network Load Balancer (NLB) outputs when we are using the `regional` or `edge` deployment type. 100 | * [Works well with our VPC Link module](https://registry.terraform.io/modules/adamwshero/api-gateway-vpc-link/aws/latest) 101 | 102 | ## The More You Know 103 | * CloudWatch Alarms 104 | * For CloudWatch Cache Hit/Miss alarms to work, you must enable the cache cluster for the stage. 105 | * NLB Health Checks 106 | * Ensure you are using the same availability zones from your NLB all the way to the target ALB where your service is running. Otherwise, you will see NLB targets (which are VPC endpint IP's) that are in an unhealthy state. 107 | * NLB Target Groups 108 | * At the time of this writing, there is an open issue for NLB's specifically where you will only be able to have 1 target group for a listener in Terraform (e.g. 443). Because of this, we must deploy an NLB, & VPCLink for each API instead of having 1 NLB and many target groups for each API. [See this issue opened for the AWS CDK (is not a CDK issue alone)](https://github.com/aws/aws-cdk/issues/11943) 109 | * VPCLink + Private Link 110 | * In the case where we are using private link + VPC Link to connect a REST API to a target service, it is important to know that the integration URI needed for the API integration request needs to match the domain/cert that is being used at the ALB level closest to the target service. Do not point the integration URI to the network load balancer. If the ALB listener for the application has a cert attached for `*.nonprod.mycompany.com`, the integration URI for the API needs to also use that domain `(e.g. my-app.nonprod.mycompany.com)` to avoid a cert mismatch error. 111 |
112 | 113 | ### Terraform Basic Example 114 | ``` 115 | module "rest-api" { 116 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 117 | 118 | 119 | inputs = { 120 | api_name = "my-app-dev" 121 | description = "Development API for the My App service." 122 | endpoint_type = "REGIONAL" 123 | put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 124 | 125 | // API Definition & Vars 126 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 127 | { 128 | endpoint_uri = "https://my-app.nonprod.company.com}/my_app_path" 129 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 130 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 131 | } 132 | ) 133 | 134 | // Stage Settings 135 | stage_names = ["dev"] 136 | stage_description = "Development stage for My App API" 137 | log_group_name = "/aws/apigateway/access/my_app/dev" 138 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 139 | 140 | // Method Settings 141 | method_settings = { 142 | "dev /*/*" = { 143 | metrics_enabled = true 144 | logging_level = "INFO" 145 | data_trace_enabled = true 146 | throttling_burst_limit = 100 147 | throttling_rate_limit = 100 148 | caching_enabled = true 149 | cache_ttl_in_seconds = 300 150 | cache_data_encrypted = true 151 | require_authorization_for_cache_control = true 152 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 153 | } 154 | } 155 | 156 | // Security 157 | enable_waf = false 158 | 159 | // Execution Role 160 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 161 | cloudwatch_policy_name = "my-app-dev" 162 | 163 | // Usage Plans & API Keys 164 | create_usage_plan = false 165 | enable_api_key = false 166 | 167 | tags = local.tags 168 | } 169 | ``` 170 | ### Terragrunt Basic Example 171 | ``` 172 | terraform { 173 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 174 | } 175 | 176 | inputs = { 177 | api_name = "my-app-dev" 178 | description = "Development API for the My App service." 179 | endpoint_type = "REGIONAL" 180 | put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 181 | 182 | // API Definition & Vars 183 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 184 | { 185 | endpoint_uri = "https://my-app.nonprod.company.com}/my_app_path" 186 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 187 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 188 | } 189 | ) 190 | 191 | // Stage Settings 192 | stage_names = ["dev"] 193 | stage_description = "Development stage for My App API" 194 | log_group_name = "/aws/apigateway/access/my_app/dev" 195 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 196 | 197 | // Method Settings 198 | method_settings = { 199 | "dev /*/*" = { 200 | metrics_enabled = true 201 | logging_level = "INFO" 202 | data_trace_enabled = true 203 | throttling_burst_limit = 100 204 | throttling_rate_limit = 100 205 | caching_enabled = true 206 | cache_ttl_in_seconds = 300 207 | cache_data_encrypted = true 208 | require_authorization_for_cache_control = true 209 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 210 | } 211 | } 212 | 213 | // Security 214 | enable_waf = false 215 | 216 | // Execution Role 217 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 218 | cloudwatch_policy_name = "my-app-dev" 219 | 220 | // Usage Plans & API Keys 221 | create_usage_plan = false 222 | enable_api_key = false 223 | 224 | tags = local.tags 225 | } 226 | ``` 227 | 228 | ## Supporting Articles & Documentation 229 | - Working with AWS API Gateway Extensions to OpenAPI 230 | - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html 231 | - AWS API Gateway Dimensions & Metrics 232 | - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-metrics-and-dimensions.html 233 | - OpenAPI Specification 234 | - https://github.com/OAI/OpenAPI-Specification 235 | - https://spec.openapis.org/oas/v3.1.0#openapi-specification 236 | - https://swagger.io/docs/specification/about/ 237 | - IAM Role & Policy requirements for CloudWatch Logging 238 | - https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html 239 | 240 | 241 | 243 | ## Requirements 244 | 245 | | Name | Version | 246 | |------|---------| 247 | | [terraform](#requirement\_terraform) | >= 0.13.1 | 248 | | [aws](#requirement\_aws) | >= 5.5.0, < 5.75.0 | 249 | 250 | ## Providers 251 | 252 | | Name | Version | 253 | |------|---------| 254 | | [aws](#provider\_aws) | >= 5.5.0, < 5.75.0 | 255 | 256 | ## Modules 257 | 258 | No modules. 259 | 260 | ## Resources 261 | 262 | | Name | Type | 263 | |------|------| 264 | | [aws_api_gateway_account.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_account) | resource | 265 | | [aws_api_gateway_api_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource | 266 | | [aws_api_gateway_base_path_mapping.edge_acm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_base_path_mapping) | resource | 267 | | [aws_api_gateway_base_path_mapping.edge_iam](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_base_path_mapping) | resource | 268 | | [aws_api_gateway_base_path_mapping.existing](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_base_path_mapping) | resource | 269 | | [aws_api_gateway_base_path_mapping.regional_acm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_base_path_mapping) | resource | 270 | | [aws_api_gateway_base_path_mapping.regional_iam](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_base_path_mapping) | resource | 271 | | [aws_api_gateway_deployment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_deployment) | resource | 272 | | [aws_api_gateway_domain_name.edge_acm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_domain_name) | resource | 273 | | [aws_api_gateway_domain_name.edge_iam](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_domain_name) | resource | 274 | | [aws_api_gateway_domain_name.regional_acm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_domain_name) | resource | 275 | | [aws_api_gateway_domain_name.regional_iam](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_domain_name) | resource | 276 | | [aws_api_gateway_method_settings.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_method_settings) | resource | 277 | | [aws_api_gateway_model.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_model) | resource | 278 | | [aws_api_gateway_rest_api.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_rest_api) | resource | 279 | | [aws_api_gateway_rest_api_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_rest_api_policy) | resource | 280 | | [aws_api_gateway_stage.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_stage) | resource | 281 | | [aws_api_gateway_usage_plan.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_usage_plan) | resource | 282 | | [aws_api_gateway_usage_plan_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_usage_plan_key) | resource | 283 | | [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | 284 | | [aws_wafv2_web_acl_association.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource | 285 | 286 | ## Inputs 287 | 288 | | Name | Description | Type | Default | Required | 289 | |------|-------------|------|---------|:--------:| 290 | | [access\_log\_format](#input\_access\_log\_format) | (Required) Formatting and values recorded in the logs. For more information on configuring the log format rules visit the AWS documentation | `string` | n/a | yes | 291 | | [api\_key\_description](#input\_api\_key\_description) | (Optional) API key description. Defaults to `Managed by Terraform`. | `string` | `null` | no | 292 | | [api\_key\_name](#input\_api\_key\_name) | (Required) Name of the API key. | `string` | `null` | no | 293 | | [api\_keys](#input\_api\_keys) | Map of objects that define the usage plan to be created. |
list(
object({
name = string
key_type = string
usage_plan = string
})
)
|
[
{
"key_type": null,
"name": null,
"usage_plan": null
}
]
| no | 294 | | [api\_name](#input\_api\_name) | (Required) Name of the REST API. If importing an OpenAPI specification via the `body` argument, this corresponds to the `info.title` field. If the argument value is different than the OpenAPI value, the argument value will override the OpenAPI value. | `string` | `null` | no | 295 | | [burst\_limit](#input\_burst\_limit) | (Optional) - The API request burst limit, the maximum rate limit over a time ranging from one to a few seconds, depending upon whether the underlying token bucket is at its full capacity. | `number` | `5` | no | 296 | | [cache\_cluster\_enabled](#input\_cache\_cluster\_enabled) | (Optional) Whether a cache cluster is enabled for the stage. | `bool` | `true` | no | 297 | | [cache\_cluster\_size](#input\_cache\_cluster\_size) | (Optional) Size of the cache cluster for the stage, if enabled. Allowed values include `0.5`, `1.6`, `6.1`, `13.5`, `28.4`, `58.2`, `118` and `237`. | `number` | `0.5` | no | 298 | | [cache\_data\_encrypted](#input\_cache\_data\_encrypted) | (Optional) Whether the cached responses are encrypted. | `bool` | `false` | no | 299 | | [cache\_ttl\_in\_seconds](#input\_cache\_ttl\_in\_seconds) | (Optional) Time to live (TTL), in seconds, for cached responses. The higher the TTL, the longer the response will be cached. | `number` | `300` | no | 300 | | [caching\_enabled](#input\_caching\_enabled) | (Optional) Whether responses should be cached and returned for requests. A cache cluster must be enabled on the stage for responses to be cached. | `bool` | `true` | no | 301 | | [certificate\_type](#input\_certificate\_type) | This resource currently only supports managing a single value. Valid values: `ACM` or `IAM`. If unspecified, defaults to `acm` | `string` | `"ACM"` | no | 302 | | [client\_certificate\_id](#input\_client\_certificate\_id) | (Optional) Identifier of a client certificate for the stage. | `string` | `null` | no | 303 | | [client\_name](#input\_client\_name) | client name to use this api. | `string` | `null` | no | 304 | | [cloudwatch\_role\_arn](#input\_cloudwatch\_role\_arn) | (Required) for the `api_gateway_account` resource. | `string` | `null` | no | 305 | | [create\_api\_domain\_name](#input\_create\_api\_domain\_name) | Whether to create API domain name resource. | `bool` | `false` | no | 306 | | [create\_rest\_api\_policy](#input\_create\_rest\_api\_policy) | Enables creation of the resource policy for a given API. | `bool` | `true` | no | 307 | | [create\_usage\_plan](#input\_create\_usage\_plan) | Allows creation of a usage plan. (Requires `var.enable_api_key = true`) | `bool` | `false` | no | 308 | | [data\_trace\_enabled](#input\_data\_trace\_enabled) | (Optional) Whether data trace logging is enabled for this method, which effects the log entries pushed to Amazon CloudWatch Logs. | `bool` | `false` | no | 309 | | [description](#input\_description) | (Optional) Description of the REST API. If importing an OpenAPI specification via the `body` argument, this corresponds to the `info.description` field. If the argument value is provided and is different than the OpenAPI value, the argument value will override the OpenAPI value. | `string` | `null` | no | 310 | | [documentation\_version](#input\_documentation\_version) | (Optional) Version of the associated API documentation. | `string` | `null` | no | 311 | | [domain\_base\_path](#input\_domain\_base\_path) | (Optional) Path segment that must be prepended to the path when accessing the API via this mapping. If omitted, the API is exposed at the root of the given domain. | `string` | `"/"` | no | 312 | | [domain\_certificate\_arn](#input\_domain\_certificate\_arn) | The ARN of an AWS-managed certificate that will be used by the endpoint for the domain name. | `string` | `null` | no | 313 | | [domain\_certificate\_name](#input\_domain\_certificate\_name) | Unique name to use when registering this certificate as an IAM server certificate. Conflicts with certificate\_arn, regional\_certificate\_arn, and regional\_certificate\_name. Required if certificate\_arn is not set. | `string` | `null` | no | 314 | | [domain\_names](#input\_domain\_names) | Fully-qualified domain name to register. The domain names to use for API gateway it will use the index of stage\_names to select the domain name. | `list(string)` | `null` | no | 315 | | [enable\_api\_key](#input\_enable\_api\_key) | (Optional) Whether the API key can be used by callers. Defaults to `false`. | `bool` | `false` | no | 316 | | [enable\_canary](#input\_enable\_canary) | (Optional) Whether to use the values supplied for the canary and stage\_variable\_overrides or not. | `bool` | `false` | no | 317 | | [enable\_waf](#input\_enable\_waf) | Enables associating existing WAF ACL to all stages. | `bool` | `false` | no | 318 | | [endpoint\_type](#input\_endpoint\_type) | (Required) List of endpoint types. This resource currently only supports managing a single value. Valid values: `EDGE`, `REGIONAL` or `PRIVATE`. If unspecified, defaults to `EDGE`. Must be declared as `REGIONAL` in non-Commercial partitions. If set to `PRIVATE` recommend to set put\_rest\_api\_mode = merge to not cause the endpoints and associated Route53 records to be deleted. Refer to the documentation for more information on the difference between edge-optimized and regional APIs. | `string` | `"EDGE"` | no | 319 | | [iam\_certificate\_body](#input\_iam\_certificate\_body) | Certificate issued for the domain name being registered, in PEM format. Only valid for EDGE endpoint configuration type. Conflicts with certificate\_arn, regional\_certificate\_arn, and regional\_certificate\_name | `string` | `null` | no | 320 | | [iam\_certificate\_chain](#input\_iam\_certificate\_chain) | Certificate for the CA that issued the certificate, along with any intermediate CA certificates required to create an unbroken chain to a certificate trusted by the intended API clients. Only valid for EDGE endpoint configuration type. Conflicts with certificate\_arn, regional\_certificate\_arn, and regional\_certificate\_name. | `string` | `null` | no | 321 | | [iam\_certificate\_private\_key](#input\_iam\_certificate\_private\_key) | Private key associated with the domain certificate given in certificate\_body. Only valid for EDGE endpoint configuration type. Conflicts with certificate\_arn, regional\_certificate\_arn, and regional\_certificate\_name. | `string` | `null` | no | 322 | | [limit](#input\_limit) | (Optional) - Maximum number of requests that can be made in a given time period. | `number` | `20` | no | 323 | | [log\_group\_kms\_key](#input\_log\_group\_kms\_key) | (Optional) The ARN of the KMS Key to use when encrypting log data. Please note, after the AWS KMS CMK is disassociated from the log group, AWS CloudWatch Logs stops encrypting newly ingested data for the log group. All previously ingested data remains encrypted, and AWS CloudWatch Logs requires permissions for the CMK whenever the encrypted data is requested. | `string` | `null` | no | 324 | | [log\_group\_name](#input\_log\_group\_name) | (Optional, Forces new resource) The name of the log group. If omitted, Terraform will assign a random, unique name. | `string` | `null` | no | 325 | | [log\_group\_retention\_in\_days](#input\_log\_group\_retention\_in\_days) | (Optional) Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, and 0. If you select 0, the events in the log group are always retained and never expire. | `string` | `7` | no | 326 | | [logging\_level](#input\_logging\_level) | (Optional) Logging level for this method, which effects the log entries pushed to Amazon CloudWatch Logs. The available levels are OFF, ERROR, and INFO. | `string` | `"INFO"` | no | 327 | | [method\_settings](#input\_method\_settings) | Stage method settings | `any` | `{}` | no | 328 | | [metrics\_enabled](#input\_metrics\_enabled) | (Optional) Whether Amazon CloudWatch metrics are enabled for this method. | `string` | `true` | no | 329 | | [models](#input\_models) | n/a | `any` | `{}` | no | 330 | | [mutual\_tls\_authentication](#input\_mutual\_tls\_authentication) | An Amazon S3 URL that specifies the truststore for mutual TLS authentication as well as version, keyed at uri and version | `map(string)` | `{}` | no | 331 | | [offset](#input\_offset) | (Optional) - Number of requests subtracted from the given limit in the initial time period. | `number` | `2` | no | 332 | | [openapi\_definition](#input\_openapi\_definition) | (Required) YAML formatted definition file using OpenAPI 3.x specification. This definition contains all API configuration inputs. Any inputs used in Terraform will override inputs in the definition. | `string` | n/a | yes | 333 | | [percent\_traffic](#input\_percent\_traffic) | (Optional) Percent 0.0 - 100.0 of traffic to divert to the canary deployment. | `number` | `null` | no | 334 | | [period](#input\_period) | (Optional) - Time period in which the limit applies. Valid values are `DAY`, `WEEK` or `MONTH`. | `string` | `"WEEK"` | no | 335 | | [put\_rest\_api\_mode](#input\_put\_rest\_api\_mode) | (Optional) Mode of the PutRestApi operation when importing an OpenAPI specification via the body argument (create or update operation). Valid values are merge and overwrite. If unspecificed, defaults to overwrite (for backwards compatibility). This corresponds to the x-amazon-apigateway-put-integration-method extension. If the argument value is provided and is different than the OpenAPI value, the argument value will override the OpenAPI value. | `string` | `"overwrite"` | no | 336 | | [rate\_limit](#input\_rate\_limit) | (Optional) - The API request steady-state rate limit. | `number` | `10` | no | 337 | | [require\_authorization\_for\_cache\_control](#input\_require\_authorization\_for\_cache\_control) | (Optional) Whether authorization is required for a cache invalidation request. | `bool` | `true` | no | 338 | | [rest\_api\_policy](#input\_rest\_api\_policy) | (Required) JSON formatted policy document that controls access to the API Gateway. For more information about building AWS IAM policy documents with Terraform, see the AWS IAM Policy Document Guide | `string` | `""` | no | 339 | | [stage\_description](#input\_stage\_description) | (Optional) Description of the stage. | `string` | `null` | no | 340 | | [stage\_names](#input\_stage\_names) | (Required) Name of the stage(s). | `list(string)` | `null` | no | 341 | | [stage\_variable\_overrides](#input\_stage\_variable\_overrides) | (Optional) Map of overridden stage variables (including new variables) for the canary deployment. | `any` | `{}` | no | 342 | | [stage\_variables](#input\_stage\_variables) | (Optional) Map that defines the stage variables. | `any` | `{}` | no | 343 | | [tags](#input\_tags) | A map of tags to assign to resources. | `map(string)` | `{}` | no | 344 | | [throttling\_burst\_limit](#input\_throttling\_burst\_limit) | (Optional) Throttling burst limit. Default: -1 (throttling disabled). | `number` | `-1` | no | 345 | | [throttling\_rate\_limit](#input\_throttling\_rate\_limit) | (Optional) Throttling rate limit. Default: -1 (throttling disabled). | `number` | `-1` | no | 346 | | [unauthorized\_cache\_control\_header\_strategy](#input\_unauthorized\_cache\_control\_header\_strategy) | (Optional) How to handle unauthorized requests for cache invalidation. The available values are `FAIL_WITH_403`, `SUCCEED_WITH_RESPONSE_HEADER`, `SUCCEED_WITHOUT_RESPONSE_HEADER`. | `string` | `"SUCCEED_WITH_RESPONSE_HEADER"` | no | 347 | | [usage\_plans](#input\_usage\_plans) | Map of objects that define the usage plan to be created. |
list(
object({
name = string
description = string
burst_limit = number
rate_limit = number
quota_limit = number
quota_offset = number
quota_period = string
stages = list(string)
})
)
|
[
{
"burst_limit": null,
"description": null,
"name": null,
"quota_limit": null,
"quota_offset": null,
"quota_period": null,
"rate_limit": null,
"stages": [
null
]
}
]
| no | 348 | | [use\_stage\_cache](#input\_use\_stage\_cache) | (Optional) Whether the canary deployment uses the stage cache. Defaults to false. | `bool` | `false` | no | 349 | | [waf\_acl](#input\_waf\_acl) | (Required) The ID of the WAF Regional WebACL to create an association. | `string` | `null` | no | 350 | | [xray\_tracing\_enabled](#input\_xray\_tracing\_enabled) | (Optional) Whether active tracing with X-ray is enabled. Defaults to false. | `bool` | `false` | no | 351 | 352 | ## Outputs 353 | 354 | | Name | Description | 355 | |------|-------------| 356 | | [api\_gateway\_rest\_api\_arn](#output\_api\_gateway\_rest\_api\_arn) | Arn of the REST API. | 357 | | [api\_gateway\_rest\_api\_execution\_arn](#output\_api\_gateway\_rest\_api\_execution\_arn) | Execution Arn of the REST API. | 358 | | [api\_gateway\_rest\_api\_id](#output\_api\_gateway\_rest\_api\_id) | Id of the REST API. | 359 | | [api\_gateway\_rest\_api\_name](#output\_api\_gateway\_rest\_api\_name) | Name of the REST API. | 360 | | [api\_gateway\_rest\_api\_stage\_arn](#output\_api\_gateway\_rest\_api\_stage\_arn) | Arn of the deployed stage(s). | 361 | | [api\_gateway\_rest\_api\_stage\_execution\_arn](#output\_api\_gateway\_rest\_api\_stage\_execution\_arn) | Execution arn of the deployed stage(s). | 362 | | [api\_gateway\_rest\_api\_stage\_id](#output\_api\_gateway\_rest\_api\_stage\_id) | Id of the deployed stage(s). | 363 | | [api\_gateway\_rest\_api\_stage\_invoke\_url](#output\_api\_gateway\_rest\_api\_stage\_invoke\_url) | Invoke URL of the deployed stage(s). | 364 | | [api\_gateway\_rest\_api\_stage\_web\_acl](#output\_api\_gateway\_rest\_api\_stage\_web\_acl) | WAF Access Control List for the stage(s) | 365 | | [aws\_api\_gateway\_domain\_name](#output\_aws\_api\_gateway\_domain\_name) | api gateway domain name | 366 | 367 | -------------------------------------------------------------------------------- /assets/public-api-distributed-model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamwshero/terraform-aws-api-gateway/d4360d3d1a6c15b4bb76f5447a85cd2c242a0384/assets/public-api-distributed-model.png -------------------------------------------------------------------------------- /examples/terraform/README.md: -------------------------------------------------------------------------------- 1 | ### Terraform Basic Example + Lambda (as authorizer) 2 | ``` 3 | module "rest-api" { 4 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 5 | 6 | 7 | inputs = { 8 | api_name = "my-app-dev" 9 | description = "Development API for the My App service." 10 | endpoint_type = "REGIONAL" 11 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 12 | 13 | // API Definition & Vars 14 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 15 | { 16 | endpoint_uri = "https://my-app.nonprod.company.com}/my_app_path" 17 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 18 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 19 | } 20 | ) 21 | 22 | // Stage Settings 23 | stage_names = ["dev"] 24 | stage_description = "Development stage for My App API" 25 | log_group_name = "/aws/apigateway/access/my_app/dev" 26 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 27 | 28 | // Method Settings 29 | method_settings = { 30 | "dev /*/*" = { 31 | metrics_enabled = true 32 | logging_level = "INFO" 33 | data_trace_enabled = true 34 | throttling_burst_limit = 100 35 | throttling_rate_limit = 100 36 | caching_enabled = true 37 | cache_ttl_in_seconds = 300 38 | cache_data_encrypted = true 39 | require_authorization_for_cache_control = true 40 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 41 | } 42 | } 43 | 44 | // Security 45 | enable_waf = false 46 | 47 | // Execution Role 48 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 49 | cloudwatch_policy_name = "my-app-dev" 50 | 51 | // Usage Plans & API Keys 52 | create_usage_plan = false 53 | enable_api_key = false 54 | 55 | tags = local.tags 56 | } 57 | ``` 58 | ### Terraform Example + Lambda (as authorizer) + Stage Canary + Method Settings 59 | ``` 60 | module "rest-api" { 61 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 62 | 63 | 64 | inputs = { 65 | api_name = "my-app-dev" 66 | description = "Development API for the My App service." 67 | endpoint_type = "REGIONAL" 68 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 69 | 70 | // API Definition & Vars 71 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 72 | { 73 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 74 | vpc_link_id = "9ab12c" 75 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 76 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 77 | } 78 | ) 79 | 80 | // Stage Settings 81 | stage_names = ["dev"] 82 | stage_description = "Development stage for My App API" 83 | cache_cluster_enabled = true 84 | cache_cluster_size = 0.5 85 | xray_tracing_enabled = false 86 | log_group_name = "/aws/apigateway/access/my_app/dev" 87 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 88 | // Canary Stage Settings 89 | enable_canary = false 90 | use_stage_cache = false 91 | percent_traffic = 0 92 | stage_variable_overrides = { 93 | stage_description = "Canary Development stage for My App API" 94 | cache_cluster_enabled = false 95 | cache_cluster_size = 0.5 96 | xray_tracing_enabled = false 97 | } 98 | 99 | // Method Settings 100 | method_settings = { 101 | "dev /*/*" = { 102 | metrics_enabled = true 103 | logging_level = "INFO" 104 | data_trace_enabled = true 105 | throttling_burst_limit = 100 106 | throttling_rate_limit = 100 107 | caching_enabled = true 108 | cache_ttl_in_seconds = 300 109 | cache_data_encrypted = true 110 | require_authorization_for_cache_control = true 111 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 112 | } 113 | } 114 | 115 | // Security 116 | enable_waf = false 117 | 118 | // Execution Role 119 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 120 | cloudwatch_policy_name = "my-app-dev" 121 | 122 | // Usage Plans & API Keys 123 | create_usage_plan = false 124 | enable_api_key = false 125 | 126 | tags = local.tags 127 | } 128 | ``` 129 | 130 | ### Terraform Complete Example + Lambda (as authorizer) + Stage Canary + Method Settings + WAF 131 | ``` 132 | module "rest-api" { 133 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 134 | 135 | 136 | inputs = { 137 | api_name = "my-app-dev" 138 | description = "Development API for the My App service." 139 | endpoint_type = "REGIONAL" 140 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 141 | 142 | // API Definition & Vars 143 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 144 | { 145 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 146 | vpc_link_id = "9ab12c" 147 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 148 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 149 | } 150 | ) 151 | 152 | // Stage Settings 153 | stage_names = ["dev"] 154 | stage_description = "Development stage for My App API" 155 | cache_cluster_enabled = true 156 | cache_cluster_size = 0.5 157 | xray_tracing_enabled = false 158 | log_group_name = "/aws/apigateway/access/my_app/dev" 159 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 160 | // Canary Stage Settings 161 | enable_canary = false 162 | use_stage_cache = false 163 | percent_traffic = 0 164 | stage_variable_overrides = { 165 | stage_description = "Canary Development stage for My App API" 166 | cache_cluster_enabled = false 167 | cache_cluster_size = 0.5 168 | xray_tracing_enabled = false 169 | } 170 | 171 | // Method Settings 172 | method_settings = { 173 | "dev /*/*" = { 174 | metrics_enabled = true 175 | logging_level = "INFO" 176 | data_trace_enabled = true 177 | throttling_burst_limit = 100 178 | throttling_rate_limit = 100 179 | caching_enabled = true 180 | cache_ttl_in_seconds = 300 181 | cache_data_encrypted = true 182 | require_authorization_for_cache_control = true 183 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 184 | } 185 | } 186 | 187 | // Security 188 | enable_waf = true 189 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 190 | 191 | // Execution Role 192 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 193 | cloudwatch_policy_name = "my-app-dev" 194 | 195 | // Usage Plans & API Keys 196 | create_usage_plan = false 197 | enable_api_key = false 198 | 199 | tags = local.tags 200 | } 201 | ``` 202 | 203 | 204 | ### Terraform Complete Example + Lambda (as authorizer) + Stage Canary + Method Settings + WAF + API Keys + Usage Plans 205 | ``` 206 | module "rest-api" { 207 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 208 | 209 | 210 | inputs = { 211 | api_name = "my-app-dev" 212 | description = "Development API for the My App service." 213 | endpoint_type = "REGIONAL" 214 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 215 | 216 | // API Definition & Vars 217 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 218 | { 219 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 220 | vpc_link_id = "9ab12c" 221 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 222 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 223 | } 224 | ) 225 | 226 | // Stage Settings 227 | stage_names = ["dev"] 228 | stage_description = "Development stage for My App API" 229 | cache_cluster_enabled = true 230 | cache_cluster_size = 0.5 231 | xray_tracing_enabled = false 232 | log_group_name = "/aws/apigateway/access/my_app/dev" 233 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 234 | // Canary Stage Settings 235 | enable_canary = false 236 | use_stage_cache = false 237 | percent_traffic = 0 238 | stage_variable_overrides = { 239 | stage_description = "Canary Development stage for My App API" 240 | cache_cluster_enabled = false 241 | cache_cluster_size = 0.5 242 | xray_tracing_enabled = false 243 | } 244 | 245 | // Method Settings 246 | method_settings = { 247 | "dev /*/*" = { 248 | metrics_enabled = true 249 | logging_level = "INFO" 250 | data_trace_enabled = true 251 | throttling_burst_limit = 100 252 | throttling_rate_limit = 100 253 | caching_enabled = true 254 | cache_ttl_in_seconds = 300 255 | cache_data_encrypted = true 256 | require_authorization_for_cache_control = true 257 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 258 | } 259 | } 260 | 261 | // Security 262 | enable_waf = true 263 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 264 | 265 | // Execution Role 266 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 267 | cloudwatch_policy_name = "my-app-dev" 268 | 269 | // Usage Plans & API Keys 270 | create_usage_plan = true 271 | enable_api_key = true 272 | api_keys = [ 273 | { 274 | name = "open-use-internal-dev" 275 | key_type = "API_KEY" 276 | usage_plan = "open-use-internal-dev" 277 | }, 278 | { 279 | name = "external-partner-dev" 280 | key_type = "API_KEY" 281 | usage_plan = "external-partner-daily-throttle-dev" 282 | } 283 | ] 284 | usage_plans = [ 285 | { 286 | name = "open-use-internal-${local.env}" 287 | description = "Open API Usage Plan." 288 | burst_limit = 10000000 289 | rate_limit = 10000000 290 | quota_limit = 10000000 291 | quota_offset = 6 292 | quota_period = "WEEK" 293 | stages = ["dev"] 294 | }, 295 | { 296 | name = "external-partner-daily-throttle-dev" 297 | description = "Daily throttled API Usage Plan." 298 | burst_limit = 300 299 | rate_limit = 600 300 | quota_limit = 15000 301 | quota_offset = 0 302 | quota_period = "DAY", 303 | stages = ["dev"] 304 | } 305 | ] 306 | 307 | tags = local.tags 308 | } 309 | ``` 310 | -------------------------------------------------------------------------------- /examples/terraform/api_policy.json.tpl: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Principal": "*", 7 | "Action": "execute-api:Invoke", 8 | "Resource": [ 9 | "execute-api:/*" 10 | ] 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /examples/terraform/log_format.json.tpl: -------------------------------------------------------------------------------- 1 | { "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength","authError":"$context.authorizer.error"} -------------------------------------------------------------------------------- /examples/terraform/main.tf: -------------------------------------------------------------------------------- 1 | module "rest-api" { 2 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 3 | 4 | api_name = "my-app-dev" 5 | description = "Development API for the My App service." 6 | endpoint_type = "REGIONAL" 7 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 8 | 9 | // API Custom Domain 10 | create_api_domain_name = true 11 | domain_names = ["mydomain.something.com"] 12 | domain_certificate_arn = "arn:aws:acm:us-east-1:1111111111111:certificate/1aa11a11-a1a1-1a11-aaa1-1111aaaa1a1a" 13 | 14 | // API Resource Policy 15 | create_rest_api_policy = true 16 | rest_api_policy = templatefile("${get_terragrunt_dir()}/api_policy.json.tpl", 17 | {} 18 | ) 19 | 20 | // API Definition & Vars 21 | openapi_definition = templatefile("${path.module}/openapi.yaml", 22 | { 23 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 24 | vpc_link_id = "9ab12c" 25 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 26 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 27 | } 28 | ) 29 | 30 | // Stage Settings 31 | stage_names = ["dev"] 32 | stage_description = "Development stage for My App API" 33 | cache_cluster_enabled = true 34 | cache_cluster_size = 0.5 35 | xray_tracing_enabled = false 36 | log_group_name = "/aws/apigateway/access/my_app/dev" 37 | access_log_format = templatefile("${path.module}/log_format.json.tpl", {}) 38 | // Canary Stage Settings 39 | enable_canary = false 40 | use_stage_cache = false 41 | percent_traffic = 0 42 | stage_variable_overrides = { 43 | stage_description = "Canary Development stage for My App API" 44 | cache_cluster_enabled = false 45 | cache_cluster_size = 0.5 46 | xray_tracing_enabled = false 47 | } 48 | 49 | // Method Settings 50 | method_settings = { 51 | "dev /*/*" = { 52 | metrics_enabled = true 53 | logging_level = "INFO" 54 | data_trace_enabled = true 55 | throttling_burst_limit = 100 56 | throttling_rate_limit = 100 57 | caching_enabled = true 58 | cache_ttl_in_seconds = 300 59 | cache_data_encrypted = true 60 | require_authorization_for_cache_control = true 61 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 62 | } 63 | } 64 | 65 | // Security 66 | enable_waf = true 67 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 68 | 69 | // Execution Role 70 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 71 | cloudwatch_policy_name = "my-app-dev" 72 | 73 | // Usage Plans & API Keys 74 | create_usage_plan = true 75 | enable_api_key = true 76 | api_keys = [ 77 | { 78 | name = "open-use-internal-dev" 79 | key_type = "API_KEY" 80 | usage_plan = "open-use-internal-dev" 81 | }, 82 | { 83 | name = "external-partner-dev" 84 | key_type = "API_KEY" 85 | usage_plan = "external-partner-daily-throttle-dev" 86 | } 87 | ] 88 | usage_plans = [ 89 | { 90 | name = "open-use-internal-${local.env}" 91 | description = "Open API Usage Plan." 92 | burst_limit = 10000000 93 | rate_limit = 10000000 94 | quota_limit = 10000000 95 | quota_offset = 6 96 | quota_period = "WEEK" 97 | stages = ["dev"] 98 | }, 99 | { 100 | name = "external-partner-daily-throttle-dev" 101 | description = "Daily throttled API Usage Plan." 102 | burst_limit = 300 103 | rate_limit = 600 104 | quota_limit = 15000 105 | quota_offset = 0 106 | quota_period = "DAY", 107 | stages = ["dev"] 108 | } 109 | ] 110 | 111 | tags = local.tags 112 | } 113 | -------------------------------------------------------------------------------- /examples/terraform/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: 3.0.1 2 | info: 3 | title: Integrations 4 | description: An API that reuses integrations 5 | version: '1.0' 6 | paths: 7 | "/path": 8 | get: 9 | x-amazon-apigateway-integration: 10 | type: HTTP_PROXY 11 | uri: ${endpoint_uri} 12 | httpMethod: ANY 13 | passthroughBehavior: when_no_match 14 | payloadFormatVersion: '1.0' 15 | connectionType: VPC_LINK 16 | connectionId: ${vpc_link_id} 17 | responses: 18 | '200': 19 | description: Return a 200 20 | security: [ 21 | apiKeyAuth: [] 22 | ] 23 | x-amazon-apigateway-endpoint-configuration: 24 | vpcEndpointIds: [""] 25 | disableExecuteApiEndpoint: true 26 | "/path2": 27 | post: 28 | x-amazon-apigateway-integration: 29 | type: HTTP_PROXY 30 | uri: ${endpoint_uri} 31 | httpMethod: ANY 32 | passthroughBehavior: when_no_match 33 | payloadFormatVersion: '1.0' 34 | connectionType: VPC_LINK 35 | connectionId: ${vpc_link_id} 36 | responses: 37 | '200': 38 | description: Return a 200 39 | security: [ 40 | lambdaAuth: [] 41 | ] 42 | components: 43 | securitySchemes: 44 | lambdaAuth: 45 | type: apiKey 46 | name: cs-authorization 47 | in: header 48 | x-amazon-apigateway-authtype: custom 49 | x-amazon-apigateway-authorizer: 50 | type: request 51 | identitySource: method.request.header.cs-authorization 52 | authorizerUri: ${lambda_invoke_arn} 53 | identityValidationExpression: "^x-[a-z]+" 54 | authorizerResultTtlInSeconds: 60 55 | apiKeyAuth: 56 | type: apiKey 57 | name: x-api-key 58 | in: header 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /examples/terragrunt/README.md: -------------------------------------------------------------------------------- 1 | ### Terragrunt Basic Example + Lambda (as authorizer) 2 | ``` 3 | terraform { 4 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 5 | } 6 | 7 | inputs = { 8 | api_name = "my-app-dev" 9 | description = "Development API for the My App service." 10 | endpoint_type = "REGIONAL" 11 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 12 | 13 | // API Definition & Vars 14 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 15 | { 16 | endpoint_uri = "https://my-app.nonprod.company.com}/my_app_path" 17 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 18 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 19 | } 20 | ) 21 | 22 | // Stage Settings 23 | stage_names = ["dev"] 24 | stage_description = "Development stage for My App API" 25 | log_group_name = "/aws/apigateway/access/my_app/dev" 26 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 27 | 28 | // Method Settings 29 | method_settings = { 30 | "dev /*/*" = { 31 | metrics_enabled = true 32 | logging_level = "INFO" 33 | data_trace_enabled = true 34 | throttling_burst_limit = 100 35 | throttling_rate_limit = 100 36 | caching_enabled = true 37 | cache_ttl_in_seconds = 300 38 | cache_data_encrypted = true 39 | require_authorization_for_cache_control = true 40 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 41 | } 42 | } 43 | 44 | // Security 45 | enable_waf = false 46 | 47 | // Execution Role 48 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 49 | cloudwatch_policy_name = "my-app-dev" 50 | 51 | // Usage Plans & API Keys 52 | create_usage_plan = false 53 | enable_api_key = false 54 | 55 | tags = local.tags 56 | } 57 | ``` 58 | ### Terragrunt Example + Lambda (as authorizer) + Stage Canary + Method Settings 59 | ``` 60 | terraform { 61 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 62 | } 63 | 64 | inputs = { 65 | api_name = "my-app-dev" 66 | description = "Development API for the My App service." 67 | endpoint_type = "REGIONAL" 68 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 69 | 70 | // API Definition & Vars 71 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 72 | { 73 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 74 | vpc_link_id = "9ab12c" 75 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 76 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 77 | } 78 | ) 79 | 80 | // Stage Settings 81 | stage_names = ["dev"] 82 | stage_description = "Development stage for My App API" 83 | cache_cluster_enabled = true 84 | cache_cluster_size = 0.5 85 | xray_tracing_enabled = false 86 | log_group_name = "/aws/apigateway/access/my_app/dev" 87 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 88 | // Canary Stage Settings 89 | enable_canary = false 90 | use_stage_cache = false 91 | percent_traffic = 0 92 | stage_variable_overrides = { 93 | stage_description = "Canary Development stage for My App API" 94 | cache_cluster_enabled = false 95 | cache_cluster_size = 0.5 96 | xray_tracing_enabled = false 97 | } 98 | 99 | // Method Settings 100 | method_settings = { 101 | "dev /*/*" = { 102 | metrics_enabled = true 103 | logging_level = "INFO" 104 | data_trace_enabled = true 105 | throttling_burst_limit = 100 106 | throttling_rate_limit = 100 107 | caching_enabled = true 108 | cache_ttl_in_seconds = 300 109 | cache_data_encrypted = true 110 | require_authorization_for_cache_control = true 111 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 112 | } 113 | } 114 | 115 | // Security 116 | enable_waf = false 117 | 118 | // Execution Role 119 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 120 | cloudwatch_policy_name = "my-app-dev" 121 | 122 | // Usage Plans & API Keys 123 | create_usage_plan = false 124 | enable_api_key = false 125 | 126 | tags = local.tags 127 | } 128 | ``` 129 | 130 | ### Terragrunt Complete Example + Lambda (as authorizer) + Stage Canary + Method Settings + WAF 131 | ``` 132 | terraform { 133 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 134 | } 135 | 136 | inputs = { 137 | api_name = "my-app-dev" 138 | description = "Development API for the My App service." 139 | endpoint_type = "REGIONAL" 140 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 141 | 142 | // API Definition & Vars 143 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 144 | { 145 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 146 | vpc_link_id = "9ab12c" 147 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 148 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 149 | } 150 | ) 151 | 152 | // Stage Settings 153 | stage_names = ["dev"] 154 | stage_description = "Development stage for My App API" 155 | cache_cluster_enabled = true 156 | cache_cluster_size = 0.5 157 | xray_tracing_enabled = false 158 | log_group_name = "/aws/apigateway/access/my_app/dev" 159 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 160 | // Canary Stage Settings 161 | enable_canary = false 162 | use_stage_cache = false 163 | percent_traffic = 0 164 | stage_variable_overrides = { 165 | stage_description = "Canary Development stage for My App API" 166 | cache_cluster_enabled = false 167 | cache_cluster_size = 0.5 168 | xray_tracing_enabled = false 169 | } 170 | 171 | // Method Settings 172 | method_settings = { 173 | "dev /*/*" = { 174 | metrics_enabled = true 175 | logging_level = "INFO" 176 | data_trace_enabled = true 177 | throttling_burst_limit = 100 178 | throttling_rate_limit = 100 179 | caching_enabled = true 180 | cache_ttl_in_seconds = 300 181 | cache_data_encrypted = true 182 | require_authorization_for_cache_control = true 183 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 184 | } 185 | } 186 | 187 | // Security 188 | enable_waf = true 189 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 190 | 191 | // Execution Role 192 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 193 | cloudwatch_policy_name = "my-app-dev" 194 | 195 | // Usage Plans & API Keys 196 | create_usage_plan = false 197 | enable_api_key = false 198 | 199 | tags = local.tags 200 | } 201 | ``` 202 | 203 | 204 | ### Terragrunt Complete Example + Lambda (as authorizer) + Stage Canary + Method Settings + WAF + API Keys + Usage Plans 205 | ``` 206 | terraform { 207 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 208 | } 209 | 210 | inputs = { 211 | api_name = "my-app-dev" 212 | description = "Development API for the My App service." 213 | endpoint_type = "REGIONAL" 214 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 215 | 216 | // API Definition & Vars 217 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 218 | { 219 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 220 | vpc_link_id = "9ab12c" 221 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 222 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 223 | } 224 | ) 225 | 226 | // Stage Settings 227 | stage_names = ["dev"] 228 | stage_description = "Development stage for My App API" 229 | cache_cluster_enabled = true 230 | cache_cluster_size = 0.5 231 | xray_tracing_enabled = false 232 | log_group_name = "/aws/apigateway/access/my_app/dev" 233 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 234 | // Canary Stage Settings 235 | enable_canary = false 236 | use_stage_cache = false 237 | percent_traffic = 0 238 | stage_variable_overrides = { 239 | stage_description = "Canary Development stage for My App API" 240 | cache_cluster_enabled = false 241 | cache_cluster_size = 0.5 242 | xray_tracing_enabled = false 243 | } 244 | 245 | // Method Settings 246 | method_settings = { 247 | "dev /*/*" = { 248 | metrics_enabled = true 249 | logging_level = "INFO" 250 | data_trace_enabled = true 251 | throttling_burst_limit = 100 252 | throttling_rate_limit = 100 253 | caching_enabled = true 254 | cache_ttl_in_seconds = 300 255 | cache_data_encrypted = true 256 | require_authorization_for_cache_control = true 257 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 258 | } 259 | } 260 | 261 | // Security 262 | enable_waf = true 263 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 264 | 265 | // Execution Role 266 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 267 | cloudwatch_policy_name = "my-app-dev" 268 | 269 | // Usage Plans & API Keys 270 | create_usage_plan = true 271 | enable_api_key = true 272 | api_keys = [ 273 | { 274 | name = "open-use-internal-dev" 275 | key_type = "API_KEY" 276 | usage_plan = "open-use-internal-dev" 277 | }, 278 | { 279 | name = "external-partner-dev" 280 | key_type = "API_KEY" 281 | usage_plan = "external-partner-daily-throttle-dev" 282 | } 283 | ] 284 | usage_plans = [ 285 | { 286 | name = "open-use-internal-${local.env}" 287 | description = "Open API Usage Plan." 288 | burst_limit = 10000000 289 | rate_limit = 10000000 290 | quota_limit = 10000000 291 | quota_offset = 6 292 | quota_period = "WEEK" 293 | stages = ["dev"] 294 | }, 295 | { 296 | name = "external-partner-daily-throttle-dev" 297 | description = "Daily throttled API Usage Plan." 298 | burst_limit = 300 299 | rate_limit = 600 300 | quota_limit = 15000 301 | quota_offset = 0 302 | quota_period = "DAY", 303 | stages = ["dev"] 304 | } 305 | ] 306 | 307 | tags = local.tags 308 | } 309 | ``` 310 | -------------------------------------------------------------------------------- /examples/terragrunt/api_policy.json.tpl: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Principal": "*", 7 | "Action": "execute-api:Invoke", 8 | "Resource": [ 9 | "execute-api:/*" 10 | ] 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /examples/terragrunt/log_format.json.tpl: -------------------------------------------------------------------------------- 1 | { "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength","authError":"$context.authorizer.error"} -------------------------------------------------------------------------------- /examples/terragrunt/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: 3.0.1 2 | info: 3 | title: Integrations 4 | description: An API that reuses integrations 5 | version: '1.0' 6 | paths: 7 | "/path": 8 | get: 9 | x-amazon-apigateway-integration: 10 | type: HTTP_PROXY 11 | uri: ${endpoint_uri} 12 | httpMethod: ANY 13 | passthroughBehavior: when_no_match 14 | payloadFormatVersion: '1.0' 15 | connectionType: VPC_LINK 16 | connectionId: ${vpc_link_id} 17 | security: [ 18 | apiKeyAuth: [] 19 | ] 20 | x-amazon-apigateway-endpoint-configuration: 21 | vpcEndpointIds: [""] 22 | disableExecuteApiEndpoint: true 23 | "/path2": 24 | post: 25 | x-amazon-apigateway-integration: 26 | type: HTTP_PROXY 27 | uri: ${endpoint_uri} 28 | httpMethod: ANY 29 | passthroughBehavior: when_no_match 30 | payloadFormatVersion: '1.0' 31 | connectionType: VPC_LINK 32 | connectionId: ${vpc_link_id} 33 | security: [ 34 | lambdaAuth: [] 35 | ] 36 | components: 37 | securitySchemes: 38 | lambdaAuth: 39 | type: apiKey 40 | name: cs-authorization 41 | in: header 42 | x-amazon-apigateway-authtype: custom 43 | x-amazon-apigateway-authorizer: 44 | type: request 45 | identitySource: method.request.header.cs-authorization 46 | authorizerUri: ${lambda_invoke_arn} 47 | identityValidationExpression: "^x-[a-z]+" 48 | authorizerResultTtlInSeconds: 60 49 | apiKeyAuth: 50 | type: apiKey 51 | name: x-api-key 52 | in: header 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /examples/terragrunt/terragrunt.hcl: -------------------------------------------------------------------------------- 1 | locals { 2 | account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl")) 3 | product_vars = read_terragrunt_config(find_in_parent_folders("product.hcl")) 4 | env_vars = read_terragrunt_config(find_in_parent_folders("env.hcl")) 5 | product = local.product_vars.locals.product_name 6 | prefix = local.product_vars.locals.prefix 7 | account = local.account_vars.locals.account_id 8 | env = local.env_vars.locals.env 9 | 10 | tags = merge( 11 | local.env_vars.locals.tags, 12 | local.additional_tags 13 | ) 14 | 15 | additional_tags = { 16 | } 17 | } 18 | 19 | include { 20 | path = find_in_parent_folders() 21 | } 22 | 23 | dependency "waf_acl" { 24 | config_path = "../waf" 25 | } 26 | 27 | dependency "execution_role" { 28 | config_path = "../iam/roles/apigw-execution" 29 | } 30 | 31 | dependency "execution_policy" { 32 | config_path = "../iam/policies/apigw-execution" 33 | } 34 | 35 | terraform { 36 | source = "git::git@github.com:adamwshero/terraform-aws-api-gateway.git//.?ref=1.4.0" 37 | } 38 | 39 | inputs = { 40 | api_name = "my-app-dev" 41 | description = "Development API for the My App service." 42 | endpoint_type = "REGIONAL" 43 | put_put_rest_api_mode = "merge" // Toggle to `overwrite` only when renaming a resource path or removing a resource from the openapi definition. 44 | 45 | // API Custom Domain 46 | create_api_domain_name = true 47 | domain_names = ["mydomain.something.com"] 48 | domain_certificate_arn = "arn:aws:acm:us-east-1:1111111111111:certificate/1aa11a11-a1a1-1a11-aaa1-1111aaaa1a1a" 49 | 50 | // API Resource Policy 51 | create_rest_api_policy = true 52 | rest_api_policy = templatefile("${get_terragrunt_dir()}/api_policy.json.tpl", 53 | {} 54 | ) 55 | 56 | // API Definition & Vars 57 | openapi_definition = templatefile("${get_terragrunt_dir()}/openapi.yaml", 58 | { 59 | endpoint_uri = "https://my-app.nonprod.company.com/my_app_path" 60 | vpc_link_id = "9ab12c" 61 | authorizer_invoke_arn = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:111111111111:function:my-app-dev/invocation" 62 | authorizer_execution_arn = "arn:aws:iam::111111111111:role/my-app-dev" 63 | } 64 | ) 65 | 66 | // Stage Settings 67 | stage_names = ["dev"] 68 | stage_description = "Development stage for My App API" 69 | cache_cluster_enabled = true 70 | cache_cluster_size = 0.5 71 | xray_tracing_enabled = false 72 | log_group_name = "/aws/apigateway/access/my_app/dev" 73 | access_log_format = templatefile("${get_terragrunt_dir()}/log_format.json.tpl", {}) 74 | // Canary Stage Settings 75 | enable_canary = false 76 | use_stage_cache = false 77 | percent_traffic = 0 78 | stage_variable_overrides = { 79 | stage_description = "Canary Development stage for My App API" 80 | cache_cluster_enabled = false 81 | cache_cluster_size = 0.5 82 | xray_tracing_enabled = false 83 | } 84 | 85 | // Method Settings 86 | method_settings = { 87 | "dev /*/*" = { 88 | metrics_enabled = true 89 | logging_level = "INFO" 90 | data_trace_enabled = true 91 | throttling_burst_limit = 100 92 | throttling_rate_limit = 100 93 | caching_enabled = true 94 | cache_ttl_in_seconds = 300 95 | cache_data_encrypted = true 96 | require_authorization_for_cache_control = true 97 | unauthorized_cache_control_header_strategy = "FAIL_WITH_403" 98 | } 99 | } 100 | 101 | // Security 102 | enable_waf = true 103 | waf_acl = "arn:aws:wafv2:us-east-1:111111111111:regional/webacl/my-app-nonprod/f111b1a1-1c11-1ea1-a111-cd1fe111b11a" 104 | 105 | // Execution Role 106 | cloudwatch_role_arn = "arn:aws:iam::111111111111:role/my-app" 107 | cloudwatch_policy_name = "my-app-dev" 108 | 109 | // Usage Plans & API Keys 110 | create_usage_plan = true 111 | enable_api_key = true 112 | api_keys = [ 113 | { 114 | name = "open-use-internal-dev" 115 | key_type = "API_KEY" 116 | usage_plan = "open-use-internal-dev" 117 | }, 118 | { 119 | name = "external-partner-dev" 120 | key_type = "API_KEY" 121 | usage_plan = "external-partner-daily-throttle-dev" 122 | } 123 | ] 124 | usage_plans = [ 125 | { 126 | name = "open-use-internal-${local.env}" 127 | description = "Open API Usage Plan." 128 | burst_limit = 10000000 129 | rate_limit = 10000000 130 | quota_limit = 10000000 131 | quota_offset = 6 132 | quota_period = "WEEK" 133 | stages = ["dev"] 134 | }, 135 | { 136 | name = "external-partner-daily-throttle-dev" 137 | description = "Daily throttled API Usage Plan." 138 | burst_limit = 300 139 | rate_limit = 600 140 | quota_limit = 15000 141 | quota_offset = 0 142 | quota_period = "DAY", 143 | stages = ["dev"] 144 | } 145 | ] 146 | 147 | tags = local.tags 148 | } 149 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | resource "aws_api_gateway_rest_api" "this" { 2 | name = var.api_name 3 | description = var.description 4 | put_rest_api_mode = var.put_rest_api_mode 5 | body = var.openapi_definition 6 | tags = var.tags 7 | 8 | endpoint_configuration { 9 | types = [ var.endpoint_type ] 10 | } 11 | } 12 | 13 | resource "aws_api_gateway_deployment" "this" { 14 | count = length(var.stage_names) > 0 ? length(var.stage_names) : 0 15 | 16 | rest_api_id = aws_api_gateway_rest_api.this.id 17 | description = "Managed by Terraform" 18 | triggers = { 19 | # NOTE: The configuration below will satisfy ordering considerations, 20 | # but not pick up all future REST API changes. More advanced patterns 21 | # are possible, such as using the filesha1() function against the 22 | # Terraform configuration file(s) or removing the .id references to 23 | # calculate a hash against whole resources. Be aware that using whole 24 | # resources will show a difference after the initial implementation. 25 | # It will stabilize to only change when resources change afterwards. 26 | 27 | # We can use this method if we want to isolate deploys to a specific 28 | # resource or resource attribute. But for now we just deploy every time 29 | # with {timestamp()}. 30 | # https://github.com/hashicorp/terraform-provider-aws/issues/162 31 | 32 | # redeployment = sha1(jsonencode([ 33 | # aws_api_gateway_rest_api.this.body 34 | # ] 35 | # )) 36 | 37 | # We deploy the API every time Terraform is applied instead of using the 38 | # above method of only applying when the body of the openapi.yaml is 39 | # updated. 40 | redeployment = "${timestamp()}" 41 | } 42 | lifecycle { 43 | create_before_destroy = true 44 | } 45 | } 46 | 47 | resource "aws_api_gateway_stage" "this" { 48 | count = length(var.stage_names) > 0 ? length(var.stage_names) : 0 49 | 50 | rest_api_id = aws_api_gateway_rest_api.this.id 51 | stage_name = var.stage_names[count.index] 52 | description = "${var.stage_description} - Deployed on ${timestamp()}" 53 | documentation_version = var.documentation_version 54 | deployment_id = aws_api_gateway_deployment.this[count.index].id 55 | cache_cluster_enabled = var.cache_cluster_enabled 56 | cache_cluster_size = var.cache_cluster_size 57 | client_certificate_id = var.client_certificate_id 58 | variables = var.stage_variables 59 | xray_tracing_enabled = var.xray_tracing_enabled 60 | 61 | dynamic "access_log_settings" { 62 | for_each = aws_cloudwatch_log_group.this.arn != null && var.access_log_format != null ? [true] : [] 63 | 64 | content { 65 | destination_arn = aws_cloudwatch_log_group.this.arn 66 | format = var.access_log_format 67 | } 68 | } 69 | dynamic "canary_settings" { 70 | for_each = var.enable_canary == true ? [true] : [] 71 | 72 | content { 73 | percent_traffic = var.percent_traffic 74 | stage_variable_overrides = var.stage_variable_overrides 75 | use_stage_cache = var.use_stage_cache 76 | } 77 | } 78 | lifecycle { 79 | create_before_destroy = false 80 | } 81 | } 82 | 83 | resource "aws_api_gateway_method_settings" "this" { 84 | for_each = var.method_settings 85 | 86 | rest_api_id = aws_api_gateway_rest_api.this.id 87 | stage_name = element(split(" ", each.key), 0) 88 | 89 | # Extract method path after the stage name, formatted correctly 90 | method_path = join("/", slice(split(" ", each.key), 1, length(split(" ", each.key)))) 91 | 92 | dynamic "settings" { 93 | for_each = length(var.method_settings) > 0 ? [true] : [] 94 | content { 95 | metrics_enabled = lookup(each.value, "metrics_enabled", false) 96 | logging_level = lookup(each.value, "logging_level", "OFF") 97 | data_trace_enabled = lookup(each.value, "data_trace_enabled", false) 98 | throttling_burst_limit = lookup(each.value, "throttling_burst_limit", 0) 99 | throttling_rate_limit = lookup(each.value, "throttling_rate_limit", 0) 100 | caching_enabled = lookup(each.value, "caching_enabled", false) 101 | cache_ttl_in_seconds = lookup(each.value, "cache_ttl_in_seconds", 0) 102 | cache_data_encrypted = lookup(each.value, "cache_data_encrypted", false) 103 | require_authorization_for_cache_control = lookup(each.value, "require_authorization_for_cache_control", false) 104 | unauthorized_cache_control_header_strategy = lookup(each.value, "unauthorized_cache_control_header_strategy", "FAIL_WITH_403") 105 | } 106 | } 107 | } 108 | 109 | 110 | 111 | resource "aws_api_gateway_account" "this" { 112 | cloudwatch_role_arn = var.cloudwatch_role_arn 113 | } 114 | resource "aws_api_gateway_api_key" "this" { 115 | for_each = { 116 | for key in var.api_keys : key.name => { 117 | name = key.name 118 | } 119 | if var.create_usage_plan == true && var.enable_api_key == true && length(var.stage_names) > 0 120 | } 121 | enabled = var.enable_api_key 122 | name = each.value.name 123 | } 124 | 125 | resource "aws_api_gateway_usage_plan" "this" { 126 | for_each = { 127 | for key in var.usage_plans : key.name => { 128 | name = key.name 129 | description = key.description 130 | burst_limit = key.burst_limit 131 | rate_limit = key.rate_limit 132 | quota_limit = key.quota_limit 133 | quota_offset = key.quota_offset 134 | quota_period = key.quota_period 135 | stages = key.stages 136 | } 137 | if var.create_usage_plan == true && length(var.stage_names) > 0 138 | } 139 | 140 | name = var.client_name == null ? "${each.value.name}" : "${each.value.name}" 141 | description = var.client_name == null ? "${each.value.description}" : "${each.value.description} for ${var.client_name}." 142 | dynamic "api_stages" { 143 | for_each = each.value.stages 144 | content { 145 | api_id = aws_api_gateway_rest_api.this.id 146 | stage = api_stages.value 147 | } 148 | } 149 | quota_settings { 150 | limit = each.value.quota_limit 151 | offset = each.value.quota_offset 152 | period = each.value.quota_period 153 | } 154 | throttle_settings { 155 | burst_limit = each.value.burst_limit 156 | rate_limit = each.value.rate_limit 157 | } 158 | depends_on = [ 159 | aws_api_gateway_stage.this 160 | ] 161 | } 162 | 163 | resource "aws_api_gateway_usage_plan_key" "this" { 164 | for_each = { 165 | for key in var.api_keys : key.name => { 166 | name = key.name 167 | key_type = key.key_type 168 | usage_plan = key.usage_plan 169 | } 170 | if var.create_usage_plan == true && length(var.stage_names) > 0 171 | } 172 | 173 | key_id = aws_api_gateway_api_key.this[each.key].id 174 | key_type = each.value.key_type 175 | usage_plan_id = aws_api_gateway_usage_plan.this[each.value.usage_plan].id 176 | depends_on = [ 177 | aws_api_gateway_api_key.this, 178 | aws_api_gateway_usage_plan.this, 179 | ] 180 | } 181 | 182 | resource "aws_cloudwatch_log_group" "this" { 183 | name = var.log_group_name 184 | retention_in_days = var.log_group_retention_in_days 185 | kms_key_id = var.log_group_kms_key 186 | } 187 | 188 | resource "aws_wafv2_web_acl_association" "this" { 189 | count = var.enable_waf != false && length(var.stage_names) > 0 ? length(var.stage_names) : 0 190 | resource_arn = aws_api_gateway_stage.this[count.index].arn 191 | web_acl_arn = var.waf_acl 192 | } 193 | 194 | # EXISTING custom domain name 195 | resource "aws_api_gateway_base_path_mapping" "existing" { 196 | count = !var.create_api_domain_name && length(var.stage_names) > 0 ? length(var.stage_names) : 0 197 | 198 | api_id = aws_api_gateway_rest_api.this.id 199 | domain_name = var.domain_names[count.index] 200 | stage_name = aws_api_gateway_stage.this[count.index].stage_name 201 | base_path = var.domain_base_path 202 | } 203 | 204 | # REGIONAL ACM custom domain name 205 | resource "aws_api_gateway_domain_name" "regional_acm" { 206 | count = var.create_api_domain_name && var.endpoint_type == "REGIONAL" && var.certificate_type == "ACM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 207 | 208 | domain_name = var.domain_names[count.index] 209 | regional_certificate_arn = var.domain_certificate_arn 210 | endpoint_configuration { 211 | types = ["REGIONAL"] 212 | } 213 | 214 | dynamic "mutual_tls_authentication" { 215 | for_each = length(keys(var.mutual_tls_authentication)) == 0 ? [] : [var.mutual_tls_authentication] 216 | 217 | content { 218 | truststore_uri = mutual_tls_authentication.value.truststore_uri 219 | truststore_version = try(mutual_tls_authentication.value.truststore_version, null) 220 | } 221 | } 222 | } 223 | 224 | resource "aws_api_gateway_base_path_mapping" "regional_acm" { 225 | count = var.create_api_domain_name && var.endpoint_type == "REGIONAL" && var.certificate_type == "ACM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 226 | 227 | api_id = aws_api_gateway_rest_api.this.id 228 | domain_name = aws_api_gateway_domain_name.regional_acm[count.index].id 229 | stage_name = aws_api_gateway_stage.this[count.index].stage_name 230 | base_path = var.domain_base_path 231 | } 232 | 233 | # REGIONAL IAM custom domain name 234 | resource "aws_api_gateway_domain_name" "regional_iam" { 235 | count = var.create_api_domain_name && var.endpoint_type == "REGIONAL" && var.certificate_type == "IAM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 236 | 237 | domain_name = var.domain_names[count.index] 238 | 239 | regional_certificate_name = var.domain_certificate_name 240 | certificate_body = var.iam_certificate_body 241 | certificate_chain = var.iam_certificate_chain 242 | certificate_private_key = var.iam_certificate_private_key 243 | 244 | endpoint_configuration { 245 | types = ["REGIONAL"] 246 | } 247 | 248 | dynamic "mutual_tls_authentication" { 249 | for_each = length(keys(var.mutual_tls_authentication)) == 0 ? [] : [var.mutual_tls_authentication] 250 | 251 | content { 252 | truststore_uri = mutual_tls_authentication.value.truststore_uri 253 | truststore_version = try(mutual_tls_authentication.value.truststore_version, null) 254 | } 255 | } 256 | } 257 | 258 | resource "aws_api_gateway_base_path_mapping" "regional_iam" { 259 | count = var.create_api_domain_name && var.endpoint_type == "REGIONAL" && var.certificate_type == "IAM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 260 | 261 | api_id = aws_api_gateway_rest_api.this.id 262 | domain_name = aws_api_gateway_domain_name.regional_iam[count.index].id 263 | stage_name = aws_api_gateway_stage.this[count.index].stage_name 264 | base_path = var.domain_base_path 265 | } 266 | 267 | # EDGE ACM custom domain name 268 | resource "aws_api_gateway_domain_name" "edge_acm" { 269 | count = var.create_api_domain_name && var.endpoint_type == "EDGE" && var.certificate_type == "ACM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 270 | 271 | domain_name = var.domain_names[count.index] 272 | 273 | certificate_arn = var.domain_certificate_arn 274 | 275 | endpoint_configuration { 276 | types = ["EDGE"] 277 | } 278 | 279 | dynamic "mutual_tls_authentication" { 280 | for_each = length(keys(var.mutual_tls_authentication)) == 0 ? [] : [var.mutual_tls_authentication] 281 | 282 | content { 283 | truststore_uri = mutual_tls_authentication.value.truststore_uri 284 | truststore_version = try(mutual_tls_authentication.value.truststore_version, null) 285 | } 286 | } 287 | } 288 | 289 | resource "aws_api_gateway_base_path_mapping" "edge_acm" { 290 | count = var.create_api_domain_name && var.endpoint_type == "EDGE" && var.certificate_type == "ACM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 291 | 292 | api_id = aws_api_gateway_rest_api.this.id 293 | domain_name = aws_api_gateway_domain_name.edge_acm[count.index].id 294 | stage_name = aws_api_gateway_stage.this[count.index].stage_name 295 | base_path = var.domain_base_path 296 | } 297 | 298 | # EDGE IAM custom domain name 299 | resource "aws_api_gateway_domain_name" "edge_iam" { 300 | count = var.create_api_domain_name && var.endpoint_type == "EDGE" && var.certificate_type == "IAM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 301 | 302 | domain_name = var.domain_names[count.index] 303 | 304 | certificate_name = var.domain_certificate_name 305 | certificate_body = var.iam_certificate_body 306 | certificate_chain = var.iam_certificate_chain 307 | certificate_private_key = var.iam_certificate_private_key 308 | 309 | endpoint_configuration { 310 | types = ["EDGE"] 311 | } 312 | 313 | dynamic "mutual_tls_authentication" { 314 | for_each = length(keys(var.mutual_tls_authentication)) == 0 ? [] : [var.mutual_tls_authentication] 315 | 316 | content { 317 | truststore_uri = mutual_tls_authentication.value.truststore_uri 318 | truststore_version = try(mutual_tls_authentication.value.truststore_version, null) 319 | } 320 | } 321 | } 322 | 323 | resource "aws_api_gateway_base_path_mapping" "edge_iam" { 324 | count = var.create_api_domain_name && var.endpoint_type == "EDGE" && var.certificate_type == "IAM" && length(var.stage_names) > 0 ? length(var.stage_names) : 0 325 | 326 | api_id = aws_api_gateway_rest_api.this.id 327 | domain_name = aws_api_gateway_domain_name.edge_iam[count.index].id 328 | stage_name = aws_api_gateway_stage.this[count.index].stage_name 329 | base_path = var.domain_base_path 330 | } 331 | 332 | resource "aws_api_gateway_rest_api_policy" "this" { 333 | count = var.create_rest_api_policy && length(var.stage_names) > 0 ? length(var.stage_names) : 0 334 | 335 | rest_api_id = aws_api_gateway_rest_api.this.id 336 | policy = var.rest_api_policy 337 | } 338 | 339 | resource "aws_api_gateway_model" "this" { 340 | for_each = var.models 341 | rest_api_id = aws_api_gateway_rest_api.this.id 342 | name = each.key 343 | description = lookup(each.value,"description","") 344 | content_type = lookup(each.value,"content_type","") 345 | schema = lookup(each.value,"schema",{}) 346 | } 347 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "api_gateway_rest_api_arn" { 2 | description = "Arn of the REST API." 3 | value = aws_api_gateway_rest_api.this.arn 4 | } 5 | 6 | output "api_gateway_rest_api_name" { 7 | description = "Name of the REST API." 8 | value = aws_api_gateway_rest_api.this.name 9 | } 10 | 11 | output "api_gateway_rest_api_id" { 12 | description = "Id of the REST API." 13 | value = aws_api_gateway_rest_api.this.id 14 | } 15 | 16 | output "api_gateway_rest_api_execution_arn" { 17 | description = "Execution Arn of the REST API." 18 | value = aws_api_gateway_rest_api.this.execution_arn 19 | } 20 | 21 | output "api_gateway_rest_api_stage_arn" { 22 | description = "Arn of the deployed stage(s)." 23 | value = aws_api_gateway_stage.this[0].arn 24 | } 25 | 26 | output "api_gateway_rest_api_stage_id" { 27 | description = "Id of the deployed stage(s)." 28 | value = aws_api_gateway_stage.this[0].id 29 | } 30 | 31 | output "api_gateway_rest_api_stage_invoke_url" { 32 | description = "Invoke URL of the deployed stage(s)." 33 | value = aws_api_gateway_stage.this[0].invoke_url 34 | } 35 | 36 | output "api_gateway_rest_api_stage_execution_arn" { 37 | description = "Execution arn of the deployed stage(s)." 38 | value = aws_api_gateway_stage.this[0].execution_arn 39 | } 40 | 41 | output "api_gateway_rest_api_stage_web_acl" { 42 | description = "WAF Access Control List for the stage(s)" 43 | value = aws_api_gateway_stage.this[0].web_acl_arn 44 | } 45 | 46 | output "aws_api_gateway_domain_name" { 47 | description = "api gateway domain name" 48 | value = { for k, v in aws_api_gateway_domain_name.regional_acm : k => v } 49 | } 50 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "tags" { 2 | description = "A map of tags to assign to resources." 3 | type = map(string) 4 | default = {} 5 | } 6 | ##################### 7 | # REST API Variables 8 | ##################### 9 | variable "api_name" { 10 | description = "(Required) Name of the REST API. If importing an OpenAPI specification via the `body` argument, this corresponds to the `info.title` field. If the argument value is different than the OpenAPI value, the argument value will override the OpenAPI value." 11 | type = string 12 | default = null 13 | } 14 | variable "description" { 15 | description = "(Optional) Description of the REST API. If importing an OpenAPI specification via the `body` argument, this corresponds to the `info.description` field. If the argument value is provided and is different than the OpenAPI value, the argument value will override the OpenAPI value." 16 | type = string 17 | default = null 18 | } 19 | 20 | variable "openapi_definition" { 21 | description = "(Required) YAML formatted definition file using OpenAPI 3.x specification. This definition contains all API configuration inputs. Any inputs used in Terraform will override inputs in the definition." 22 | type = string 23 | } 24 | 25 | variable "put_rest_api_mode" { 26 | description = "(Optional) Mode of the PutRestApi operation when importing an OpenAPI specification via the body argument (create or update operation). Valid values are merge and overwrite. If unspecificed, defaults to overwrite (for backwards compatibility). This corresponds to the x-amazon-apigateway-put-integration-method extension. If the argument value is provided and is different than the OpenAPI value, the argument value will override the OpenAPI value." 27 | type = string 28 | default = "overwrite" 29 | } 30 | 31 | variable "endpoint_type" { 32 | description = "(Required) List of endpoint types. This resource currently only supports managing a single value. Valid values: `EDGE`, `REGIONAL` or `PRIVATE`. If unspecified, defaults to `EDGE`. Must be declared as `REGIONAL` in non-Commercial partitions. If set to `PRIVATE` recommend to set put_rest_api_mode = merge to not cause the endpoints and associated Route53 records to be deleted. Refer to the documentation for more information on the difference between edge-optimized and regional APIs." 33 | type = string 34 | default = "EDGE" 35 | } 36 | 37 | ############################# 38 | # API Gateway Stage Settings 39 | ############################# 40 | variable "stage_names" { 41 | description = "(Required) Name of the stage(s)." 42 | type = list(string) 43 | default = null 44 | } 45 | 46 | variable "stage_description" { 47 | description = "(Optional) Description of the stage." 48 | type = string 49 | default = null 50 | } 51 | 52 | variable "documentation_version" { 53 | description = "(Optional) Version of the associated API documentation." 54 | type = string 55 | default = null 56 | } 57 | 58 | variable "cache_cluster_enabled" { 59 | description = "(Optional) Whether a cache cluster is enabled for the stage." 60 | type = bool 61 | default = true 62 | } 63 | 64 | variable "cache_cluster_size" { 65 | description = "(Optional) Size of the cache cluster for the stage, if enabled. Allowed values include `0.5`, `1.6`, `6.1`, `13.5`, `28.4`, `58.2`, `118` and `237`." 66 | type = number 67 | default = 0.5 68 | } 69 | 70 | variable "percent_traffic" { 71 | description = "(Optional) Percent 0.0 - 100.0 of traffic to divert to the canary deployment." 72 | type = number 73 | default = null 74 | } 75 | 76 | variable "enable_canary" { 77 | description = "(Optional) Whether to use the values supplied for the canary and stage_variable_overrides or not." 78 | type = bool 79 | default = false 80 | } 81 | 82 | variable "stage_variable_overrides" { 83 | description = "(Optional) Map of overridden stage variables (including new variables) for the canary deployment." 84 | type = any 85 | default = {} 86 | } 87 | 88 | variable "use_stage_cache" { 89 | description = "(Optional) Whether the canary deployment uses the stage cache. Defaults to false." 90 | type = bool 91 | default = false 92 | } 93 | 94 | variable "client_certificate_id" { 95 | description = "(Optional) Identifier of a client certificate for the stage." 96 | type = string 97 | default = null 98 | } 99 | 100 | variable "stage_variables" { 101 | description = "(Optional) Map that defines the stage variables." 102 | type = any 103 | default = {} 104 | } 105 | 106 | variable "xray_tracing_enabled" { 107 | description = "(Optional) Whether active tracing with X-ray is enabled. Defaults to false." 108 | type = bool 109 | default = false 110 | } 111 | variable "access_log_format" { 112 | description = "(Required) Formatting and values recorded in the logs. For more information on configuring the log format rules visit the AWS documentation" 113 | type = string 114 | } 115 | 116 | variable "models" { 117 | description = "" 118 | type = any 119 | default = {} 120 | } 121 | 122 | ######################################## 123 | # API Gateway Method Settings Variables 124 | ######################################## 125 | variable "method_settings" { 126 | description = "Stage method settings" 127 | type = any 128 | default = {} 129 | } 130 | 131 | variable "metrics_enabled" { 132 | description = "(Optional) Whether Amazon CloudWatch metrics are enabled for this method." 133 | type = string 134 | default = true 135 | } 136 | 137 | variable "logging_level" { 138 | description = "(Optional) Logging level for this method, which effects the log entries pushed to Amazon CloudWatch Logs. The available levels are OFF, ERROR, and INFO." 139 | type = string 140 | default = "INFO" 141 | } 142 | 143 | variable "data_trace_enabled" { 144 | description = "(Optional) Whether data trace logging is enabled for this method, which effects the log entries pushed to Amazon CloudWatch Logs." 145 | type = bool 146 | default = false 147 | } 148 | 149 | variable "throttling_burst_limit" { 150 | description = "(Optional) Throttling burst limit. Default: -1 (throttling disabled)." 151 | type = number 152 | default = -1 153 | } 154 | 155 | variable "throttling_rate_limit" { 156 | description = "(Optional) Throttling rate limit. Default: -1 (throttling disabled)." 157 | type = number 158 | default = -1 159 | } 160 | 161 | variable "caching_enabled" { 162 | description = "(Optional) Whether responses should be cached and returned for requests. A cache cluster must be enabled on the stage for responses to be cached." 163 | type = bool 164 | default = true 165 | } 166 | 167 | variable "cache_ttl_in_seconds" { 168 | description = "(Optional) Time to live (TTL), in seconds, for cached responses. The higher the TTL, the longer the response will be cached." 169 | type = number 170 | default = 300 171 | } 172 | 173 | variable "cache_data_encrypted" { 174 | description = "(Optional) Whether the cached responses are encrypted." 175 | type = bool 176 | default = false 177 | } 178 | 179 | variable "require_authorization_for_cache_control" { 180 | description = "(Optional) Whether authorization is required for a cache invalidation request." 181 | type = bool 182 | default = true 183 | } 184 | 185 | variable "unauthorized_cache_control_header_strategy" { 186 | description = "(Optional) How to handle unauthorized requests for cache invalidation. The available values are `FAIL_WITH_403`, `SUCCEED_WITH_RESPONSE_HEADER`, `SUCCEED_WITHOUT_RESPONSE_HEADER`." 187 | type = string 188 | default = "SUCCEED_WITH_RESPONSE_HEADER" 189 | } 190 | 191 | ################################ 192 | # API Gateway API Key Variables 193 | ################################ 194 | variable "enable_api_key" { 195 | description = "(Optional) Whether the API key can be used by callers. Defaults to `false`." 196 | type = bool 197 | default = false 198 | } 199 | 200 | variable "api_key_name" { 201 | description = "(Required) Name of the API key." 202 | type = string 203 | default = null 204 | } 205 | 206 | variable "api_key_description" { 207 | description = "(Optional) API key description. Defaults to `Managed by Terraform`." 208 | type = string 209 | default = null 210 | } 211 | 212 | ################################### 213 | # API Gateway Usage Plan Variables 214 | ################################### 215 | variable "create_usage_plan" { 216 | description = "Allows creation of a usage plan. (Requires `var.enable_api_key = true`)" 217 | type = bool 218 | default = false 219 | } 220 | 221 | variable "usage_plans" { 222 | description = "Map of objects that define the usage plan to be created." 223 | type = list( 224 | object({ 225 | name = string 226 | description = string 227 | burst_limit = number 228 | rate_limit = number 229 | quota_limit = number 230 | quota_offset = number 231 | quota_period = string 232 | stages = list(string) 233 | }) 234 | ) 235 | default = [{ 236 | burst_limit = null 237 | description = null 238 | name = null 239 | quota_limit = null 240 | quota_offset = null 241 | quota_period = null 242 | rate_limit = null 243 | stages = [null] 244 | }] 245 | } 246 | 247 | variable "api_keys" { 248 | description = "Map of objects that define the usage plan to be created." 249 | type = list( 250 | object({ 251 | name = string 252 | key_type = string 253 | usage_plan = string 254 | }) 255 | ) 256 | default = [{ 257 | key_type = null 258 | name = null 259 | usage_plan = null 260 | }] 261 | } 262 | 263 | variable "client_name" { 264 | description = "client name to use this api." 265 | type = string 266 | default = null 267 | } 268 | 269 | variable "limit" { 270 | description = "(Optional) - Maximum number of requests that can be made in a given time period." 271 | type = number 272 | default = 20 273 | } 274 | 275 | variable "offset" { 276 | description = "(Optional) - Number of requests subtracted from the given limit in the initial time period." 277 | type = number 278 | default = 2 279 | } 280 | 281 | variable "period" { 282 | description = "(Optional) - Time period in which the limit applies. Valid values are `DAY`, `WEEK` or `MONTH`." 283 | type = string 284 | default = "WEEK" 285 | } 286 | 287 | variable "burst_limit" { 288 | description = "(Optional) - The API request burst limit, the maximum rate limit over a time ranging from one to a few seconds, depending upon whether the underlying token bucket is at its full capacity." 289 | type = number 290 | default = 5 291 | } 292 | 293 | variable "rate_limit" { 294 | description = "(Optional) - The API request steady-state rate limit." 295 | type = number 296 | default = 10 297 | } 298 | 299 | ############################## 300 | # API Gateway domain variables 301 | ############################## 302 | 303 | variable "create_api_domain_name" { 304 | description = "Whether to create API domain name resource." 305 | type = bool 306 | default = false 307 | } 308 | 309 | variable "domain_names" { 310 | description = "Fully-qualified domain name to register. The domain names to use for API gateway it will use the index of stage_names to select the domain name." 311 | type = list(string) 312 | default = null 313 | } 314 | 315 | variable "domain_base_path" { 316 | description = "(Optional) Path segment that must be prepended to the path when accessing the API via this mapping. If omitted, the API is exposed at the root of the given domain." 317 | type = string 318 | default = "/" 319 | } 320 | 321 | variable "domain_certificate_arn" { 322 | description = "The ARN of an AWS-managed certificate that will be used by the endpoint for the domain name." 323 | type = string 324 | default = null 325 | } 326 | 327 | variable "mutual_tls_authentication" { 328 | description = "An Amazon S3 URL that specifies the truststore for mutual TLS authentication as well as version, keyed at uri and version" 329 | type = map(string) 330 | default = {} 331 | } 332 | 333 | variable "certificate_type" { 334 | description = "This resource currently only supports managing a single value. Valid values: `ACM` or `IAM`. If unspecified, defaults to `acm`" 335 | type = string 336 | default = "ACM" 337 | } 338 | 339 | variable "domain_certificate_name" { 340 | description = "Unique name to use when registering this certificate as an IAM server certificate. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name. Required if certificate_arn is not set." 341 | type = string 342 | default = null 343 | } 344 | 345 | variable "iam_certificate_body" { 346 | description = "Certificate issued for the domain name being registered, in PEM format. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name" 347 | type = string 348 | default = null 349 | } 350 | 351 | variable "iam_certificate_chain" { 352 | description = "Certificate for the CA that issued the certificate, along with any intermediate CA certificates required to create an unbroken chain to a certificate trusted by the intended API clients. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name." 353 | type = string 354 | default = null 355 | } 356 | 357 | variable "iam_certificate_private_key" { 358 | description = "Private key associated with the domain certificate given in certificate_body. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name." 359 | type = string 360 | default = null 361 | } 362 | 363 | ############################## 364 | # REST API Resource Policy 365 | ############################## 366 | variable "create_rest_api_policy" { 367 | description = "Enables creation of the resource policy for a given API." 368 | type = bool 369 | default = true 370 | } 371 | 372 | variable "rest_api_policy" { 373 | description = "(Required) JSON formatted policy document that controls access to the API Gateway. For more information about building AWS IAM policy documents with Terraform, see the AWS IAM Policy Document Guide" 374 | type = string 375 | default = "" 376 | } 377 | 378 | ################ 379 | # WAF Variables 380 | ################ 381 | variable "enable_waf" { 382 | description = "Enables associating existing WAF ACL to all stages." 383 | type = bool 384 | default = false 385 | } 386 | variable "waf_acl" { 387 | description = "(Required) The ID of the WAF Regional WebACL to create an association." 388 | type = string 389 | default = null 390 | } 391 | 392 | ####################### 393 | # CloudWatch Variables 394 | ####################### 395 | variable "cloudwatch_role_arn" { 396 | description = "(Required) for the `api_gateway_account` resource." 397 | type = string 398 | default = null 399 | } 400 | 401 | variable "log_group_name" { 402 | description = "(Optional, Forces new resource) The name of the log group. If omitted, Terraform will assign a random, unique name." 403 | type = string 404 | default = null 405 | } 406 | 407 | variable "log_group_retention_in_days" { 408 | description = "(Optional) Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, and 0. If you select 0, the events in the log group are always retained and never expire." 409 | type = string 410 | default = 7 411 | } 412 | 413 | variable "log_group_kms_key" { 414 | description = "(Optional) The ARN of the KMS Key to use when encrypting log data. Please note, after the AWS KMS CMK is disassociated from the log group, AWS CloudWatch Logs stops encrypting newly ingested data for the log group. All previously ingested data remains encrypted, and AWS CloudWatch Logs requires permissions for the CMK whenever the encrypted data is requested." 415 | type = string 416 | default = null 417 | } 418 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.13.1" 3 | 4 | required_providers { 5 | aws = { 6 | source = "hashicorp/aws" 7 | version = ">= 5.5.0, < 5.75.0" 8 | } 9 | 10 | # Uncomment the lines below to generate the docs with the requirement 11 | # terraform-docs markdown table --output-file README.md --output-mode inject . 12 | # terragrunt = { 13 | # source = "gruntworks/terragrunt" 14 | # version = ">= 0.28.0" 15 | # } 16 | } 17 | } --------------------------------------------------------------------------------