Skip to content

Commit 4ab6567

Browse files
authored
feat: API Caching (#29)
1 parent 9f04aa7 commit 4ab6567

File tree

7 files changed

+100
-6
lines changed

7 files changed

+100
-6
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ $ terraform apply
112112
| Name | Version |
113113
|------|---------|
114114
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
115-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.70 |
115+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
116116
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
117117

118118
## Providers
119119

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

124124
## Modules
125125

@@ -129,6 +129,7 @@ No modules.
129129

130130
| Name | Type |
131131
|------|------|
132+
| [aws_appsync_api_cache.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_api_cache) | resource |
132133
| [aws_appsync_api_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_api_key) | resource |
133134
| [aws_appsync_datasource.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_datasource) | resource |
134135
| [aws_appsync_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appsync_function) | resource |
@@ -149,6 +150,12 @@ No modules.
149150
| <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 |
150151
| <a name="input_api_keys"></a> [api\_keys](#input\_api\_keys) | Map of API keys to create | `map(string)` | `{}` | no |
151152
| <a name="input_authentication_type"></a> [authentication\_type](#input\_authentication\_type) | The authentication type to use by GraphQL API | `string` | `"API_KEY"` | no |
153+
| <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 |
154+
| <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 |
155+
| <a name="input_cache_ttl"></a> [cache\_ttl](#input\_cache\_ttl) | TTL in seconds for cache entries | `number` | `1` | no |
156+
| <a name="input_cache_type"></a> [cache\_type](#input\_cache\_type) | The cache instance type. | `string` | `"SMALL"` | no |
157+
| <a name="input_caching_behavior"></a> [caching\_behavior](#input\_caching\_behavior) | Caching behavior. | `string` | `"FULL_REQUEST_CACHING"` | no |
158+
| <a name="input_caching_enabled"></a> [caching\_enabled](#input\_caching\_enabled) | Whether caching with Elasticache is enabled. | `bool` | `false` | no |
152159
| <a name="input_create_graphql_api"></a> [create\_graphql\_api](#input\_create\_graphql\_api) | Whether to create GraphQL API | `bool` | `true` | no |
153160
| <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 |
154161
| <a name="input_datasources"></a> [datasources](#input\_datasources) | Map of datasources to create | `any` | `{}` | no |

examples/complete/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Note that this example may create resources which cost money. Run `terraform des
2121
| Name | Version |
2222
|------|---------|
2323
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
24-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.46 |
24+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
2525
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2626

2727
## Providers
2828

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

3434
## Modules

examples/complete/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ module "appsync" {
1818

1919
schema = file("schema.graphql")
2020

21+
caching_enabled = true
22+
23+
caching_behavior = "PER_RESOLVER_CACHING"
24+
cache_type = "SMALL"
25+
cache_ttl = 60
26+
cache_at_rest_encryption_enabled = true
27+
cache_transit_encryption_enabled = true
28+
2129
api_keys = {
2230
future = "2021-08-20T15:00:00Z"
2331
default = null

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 2.46"
7+
version = ">= 3.73"
88
}
99
random = {
1010
source = "hashicorp/random"

main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ resource "aws_appsync_graphql_api" "this" {
8888
tags = merge({ Name = var.name }, var.graphql_api_tags)
8989
}
9090

91+
# API Cache
92+
resource "aws_appsync_api_cache" "example" {
93+
count = var.create_graphql_api && var.caching_enabled ? 1 : 0
94+
95+
api_id = aws_appsync_graphql_api.this[0].id
96+
97+
api_caching_behavior = var.caching_behavior
98+
type = var.cache_type
99+
ttl = var.cache_ttl
100+
at_rest_encryption_enabled = var.cache_at_rest_encryption_enabled
101+
transit_encryption_enabled = var.cache_transit_encryption_enabled
102+
}
103+
91104
# API Key
92105
resource "aws_appsync_api_key" "this" {
93106
for_each = var.create_graphql_api && var.authentication_type == "API_KEY" ? var.api_keys : {}

variables.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "logging_enabled" {
1010
default = false
1111
}
1212

13+
variable "caching_enabled" {
14+
description = "Whether caching with Elasticache is enabled."
15+
type = bool
16+
default = false
17+
}
18+
1319
variable "xray_enabled" {
1420
description = "Whether tracing with X-ray is enabled."
1521
type = bool
@@ -106,6 +112,66 @@ variable "tags" {
106112
default = {}
107113
}
108114

115+
# API Cache
116+
variable "caching_behavior" {
117+
description = "Caching behavior."
118+
type = string
119+
default = "FULL_REQUEST_CACHING"
120+
121+
validation {
122+
condition = contains([
123+
"FULL_REQUEST_CACHING",
124+
"PER_RESOLVER_CACHING"
125+
], var.caching_behavior)
126+
error_message = "Allowed values for input_parameter are \"FULL_REQUEST_CACHING\", or \"PER_RESOLVER_CACHING\"."
127+
}
128+
}
129+
130+
variable "cache_type" {
131+
description = "The cache instance type."
132+
type = string
133+
default = "SMALL"
134+
135+
validation {
136+
condition = contains([
137+
"SMALL",
138+
"MEDIUM",
139+
"LARGE",
140+
"XLARGE",
141+
"LARGE_2X",
142+
"LARGE_4X",
143+
"LARGE_8X",
144+
"LARGE_12X",
145+
"T2_SMALL",
146+
"T2_MEDIUM",
147+
"R4_LARGE",
148+
"R4_XLARGE",
149+
"R4_2XLARGE",
150+
"R4_4XLARGE",
151+
"R4_8XLARGE"
152+
], var.cache_type)
153+
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\"."
154+
}
155+
}
156+
157+
variable "cache_ttl" {
158+
description = "TTL in seconds for cache entries"
159+
type = number
160+
default = 1
161+
}
162+
163+
variable "cache_at_rest_encryption_enabled" {
164+
description = "At-rest encryption flag for cache."
165+
type = bool
166+
default = false
167+
}
168+
169+
variable "cache_transit_encryption_enabled" {
170+
description = "Transit encryption flag when connecting to cache."
171+
type = bool
172+
default = false
173+
}
174+
109175
# API Keys
110176
variable "api_keys" {
111177
description = "Map of API keys to create"

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.70"
7+
version = ">= 3.73"
88
}
99
random = {
1010
source = "hashicorp/random"

0 commit comments

Comments
 (0)