From 4ab656775e6c9c2d8130a51e811cc1a332b639eb Mon Sep 17 00:00:00 2001
From: Sven Lito <57947418+svenlito@users.noreply.github.com>
Date: Mon, 14 Feb 2022 19:11:50 +0700
Subject: [PATCH] feat: API Caching (#29)
---
README.md | 11 ++++--
examples/complete/README.md | 4 +--
examples/complete/main.tf | 8 +++++
examples/complete/versions.tf | 2 +-
main.tf | 13 +++++++
variables.tf | 66 +++++++++++++++++++++++++++++++++++
versions.tf | 2 +-
7 files changed, 100 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 6897216..89363a2 100644
--- a/README.md
+++ b/README.md
@@ -112,14 +112,14 @@ $ terraform apply
| Name | Version |
|------|---------|
| [terraform](#requirement\_terraform) | >= 0.13.1 |
-| [aws](#requirement\_aws) | >= 3.70 |
+| [aws](#requirement\_aws) | >= 3.73 |
| [random](#requirement\_random) | >= 2.0 |
## Providers
| Name | Version |
|------|---------|
-| [aws](#provider\_aws) | >= 3.70 |
+| [aws](#provider\_aws) | >= 3.73 |
## Modules
@@ -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 |
@@ -149,6 +150,12 @@ No modules.
| [additional\_authentication\_provider](#input\_additional\_authentication\_provider) | One or more additional authentication providers for the GraphqlApi. | `any` | `{}` | no |
| [api\_keys](#input\_api\_keys) | Map of API keys to create | `map(string)` | `{}` | no |
| [authentication\_type](#input\_authentication\_type) | The authentication type to use by GraphQL API | `string` | `"API_KEY"` | no |
+| [cache\_at\_rest\_encryption\_enabled](#input\_cache\_at\_rest\_encryption\_enabled) | At-rest encryption flag for cache. | `bool` | `false` | no |
+| [cache\_transit\_encryption\_enabled](#input\_cache\_transit\_encryption\_enabled) | Transit encryption flag when connecting to cache. | `bool` | `false` | no |
+| [cache\_ttl](#input\_cache\_ttl) | TTL in seconds for cache entries | `number` | `1` | no |
+| [cache\_type](#input\_cache\_type) | The cache instance type. | `string` | `"SMALL"` | no |
+| [caching\_behavior](#input\_caching\_behavior) | Caching behavior. | `string` | `"FULL_REQUEST_CACHING"` | no |
+| [caching\_enabled](#input\_caching\_enabled) | Whether caching with Elasticache is enabled. | `bool` | `false` | no |
| [create\_graphql\_api](#input\_create\_graphql\_api) | Whether to create GraphQL API | `bool` | `true` | no |
| [create\_logs\_role](#input\_create\_logs\_role) | Whether to create service role for Cloudwatch logs | `bool` | `true` | no |
| [datasources](#input\_datasources) | Map of datasources to create | `any` | `{}` | no |
diff --git a/examples/complete/README.md b/examples/complete/README.md
index 409fd26..bb72f0f 100644
--- a/examples/complete/README.md
+++ b/examples/complete/README.md
@@ -21,14 +21,14 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version |
|------|---------|
| [terraform](#requirement\_terraform) | >= 0.13.1 |
-| [aws](#requirement\_aws) | >= 2.46 |
+| [aws](#requirement\_aws) | >= 3.73 |
| [random](#requirement\_random) | >= 2.0 |
## Providers
| Name | Version |
|------|---------|
-| [aws](#provider\_aws) | >= 2.46 |
+| [aws](#provider\_aws) | >= 3.73 |
| [random](#provider\_random) | >= 2.0 |
## Modules
diff --git a/examples/complete/main.tf b/examples/complete/main.tf
index de8bfcd..1982a58 100644
--- a/examples/complete/main.tf
+++ b/examples/complete/main.tf
@@ -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
diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf
index 6392d45..9ce6d56 100644
--- a/examples/complete/versions.tf
+++ b/examples/complete/versions.tf
@@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
- version = ">= 2.46"
+ version = ">= 3.73"
}
random = {
source = "hashicorp/random"
diff --git a/main.tf b/main.tf
index 69ed34b..755e66a 100644
--- a/main.tf
+++ b/main.tf
@@ -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 : {}
diff --git a/variables.tf b/variables.tf
index 30a59ba..bfdc486 100644
--- a/variables.tf
+++ b/variables.tf
@@ -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
@@ -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"
diff --git a/versions.tf b/versions.tf
index 3b787ed..9ce6d56 100644
--- a/versions.tf
+++ b/versions.tf
@@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
- version = ">= 3.70"
+ version = ">= 3.73"
}
random = {
source = "hashicorp/random"