Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Module - API Gateway #97

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
183 changes: 183 additions & 0 deletions modules/aws/api_gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<!-- Blank module readme template: Do a search and replace with your text editor for the following: `module_name`, `module_description` -->
<!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 -->
<a name="readme-top"></a>


<!-- PROJECT SHIELDS -->
<!--
*** I'm using markdown "reference style" links for readability.
*** Reference links are enclosed in brackets [ ] instead of parentheses ( ).
*** See the bottom of this document for the declaration of the reference variables
*** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use.
*** https://www.markdownguide.org/basic-syntax/#reference-style-links
-->
[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
[![LinkedIn][linkedin-shield]][linkedin-url]


<!-- PROJECT LOGO -->
<br />
<div align="center">
<a href="https://github.com/zachreborn/terraform-modules">
<img src="/images/terraform_modules_logo.webp" alt="Logo" width="300" height="300">
</a>

<h3 align="center">API Gateway</h3>
<p align="center">
This module creates an AWS API Gateway v2 configuration.
<br />
<a href="https://github.com/zachreborn/terraform-modules"><strong>Explore the docs »</strong></a>
<br />
<br />
<a href="https://zacharyhill.co">Zachary Hill</a>
·
<a href="https://github.com/zachreborn/terraform-modules/issues">Report Bug</a>
·
<a href="https://github.com/zachreborn/terraform-modules/issues">Request Feature</a>
</p>
</div>


<!-- TABLE OF CONTENTS -->
<details>
<summary>Table of Contents</summary>
<ol>
<li><a href="#usage">Usage</a></li>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#providers">Providers</a></li>
<li><a href="#modules">Modules</a></li>
<li><a href="#Resources">Resources</a></li>
<li><a href="#inputs">Inputs</a></li>
<li><a href="#outputs">Outputs</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#acknowledgments">Acknowledgments</a></li>
</ol>
</details>


<!-- USAGE EXAMPLES -->
## Usage
### Basic HTTP API Gateway Example
This example creates a basic HTTP API Gateway.
```
module "example_api_gateway" {
source = "github.com/zachreborn/terraform-modules//modules/aws/api_gateway"

name = "example-api"
protocol_type = "HTTP"
}
```

_For more examples, please refer to the [Documentation](https://github.com/zachreborn/terraform-modules)_

<p align="right">(<a href="#readme-top">back to top</a>)</p>

<!-- terraform-docs output will be input automatically below-->
<!-- terraform-docs markdown table --output-file README.md --output-mode inject .-->
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_apigatewayv2_api.api](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_api) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_api_gateway_version"></a> [api\_gateway\_version](#input\_api\_gateway\_version) | Version of the API Gateway | `string` | `null` | no |
| <a name="input_api_key_selection_expression"></a> [api\_key\_selection\_expression](#input\_api\_key\_selection\_expression) | API key selection expression for the API Gateway | `string` | `"$request.header.x-api-key"` | no |
| <a name="input_body"></a> [body](#input\_body) | OpenAPI specification for the API Gateway | `string` | `null` | no |
| <a name="input_cors_configuration"></a> [cors\_configuration](#input\_cors\_configuration) | CORS configuration for the API Gateway | <pre>object({<br/> allow_credentials = bool<br/> allow_headers = list(string)<br/> allow_methods = list(string)<br/> allow_origins = list(string)<br/> expose_headers = list(string)<br/> max_age = number<br/> })</pre> | <pre>{<br/> "allow_credentials": false,<br/> "allow_headers": [],<br/> "allow_methods": [],<br/> "allow_origins": [],<br/> "expose_headers": [],<br/> "max_age": 0<br/>}</pre> | no |
| <a name="input_credentials_arn"></a> [credentials\_arn](#input\_credentials\_arn) | ARN of the credentials for the API Gateway | `string` | `null` | no |
| <a name="input_description"></a> [description](#input\_description) | Description of the API Gateway | `string` | `null` | no |
| <a name="input_disable_execute_api_endpoint"></a> [disable\_execute\_api\_endpoint](#input\_disable\_execute\_api\_endpoint) | Whether to disable the execute-api endpoint | `bool` | `false` | no |
| <a name="input_fail_on_warnings"></a> [fail\_on\_warnings](#input\_fail\_on\_warnings) | Whether to fail on warnings during API Gateway creation | `bool` | `false` | no |
| <a name="input_name"></a> [name](#input\_name) | Name of the API Gateway | `string` | n/a | yes |
| <a name="input_protocol_type"></a> [protocol\_type](#input\_protocol\_type) | Protocol type of the API Gateway (HTTP or WEBSOCKET) | `string` | n/a | yes |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to apply to the API Gateway | `map(string)` | `{}` | no |
| <a name="input_target"></a> [target](#input\_target) | Target for the API Gateway | `string` | `null` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_api_endpoint"></a> [api\_endpoint](#output\_api\_endpoint) | The URI of the API |
| <a name="output_api_key_selection_expression"></a> [api\_key\_selection\_expression](#output\_api\_key\_selection\_expression) | The API key selection expression for the API |
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the API |
| <a name="output_cors_configuration"></a> [cors\_configuration](#output\_cors\_configuration) | The CORS configuration for the API |
| <a name="output_execution_arn"></a> [execution\_arn](#output\_execution\_arn) | The ARN prefix to be used in permission policies |
| <a name="output_id"></a> [id](#output\_id) | The API identifier |
| <a name="output_tags_all"></a> [tags\_all](#output\_tags\_all) | Map of tags assigned to the resource |
<!-- END_TF_DOCS -->

<!-- LICENSE -->
## License

Distributed under the MIT License. See `LICENSE.txt` for more information.

<p align="right">(<a href="#readme-top">back to top</a>)</p>



<!-- CONTACT -->
## Contact

Zachary Hill - [![LinkedIn][linkedin-shield]][linkedin-url] - [email protected]

Project Link: [https://github.com/zachreborn/terraform-modules](https://github.com/zachreborn/terraform-modules)

<p align="right">(<a href="#readme-top">back to top</a>)</p>



<!-- ACKNOWLEDGMENTS -->
## Acknowledgments

* [Zachary Hill](https://zacharyhill.co)
* [Jake Jones](https://github.com/jakeasarus)

<p align="right">(<a href="#readme-top">back to top</a>)</p>


<!-- MARKDOWN LINKS & IMAGES -->
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
[contributors-shield]: https://img.shields.io/github/contributors/zachreborn/terraform-modules.svg?style=for-the-badge
[contributors-url]: https://github.com/zachreborn/terraform-modules/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/zachreborn/terraform-modules.svg?style=for-the-badge
[forks-url]: https://github.com/zachreborn/terraform-modules/network/members
[stars-shield]: https://img.shields.io/github/stars/zachreborn/terraform-modules.svg?style=for-the-badge
[stars-url]: https://github.com/zachreborn/terraform-modules/stargazers
[issues-shield]: https://img.shields.io/github/issues/zachreborn/terraform-modules.svg?style=for-the-badge
[issues-url]: https://github.com/zachreborn/terraform-modules/issues
[license-shield]: https://img.shields.io/github/license/zachreborn/terraform-modules.svg?style=for-the-badge
[license-url]: https://github.com/zachreborn/terraform-modules/blob/master/LICENSE.txt
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
[linkedin-url]: https://www.linkedin.com/in/zachary-hill-5524257a/
[product-screenshot]: /images/screenshot.webp
[Terraform.io]: https://img.shields.io/badge/Terraform-7B42BC?style=for-the-badge&logo=terraform
[Terraform-url]: https://terraform.io
48 changes: 48 additions & 0 deletions modules/aws/api_gateway/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
terraform {
required_version = ">= 1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0.0"
}
}
}

###########################
# Data Sources
###########################
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

#############################
# API Gateway
#############################
resource "aws_apigatewayv2_api" "api" {
#Required
name = var.name
protocol_type = var.protocol_type
route_selection_expression = "$request.method $request.path"

#Optional
api_key_selection_expression = var.api_key_selection_expression
cors_configuration {
allow_credentials = lookup(var.cors_configuration, "allow_credentials", null)
allow_headers = lookup(var.cors_configuration, "allow_headers", null)
allow_methods = lookup(var.cors_configuration, "allow_methods", null)
allow_origins = lookup(var.cors_configuration, "allow_origins", null)
expose_headers = lookup(var.cors_configuration, "expose_headers", null)
max_age = lookup(var.cors_configuration, "max_age", null)
}
credentials_arn = var.credentials_arn
description = var.description
disable_execute_api_endpoint = var.disable_execute_api_endpoint
fail_on_warnings = var.fail_on_warnings
tags = var.tags
target = var.target
version = var.api_gateway_version
body = var.body

lifecycle {
ignore_changes = [body]
}
}
38 changes: 38 additions & 0 deletions modules/aws/api_gateway/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#####################
# Outputs
#####################

output "id" {
description = "The API identifier"
value = aws_apigatewayv2_api.api.id
}

output "api_endpoint" {
description = "The URI of the API"
value = aws_apigatewayv2_api.api.api_endpoint
}

output "arn" {
description = "The ARN of the API"
value = aws_apigatewayv2_api.api.arn
}

output "execution_arn" {
description = "The ARN prefix to be used in permission policies"
value = aws_apigatewayv2_api.api.execution_arn
}

output "api_key_selection_expression" {
description = "The API key selection expression for the API"
value = aws_apigatewayv2_api.api.api_key_selection_expression
}

output "cors_configuration" {
description = "The CORS configuration for the API"
value = aws_apigatewayv2_api.api.cors_configuration
}

output "tags_all" {
description = "Map of tags assigned to the resource"
value = aws_apigatewayv2_api.api.tags_all
}
91 changes: 91 additions & 0 deletions modules/aws/api_gateway/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
############################################
# Required
############################################

variable "name" {
description = "Name of the API Gateway"
type = string
}

variable "protocol_type" {
description = "Protocol type of the API Gateway (HTTP or WEBSOCKET)"
type = string
}

############################################
# Optional
############################################

variable "api_key_selection_expression" {
description = "API key selection expression for the API Gateway"
type = string
default = "$request.header.x-api-key"
}

variable "cors_configuration" {
description = "CORS configuration for the API Gateway"
type = object({
allow_credentials = bool
allow_headers = list(string)
allow_methods = list(string)
allow_origins = list(string)
expose_headers = list(string)
max_age = number
})
default = {
allow_credentials = false
allow_headers = []
allow_methods = []
allow_origins = []
expose_headers = []
max_age = 0
}
}

variable "credentials_arn" {
description = "ARN of the credentials for the API Gateway"
type = string
default = null
}

variable "description" {
description = "Description of the API Gateway"
type = string
default = null
}

variable "disable_execute_api_endpoint" {
description = "Whether to disable the execute-api endpoint"
type = bool
default = false
}

variable "fail_on_warnings" {
description = "Whether to fail on warnings during API Gateway creation"
type = bool
default = false
}

variable "tags" {
description = "Tags to apply to the API Gateway"
type = map(string)
default = {}
}

variable "target" {
description = "Target for the API Gateway"
type = string
default = null
}

variable "api_gateway_version" {
description = "Version of the API Gateway"
type = string
default = null
}

variable "body" {
description = "OpenAPI specification for the API Gateway"
type = string
default = null
}