Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rpstreef committed Feb 12, 2020
1 parent cd57ab6 commit 2f8d443
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"terraform.languageServer": {
"enabled": true
},
"terraform.indexing": {
"enabled": false,
"liveIndexing": false,
"delay": 500,
"exclude": [
".terraform/**/*",
"**/.terraform/**/*"
]
}
}
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# tf-apigateway
Terraform API Gateway Module
# Terraform API Gateway Module

## About:

Supports basic setup of API Gateway with an OpenAPI 3 YML document:

These parameters configure the use of the OpenAPI 3 document:
- __api_template__: API Gateway OpenAPI 3 template file
- __api_template_vars__: Variables required in the OpenAPI template file

## How to use:

Call the module:

```terraform
# -----------------------------------------------------------------------------
# API Gateway
# -----------------------------------------------------------------------------
module "apigateway" {
source = "github.com/rpstreef/tf-apigateway"
resource_tag_name = var.resource_tag_name
namespace = var.namespace
region = var.region
api_name = local.api_name
api_throttling_rate_limit = var.api_throttling_rate_limit
api_throttling_burst_limit = var.api_throttling_burst_limit
api_metrics_enabled = var.api_metrics_enabled
api_logging_level = var.api_logging_level
api_template = file("../../services/api/${local.api_name}.yml")
api_template_vars = {
region = var.region
cognito_user_pool_arn = module.cognito.cognito_user_pool_arn
lambda_identity_arn = module.identity.lambda_arn
lambda_identity_timeout = var.lambda_identity_api_timeout
lambda_user_arn = module.user.lambda_arn
lambda_user_timeout = var.lambda_user_api_timeout
}
lambda_zip_name = local.lambda_zip_name
dist_file_path = local.dist_file_path
}
```
55 changes: 55 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
locals {
resource_name_prefix = "${var.namespace}-${var.resource_tag_name}"
api_url = "${aws_api_gateway_deployment._.invoke_url}${aws_api_gateway_stage._.stage_name}"
api_name = "${local.resource_name_prefix}-${var.api_name}"
}

data "template_file" "_" {
template = var.api_template

vars = var.api_template_vars
}

resource "aws_api_gateway_rest_api" "_" {
name = local.api_name
api_key_source = "HEADER"

body = data.template_file._.rendered
}

resource "aws_api_gateway_deployment" "_" {
rest_api_id = aws_api_gateway_rest_api._.id
stage_name = ""

lifecycle {
create_before_destroy = true
}

variables = {
"source_code_hash" = filebase64sha256("${var.dist_file_path}/${var.lambda_zip_name}")
}
}

resource "aws_api_gateway_stage" "_" {
stage_name = var.namespace
rest_api_id = aws_api_gateway_rest_api._.id
deployment_id = aws_api_gateway_deployment._.id

tags = {
Environment = var.namespace
Name = var.resource_tag_name
}
}

resource "aws_api_gateway_method_settings" "_" {
rest_api_id = aws_api_gateway_rest_api._.id
stage_name = aws_api_gateway_stage._.stage_name
method_path = "*/*"

settings {
throttling_burst_limit = var.api_throttling_burst_limit
throttling_rate_limit = var.api_throttling_rate_limit
metrics_enabled = var.api_metrics_enabled
logging_level = var.api_logging_level
}
}
19 changes: 19 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "deployment_execution_arn" {
value = aws_api_gateway_deployment._.execution_arn
}

output "rest_api_id" {
value = aws_api_gateway_rest_api._.id
}

output "api_url" {
value = local.api_url
}

output "api_name" {
value = local.api_name
}

output "api_stage" {
value = aws_api_gateway_stage._.stage_name
}
62 changes: 62 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -----------------------------------------------------------------------------
# Variables: General
# -----------------------------------------------------------------------------

variable "namespace" {
description = "AWS resource namespace/prefix"
}

variable "region" {
description = "AWS region"
}

variable "resource_tag_name" {
description = "Resource name for billing purposes"
}

# -----------------------------------------------------------------------------
# Variables: API Gateway
# -----------------------------------------------------------------------------
variable "api_name" {
description = "API Gateway endpoint name"
}

variable "api_template" {
description = "API Gateway OpenAPI 3 template file"
}

variable "api_template_vars" {
description = "Variables required in the OpenAPI template file"
type = map
}

variable "api_throttling_rate_limit" {
description = "API Gateway total requests across all API's within a REST endpoint"
}

variable "api_throttling_burst_limit" {
description = "API Gateway total concurrent connections allowed for all API's within a REST endpoint"
}

variable "api_metrics_enabled" {
description = "Enables detailed API Gateway metrics"
type = bool
default = false
}

variable "api_logging_level" {
description = " (Optional) Specifies the logging level for this method, which effects the log entries pushed to Amazon CloudWatch Logs. The available levels are OFF, ERROR, and INFO."
type = string
default = "OFF"
}

# -----------------------------------------------------------------------------
# Variables: Misc.
# -----------------------------------------------------------------------------
variable "lambda_zip_name" {
description = "Lambda ZIP source code filename"
}

variable "dist_file_path" {
description = "File path to the Lambda ZIP source code"
}

0 comments on commit 2f8d443

Please sign in to comment.