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

Adding APIGateway module #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
# terraform-aws-apigateway
# API Gateway

=====================================

[![Opstree Solutions][opstree_avatar]][opstree_homepage]

[Opstree Solutions][opstree_homepage]

[opstree_homepage]: https://opstree.github.io/
[opstree_avatar]: https://img.cloudposse.com/150x150/https://github.com/opstree.png

- This terraform module will create a API Gateway.
- This project is a part of opstree's ot-aws initiative for terraform modules.


## Usage

```
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.44.0"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}

## Local tags are used to define common tags.
locals {
tags = { "Environment" : "test", "Client" : "DevOps", "Project" : "Demo", "Organisation" : "opstree" }
}

#Create API .
module "APIGateway" {
source = "./"
rest_api_name = var.rest_api_name
binary_media_types = ["image/jpeg", "image/png"]
description = var.description

types = ["EDGE"]
enable_websocket_api = false
enable_rest_api = true
}

```

## Inputs

| Name | Description | Type | Default | Required | Supported |
|------|-------------|:----:|---------|:--------:|:---------:|
| rest_api_name | Name of the API Gateway created. | `string` | | yes | |
| enable_rest_api | To create api gateway. | `bool` | | yes | |
| types | List of endpoint types. This resource currently only supports managing a single value | `string` | | yes | |
| description | Description of the REST API.| `string` | | |
| binary_media_types | List of binary media types supported by the REST API. | `string` | | yes | |
| websocket_api_name | Name of the WebSocket API | `string` | | |
| websocket_api_protocol | Type of the WebSocket API Protocol. |`string` | `WEBSOCKET` | yes | |
| websocket_api_route_selection_expression | WebSocket API Route Selection Eexpression. | `string` | yes | |
| enable_websocket_api | To enble websocket. | `bool` | `true/false` |
#

## Outputs

| Name | Description |
|------|-------------|
| api_gateway_id | The ID of API GateWay. |
| root_resource_id | Set to the ID of the API Gateway Resource on the found REST API where the route matches '/'. |
| websocket_api_id | The ID of the WebSocket API. |


#
## Contributors
| [![Reena Nain][Reena_avatar]][Reena_homepage]<br/>[Reena Nain][Reena_homepage] |


[reena_homepage]: https://gitlab.com/reena.nain
[reena_avatar]: https://gitlab.com/uploads/-/system/user/avatar/9292330/avatar.png?width=400
16 changes: 16 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "aws_api_gateway_rest_api" "rest_api" {
count = var.enable_rest_api == true ? 1 : 0
name = var.rest_api_name
binary_media_types = var.binary_media_types
description = var.description
endpoint_configuration {
types = var.types
}
}

resource "aws_apigatewayv2_api" "websocket_api" {
count = var.enable_websocket_api == true ? 1 : 0
name = var.websocket_api_name
protocol_type = var.websocket_api_protocol
route_selection_expression = var.websocket_api_route_selection_expression
}
14 changes: 14 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "api_gateway_id" {
description = "The ID of API GateWay"
value = var.enable_rest_api == true ? aws_api_gateway_rest_api.rest_api.*.id[0] : 0
}

output "root_resource_id" {
description = "Set to the ID of the API Gateway Resource on the found REST API where the route matches '/'."
value = var.enable_rest_api == true ? aws_api_gateway_rest_api.rest_api.*.root_resource_id[0] : 0
}

output "websocket_api_id" {
description = "The ID of the WebSocket API"
value = var.enable_websocket_api == true ? aws_apigatewayv2_api.websocket_api.*.id[0] : 0
}
49 changes: 49 additions & 0 deletions variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
variable "rest_api_name" {
type = string
description = "Name of the API Gateway created"
default = "terraform-api-gateway-example"
}

variable "enable_rest_api" {
type = bool
default = true
}

variable "types" {
description = "List of endpoint types."
default = ["EDGE"]
}

variable "description" {
description = "Description of the REST API."
default = "API Service"
}

variable "binary_media_types" {
description = "List of binary media types supported by the REST API."
default = ["image/jpeg", ]
}

variable "websocket_api_name" {
type = string
description = "Name of the WebSocket API"
default = "default"
}

variable "websocket_api_protocol" {
type = string
description = "Type of the WebSocket API Protocol"
default = "WEBSOCKET"
}

variable "websocket_api_route_selection_expression" {
type = string
description = "WebSocket API Route Selection Eexpression"
default = "default"
}

variable "enable_websocket_api" {
type = bool
default = false
}