Skip to content

Commit bd9f700

Browse files
feat: Add support for opensearch and eventbridge datasources (#57)
1 parent 1c5187d commit bd9f700

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ module "appsync" {
5959
endpoint = "https://search-my-domain.eu-west-1.es.amazonaws.com"
6060
region = "eu-west-1"
6161
}
62+
63+
opensearchservice1 = {
64+
type = "AMAZON_OPENSEARCH_SERVICE"
65+
endpoint = "https://opensearch-my-domain.eu-west-1.es.amazonaws.com"
66+
region = "eu-west-1"
67+
}
68+
69+
eventbridge1 = {
70+
type = "AMAZON_EVENTBRIDGE"
71+
event_bus_arn = "aws:arn:events:us-west-1:135367859850:event-bus/eventbridge1"
72+
}
6273
}
6374
6475
resolvers = {
@@ -172,6 +183,7 @@ No modules.
172183
| <a name="input_domain_name_description"></a> [domain\_name\_description](#input\_domain\_name\_description) | A description of the Domain Name. | `string` | `null` | no |
173184
| <a name="input_dynamodb_allowed_actions"></a> [dynamodb\_allowed\_actions](#input\_dynamodb\_allowed\_actions) | List of allowed IAM actions for datasources type AMAZON\_DYNAMODB | `list(string)` | <pre>[<br> "dynamodb:GetItem",<br> "dynamodb:PutItem",<br> "dynamodb:DeleteItem",<br> "dynamodb:UpdateItem",<br> "dynamodb:Query",<br> "dynamodb:Scan",<br> "dynamodb:BatchGetItem",<br> "dynamodb:BatchWriteItem"<br>]</pre> | no |
174185
| <a name="input_elasticsearch_allowed_actions"></a> [elasticsearch\_allowed\_actions](#input\_elasticsearch\_allowed\_actions) | List of allowed IAM actions for datasources type AMAZON\_ELASTICSEARCH | `list(string)` | <pre>[<br> "es:ESHttpDelete",<br> "es:ESHttpHead",<br> "es:ESHttpGet",<br> "es:ESHttpPost",<br> "es:ESHttpPut"<br>]</pre> | no |
186+
| <a name="input_eventbridge_allowed_actions"></a> [eventbridge\_allowed\_actions](#input\_eventbridge\_allowed\_actions) | List of allowed IAM actions for datasources type AMAZON\_EVENTBRIDGE | `list(string)` | <pre>[<br> "events:PutEvents"<br>]</pre> | no |
175187
| <a name="input_functions"></a> [functions](#input\_functions) | Map of functions to create | `any` | `{}` | no |
176188
| <a name="input_graphql_api_tags"></a> [graphql\_api\_tags](#input\_graphql\_api\_tags) | Map of tags to add to GraphQL API | `map(string)` | `{}` | no |
177189
| <a name="input_iam_permissions_boundary"></a> [iam\_permissions\_boundary](#input\_iam\_permissions\_boundary) | ARN for iam permissions boundary | `string` | `null` | no |
@@ -185,6 +197,7 @@ No modules.
185197
| <a name="input_logs_role_tags"></a> [logs\_role\_tags](#input\_logs\_role\_tags) | Map of tags to add to Cloudwatch logs IAM role | `map(string)` | `{}` | no |
186198
| <a name="input_name"></a> [name](#input\_name) | Name of GraphQL API | `string` | `""` | no |
187199
| <a name="input_openid_connect_config"></a> [openid\_connect\_config](#input\_openid\_connect\_config) | Nested argument containing OpenID Connect configuration. | `map(string)` | `{}` | no |
200+
| <a name="input_opensearchservice_allowed_actions"></a> [opensearchservice\_allowed\_actions](#input\_opensearchservice\_allowed\_actions) | List of allowed IAM actions for datasources type AMAZON\_OPENSEARCH\_SERVICE | `list(string)` | <pre>[<br> "es:ESHttpDelete",<br> "es:ESHttpHead",<br> "es:ESHttpGet",<br> "es:ESHttpPost",<br> "es:ESHttpPut"<br>]</pre> | no |
188201
| <a name="input_resolver_caching_ttl"></a> [resolver\_caching\_ttl](#input\_resolver\_caching\_ttl) | Default caching TTL for resolvers when caching is enabled | `number` | `60` | no |
189202
| <a name="input_resolvers"></a> [resolvers](#input\_resolvers) | Map of resolvers to create | `any` | `{}` | no |
190203
| <a name="input_schema"></a> [schema](#input\_schema) | The schema definition, in GraphQL schema language format. Terraform cannot perform drift detection of this configuration. | `string` | `""` | no |

examples/complete/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,21 @@ module "appsync" {
213213
endpoint = "https://search-my-domain.eu-west-1.es.amazonaws.com"
214214
region = "eu-west-1"
215215
}
216+
217+
# Opensearch Service support has not been finished & tested
218+
opensearchservice1 = {
219+
type = "AMAZON_OPENSEARCH_SERVICE"
220+
221+
# Note: dynamic references (module.opensearchservice1.id) do not work do not work unless you create this resource in advance
222+
endpoint = "https://search-my-domain-2.eu-west-1.es.amazonaws.com"
223+
region = "eu-west-1"
224+
}
225+
226+
eventbridge1 = {
227+
type = "AMAZON_EVENTBRIDGE"
228+
229+
event_bus_arn = "aws:arn:events:us-west-1:135367859850:event-bus/eventbridge1"
230+
}
216231
}
217232

218233
resolvers = {

iam.tf

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
data "aws_partition" "this" {}
22

33
locals {
4-
service_roles_with_policies = var.create_graphql_api ? { for k, v in var.datasources : k => v if contains(["AWS_LAMBDA", "AMAZON_DYNAMODB", "AMAZON_ELASTICSEARCH"], v.type) && tobool(lookup(v, "create_service_role", true)) } : {}
4+
service_roles_with_policies = var.create_graphql_api ? { for k, v in var.datasources : k => v if contains(["AWS_LAMBDA", "AMAZON_DYNAMODB", "AMAZON_ELASTICSEARCH", "AMAZON_OPENSEARCH_SERVICE", "AMAZON_EVENTBRIDGE"], v.type) && tobool(lookup(v, "create_service_role", true)) } : {}
55

66
service_roles_with_policies_lambda = { for k, v in local.service_roles_with_policies : k => merge(v,
77
{
@@ -39,10 +39,36 @@ locals {
3939
}
4040
) if v.type == "AMAZON_ELASTICSEARCH" }
4141

42+
service_roles_with_policies_opensearchservice = { for k, v in local.service_roles_with_policies : k => merge(v,
43+
{
44+
policy_statements = {
45+
opensearchservice = {
46+
effect = "Allow"
47+
actions = lookup(v, "policy_actions", null) == null ? var.opensearchservice_allowed_actions : v.policy_actions
48+
resources = [format("arn:${data.aws_partition.this.partition}:es:%v::domain/%v/*", v.region, v.endpoint)]
49+
}
50+
}
51+
}
52+
) if v.type == "AMAZON_OPENSEARCH_SERVICE" }
53+
54+
service_roles_with_policies_eventbridge = { for k, v in local.service_roles_with_policies : k => merge(v,
55+
{
56+
policy_statements = {
57+
eventbridge = {
58+
effect = "Allow"
59+
actions = lookup(v, "policy_actions", null) == null ? var.eventbridge_allowed_actions : v.policy_actions
60+
resources = [v.event_bus_arn]
61+
}
62+
}
63+
}
64+
) if v.type == "AMAZON_EVENTBRIDGE" }
65+
4266
service_roles_with_specific_policies = merge(
4367
local.service_roles_with_policies_lambda,
4468
local.service_roles_with_policies_dynamodb,
4569
local.service_roles_with_policies_elasticsearch,
70+
local.service_roles_with_policies_opensearchservice,
71+
local.service_roles_with_policies_eventbridge,
4672
)
4773
}
4874

main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ resource "aws_appsync_datasource" "this" {
144144
name = each.key
145145
type = each.value.type
146146
description = lookup(each.value, "description", null)
147-
service_role_arn = lookup(each.value, "service_role_arn", tobool(lookup(each.value, "create_service_role", contains(["AWS_LAMBDA", "AMAZON_DYNAMODB", "AMAZON_ELASTICSEARCH"], each.value.type))) ? aws_iam_role.service_role[each.key].arn : null)
147+
service_role_arn = lookup(each.value, "service_role_arn", tobool(lookup(each.value, "create_service_role", contains(["AWS_LAMBDA", "AMAZON_DYNAMODB", "AMAZON_ELASTICSEARCH", "AMAZON_OPENSEARCH_SERVICE", "AMAZON_EVENTBRIDGE"], each.value.type))) ? aws_iam_role.service_role[each.key].arn : null)
148148

149149
dynamic "http_config" {
150150
for_each = each.value.type == "HTTP" ? [true] : []
@@ -180,6 +180,23 @@ resource "aws_appsync_datasource" "this" {
180180
region = lookup(each.value, "region", null)
181181
}
182182
}
183+
184+
dynamic "opensearchservice_config" {
185+
for_each = each.value.type == "AMAZON_OPENSEARCH_SERVICE" ? [true] : []
186+
187+
content {
188+
endpoint = each.value.endpoint
189+
region = lookup(each.value, "region", null)
190+
}
191+
}
192+
193+
dynamic "event_bridge_config" {
194+
for_each = each.value.type == "AMAZON_EVENTBRIDGE" ? [true] : []
195+
196+
content {
197+
event_bus_arn = each.value.event_bus_arn
198+
}
199+
}
183200
}
184201

185202
# Resolvers

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ variable "elasticsearch_allowed_actions" {
230230
default = ["es:ESHttpDelete", "es:ESHttpHead", "es:ESHttpGet", "es:ESHttpPost", "es:ESHttpPut"]
231231
}
232232

233+
variable "opensearchservice_allowed_actions" {
234+
description = "List of allowed IAM actions for datasources type AMAZON_OPENSEARCH_SERVICE"
235+
type = list(string)
236+
default = ["es:ESHttpDelete", "es:ESHttpHead", "es:ESHttpGet", "es:ESHttpPost", "es:ESHttpPut"]
237+
}
238+
239+
variable "eventbridge_allowed_actions" {
240+
description = "List of allowed IAM actions for datasources type AMAZON_EVENTBRIDGE"
241+
type = list(string)
242+
default = ["events:PutEvents"]
243+
}
244+
233245
variable "iam_permissions_boundary" {
234246
description = "ARN for iam permissions boundary"
235247
type = string

wrappers/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module "wrapper" {
3636
lambda_allowed_actions = try(each.value.lambda_allowed_actions, var.defaults.lambda_allowed_actions, ["lambda:invokeFunction"])
3737
dynamodb_allowed_actions = try(each.value.dynamodb_allowed_actions, var.defaults.dynamodb_allowed_actions, ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:DeleteItem", "dynamodb:UpdateItem", "dynamodb:Query", "dynamodb:Scan", "dynamodb:BatchGetItem", "dynamodb:BatchWriteItem"])
3838
elasticsearch_allowed_actions = try(each.value.elasticsearch_allowed_actions, var.defaults.elasticsearch_allowed_actions, ["es:ESHttpDelete", "es:ESHttpHead", "es:ESHttpGet", "es:ESHttpPost", "es:ESHttpPut"])
39+
opensearchservice_allowed_actions = try(each.value.opensearchservice_allowed_actions, var.defaults.opensearchservice_allowed_actions, ["es:ESHttpDelete", "es:ESHttpHead", "es:ESHttpGet", "es:ESHttpPost", "es:ESHttpPut"])
40+
eventbridge_allowed_actions = try(each.value.eventbridge_allowed_actions, var.defaults.eventbridge_allowed_actions, ["events:PutEvents"])
3941
iam_permissions_boundary = try(each.value.iam_permissions_boundary, var.defaults.iam_permissions_boundary, null)
4042
direct_lambda_request_template = try(each.value.direct_lambda_request_template, var.defaults.direct_lambda_request_template, <<-EOF
4143
{

0 commit comments

Comments
 (0)