Skip to content

Commit

Permalink
feat: API Caching (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenlito authored Feb 14, 2022
1 parent 9f04aa7 commit 4ab6567
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 6 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ $ terraform apply
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.70 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |

## Providers

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

## Modules

Expand All @@ -129,6 +129,7 @@ No modules.

| Name | Type |
|------|------|
| [aws_appsync_api_cache.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_api_cache) | resource |
| [aws_appsync_api_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_api_key) | resource |
| [aws_appsync_datasource.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_datasource) | resource |
| [aws_appsync_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_function) | resource |
Expand All @@ -149,6 +150,12 @@ No modules.
| <a name="input_additional_authentication_provider"></a> [additional\_authentication\_provider](#input\_additional\_authentication\_provider) | One or more additional authentication providers for the GraphqlApi. | `any` | `{}` | no |
| <a name="input_api_keys"></a> [api\_keys](#input\_api\_keys) | Map of API keys to create | `map(string)` | `{}` | no |
| <a name="input_authentication_type"></a> [authentication\_type](#input\_authentication\_type) | The authentication type to use by GraphQL API | `string` | `"API_KEY"` | no |
| <a name="input_cache_at_rest_encryption_enabled"></a> [cache\_at\_rest\_encryption\_enabled](#input\_cache\_at\_rest\_encryption\_enabled) | At-rest encryption flag for cache. | `bool` | `false` | no |
| <a name="input_cache_transit_encryption_enabled"></a> [cache\_transit\_encryption\_enabled](#input\_cache\_transit\_encryption\_enabled) | Transit encryption flag when connecting to cache. | `bool` | `false` | no |
| <a name="input_cache_ttl"></a> [cache\_ttl](#input\_cache\_ttl) | TTL in seconds for cache entries | `number` | `1` | no |
| <a name="input_cache_type"></a> [cache\_type](#input\_cache\_type) | The cache instance type. | `string` | `"SMALL"` | no |
| <a name="input_caching_behavior"></a> [caching\_behavior](#input\_caching\_behavior) | Caching behavior. | `string` | `"FULL_REQUEST_CACHING"` | no |
| <a name="input_caching_enabled"></a> [caching\_enabled](#input\_caching\_enabled) | Whether caching with Elasticache is enabled. | `bool` | `false` | no |
| <a name="input_create_graphql_api"></a> [create\_graphql\_api](#input\_create\_graphql\_api) | Whether to create GraphQL API | `bool` | `true` | no |
| <a name="input_create_logs_role"></a> [create\_logs\_role](#input\_create\_logs\_role) | Whether to create service role for Cloudwatch logs | `bool` | `true` | no |
| <a name="input_datasources"></a> [datasources](#input\_datasources) | Map of datasources to create | `any` | `{}` | no |
Expand Down
4 changes: 2 additions & 2 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.46 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.46 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.73 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |

## Modules
Expand Down
8 changes: 8 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ module "appsync" {

schema = file("schema.graphql")

caching_enabled = true

caching_behavior = "PER_RESOLVER_CACHING"
cache_type = "SMALL"
cache_ttl = 60
cache_at_rest_encryption_enabled = true
cache_transit_encryption_enabled = true

api_keys = {
future = "2021-08-20T15:00:00Z"
default = null
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 2.46"
version = ">= 3.73"
}
random = {
source = "hashicorp/random"
Expand Down
13 changes: 13 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ resource "aws_appsync_graphql_api" "this" {
tags = merge({ Name = var.name }, var.graphql_api_tags)
}

# API Cache
resource "aws_appsync_api_cache" "example" {
count = var.create_graphql_api && var.caching_enabled ? 1 : 0

api_id = aws_appsync_graphql_api.this[0].id

api_caching_behavior = var.caching_behavior
type = var.cache_type
ttl = var.cache_ttl
at_rest_encryption_enabled = var.cache_at_rest_encryption_enabled
transit_encryption_enabled = var.cache_transit_encryption_enabled
}

# API Key
resource "aws_appsync_api_key" "this" {
for_each = var.create_graphql_api && var.authentication_type == "API_KEY" ? var.api_keys : {}
Expand Down
66 changes: 66 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ variable "logging_enabled" {
default = false
}

variable "caching_enabled" {
description = "Whether caching with Elasticache is enabled."
type = bool
default = false
}

variable "xray_enabled" {
description = "Whether tracing with X-ray is enabled."
type = bool
Expand Down Expand Up @@ -106,6 +112,66 @@ variable "tags" {
default = {}
}

# API Cache
variable "caching_behavior" {
description = "Caching behavior."
type = string
default = "FULL_REQUEST_CACHING"

validation {
condition = contains([
"FULL_REQUEST_CACHING",
"PER_RESOLVER_CACHING"
], var.caching_behavior)
error_message = "Allowed values for input_parameter are \"FULL_REQUEST_CACHING\", or \"PER_RESOLVER_CACHING\"."
}
}

variable "cache_type" {
description = "The cache instance type."
type = string
default = "SMALL"

validation {
condition = contains([
"SMALL",
"MEDIUM",
"LARGE",
"XLARGE",
"LARGE_2X",
"LARGE_4X",
"LARGE_8X",
"LARGE_12X",
"T2_SMALL",
"T2_MEDIUM",
"R4_LARGE",
"R4_XLARGE",
"R4_2XLARGE",
"R4_4XLARGE",
"R4_8XLARGE"
], var.cache_type)
error_message = "Allowed values for input_parameter are \"SMALL\", \"MEDIUM\", \"LARGE\", \"XLARGE\", \"LARGE_2X\", \"LARGE_4X\", \"LARGE_8X\", \"LARGE_12X\", \"T2_SMALL\", \"T2_MEDIUM\", \"R4_LARGE\", \"R4_XLARGE\", \"R4_2XLARGE\", \"R4_4XLARGE\", or \"R4_8XLARGE\"."
}
}

variable "cache_ttl" {
description = "TTL in seconds for cache entries"
type = number
default = 1
}

variable "cache_at_rest_encryption_enabled" {
description = "At-rest encryption flag for cache."
type = bool
default = false
}

variable "cache_transit_encryption_enabled" {
description = "Transit encryption flag when connecting to cache."
type = bool
default = false
}

# API Keys
variable "api_keys" {
description = "Map of API keys to create"
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.70"
version = ">= 3.73"
}
random = {
source = "hashicorp/random"
Expand Down

0 comments on commit 4ab6567

Please sign in to comment.