From a0ad49df7b2536d800b4890ae0116e6ce26e6c55 Mon Sep 17 00:00:00 2001 From: Spencer Post Date: Wed, 3 May 2023 13:11:10 -0600 Subject: [PATCH] feat(appsync): L2 construct for EventBridge DataSource. (#25369) Adds an L2 EventBridge data source to AppSync. Closes #24809. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../test/appsync.eventbridge.graphql | 28 ++ ...efaultTestDeployAssert018781F2.assets.json | 19 + ...aultTestDeployAssert018781F2.template.json | 36 ++ .../cdk.out | 1 + .../integ.json | 12 + .../manifest.json | 153 ++++++++ .../stack.assets.json | 19 + .../stack.template.json | 168 ++++++++ .../tree.json | 364 ++++++++++++++++++ .../test/integ.appsync-eventbridge.ts | 30 ++ packages/aws-cdk-lib/aws-appsync/README.md | 87 +++++ .../aws-appsync/lib/data-source.ts | 34 ++ .../aws-appsync/lib/graphqlapi-base.ts | 37 +- .../test/appsync-eventbridge.test.ts | 101 +++++ 14 files changed, 1088 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/appsync.eventbridge.graphql create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.ts create mode 100644 packages/aws-cdk-lib/aws-appsync/test/appsync-eventbridge.test.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/appsync.eventbridge.graphql b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/appsync.eventbridge.graphql new file mode 100644 index 0000000000000..74909e0659a9b --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/appsync.eventbridge.graphql @@ -0,0 +1,28 @@ +schema { + query: Query + mutation: Mutation +} + +type Query { + event(id:ID!): Event +} + +type Mutation { + emitEvent(id: ID!, name: String): PutEventsResult! +} + +type Event { + id: ID! + name: String! +} + +type Entry { + ErrorCode: String + ErrorMessage: String + EventId: String +} + +type PutEventsResult { + Entries: [Entry!] + FailedEntry: Int +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json new file mode 100644 index 0000000000000..2237ea23fb0c4 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json @@ -0,0 +1,19 @@ +{ + "version": "31.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "apiDefaultTestDeployAssert018781F2.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/cdk.out new file mode 100644 index 0000000000000..7925065efbcc4 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"31.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/integ.json new file mode 100644 index 0000000000000..1f53d4ca6f18d --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "31.0.0", + "testCases": { + "api/DefaultTest": { + "stacks": [ + "stack" + ], + "assertionStack": "api/DefaultTest/DeployAssert", + "assertionStackName": "apiDefaultTestDeployAssert018781F2" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/manifest.json new file mode 100644 index 0000000000000..42c6fefd56e2d --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/manifest.json @@ -0,0 +1,153 @@ +{ + "version": "31.0.0", + "artifacts": { + "stack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "stack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "stack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "stack.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/48b6d0709cf46b7d6a28cd05baf38e498017a3fc758be4af72c8dd16419baa5f.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "stack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "stack.assets" + ], + "metadata": { + "/stack/EventBridgeApi/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApi398AE60D" + } + ], + "/stack/EventBridgeApi/Schema": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiSchema535E9664" + } + ], + "/stack/EventBridgeApi/DefaultApiKey": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiDefaultApiKeyC757E0EA" + } + ], + "/stack/EventBridgeApi/EventBridgeDs/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiEventBridgeDsServiceRoleF433388F" + } + ], + "/stack/EventBridgeApi/EventBridgeDs/ServiceRole/DefaultPolicy/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiEventBridgeDsServiceRoleDefaultPolicyF1047C06" + } + ], + "/stack/EventBridgeApi/EventBridgeDs/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiEventBridgeDs3E3BC289" + } + ], + "/stack/EventBridgeApi/EventResolver/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "EventBridgeApiEventResolverD968F6C6" + } + ], + "/stack/DestinationEventBus/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "DestinationEventBus776315F0" + } + ], + "/stack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/stack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "stack" + }, + "apiDefaultTestDeployAssert018781F2.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "apiDefaultTestDeployAssert018781F2.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "apiDefaultTestDeployAssert018781F2": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "apiDefaultTestDeployAssert018781F2.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "apiDefaultTestDeployAssert018781F2.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "apiDefaultTestDeployAssert018781F2.assets" + ], + "metadata": { + "/api/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/api/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "api/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.assets.json new file mode 100644 index 0000000000000..ca38eba6c1e6c --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "31.0.0", + "files": { + "48b6d0709cf46b7d6a28cd05baf38e498017a3fc758be4af72c8dd16419baa5f": { + "source": { + "path": "stack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "48b6d0709cf46b7d6a28cd05baf38e498017a3fc758be4af72c8dd16419baa5f.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.template.json new file mode 100644 index 0000000000000..e7f74dc601108 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/stack.template.json @@ -0,0 +1,168 @@ +{ + "Resources": { + "EventBridgeApi398AE60D": { + "Type": "AWS::AppSync::GraphQLApi", + "Properties": { + "AuthenticationType": "API_KEY", + "Name": "EventBridgeApi" + } + }, + "EventBridgeApiSchema535E9664": { + "Type": "AWS::AppSync::GraphQLSchema", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "Definition": "schema {\n query: Query\n mutation: Mutation\n}\n\ntype Query {\n event(id:ID!): Event\n}\n\ntype Mutation {\n emitEvent(id: ID!, name: String): PutEventsResult!\n}\n\ntype Event {\n id: ID!\n name: String!\n}\n\ntype Entry {\n ErrorCode: String\n ErrorMessage: String\n EventId: String\n}\n\ntype PutEventsResult {\n Entries: [Entry!]\n FailedEntry: Int\n}" + } + }, + "EventBridgeApiDefaultApiKeyC757E0EA": { + "Type": "AWS::AppSync::ApiKey", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + } + }, + "DependsOn": [ + "EventBridgeApiSchema535E9664" + ] + }, + "EventBridgeApiEventBridgeDsServiceRoleF433388F": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "appsync.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "EventBridgeApiEventBridgeDsServiceRoleDefaultPolicyF1047C06": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyDocument": { + "Statement": [ + { + "Action": "events:PutEvents", + "Effect": "Allow", + "Resource": { + "Fn::GetAtt": [ + "DestinationEventBus776315F0", + "Arn" + ] + } + } + ], + "Version": "2012-10-17" + }, + "PolicyName": "EventBridgeApiEventBridgeDsServiceRoleDefaultPolicyF1047C06", + "Roles": [ + { + "Ref": "EventBridgeApiEventBridgeDsServiceRoleF433388F" + } + ] + } + }, + "EventBridgeApiEventBridgeDs3E3BC289": { + "Type": "AWS::AppSync::DataSource", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "Name": "EventBridgeDs", + "Type": "AMAZON_EVENTBRIDGE", + "EventBridgeConfig": { + "EventBusArn": { + "Fn::GetAtt": [ + "DestinationEventBus776315F0", + "Arn" + ] + } + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "EventBridgeApiEventBridgeDsServiceRoleF433388F", + "Arn" + ] + } + } + }, + "EventBridgeApiEventResolverD968F6C6": { + "Type": "AWS::AppSync::Resolver", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "FieldName": "emitEvent", + "TypeName": "Mutation", + "DataSourceName": "EventBridgeDs", + "Kind": "UNIT", + "RequestMappingTemplate": "{\"version\" : \"2018-05-29\", \"operation\": \"PutEvents\", \"events\" : [{ \"source\": \"integ.appsync.eventbridge\", \"detailType\": \"Mutation.emitEvent\", \"detail\": $util.toJson($context.arguments) }]}", + "ResponseMappingTemplate": "$util.toJson($ctx.result)" + }, + "DependsOn": [ + "EventBridgeApiEventBridgeDs3E3BC289", + "EventBridgeApiSchema535E9664" + ] + }, + "DestinationEventBus776315F0": { + "Type": "AWS::Events::EventBus", + "Properties": { + "Name": "stackDestinationEventBus3059F22F" + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/tree.json new file mode 100644 index 0000000000000..543c4f6126a69 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.js.snapshot/tree.json @@ -0,0 +1,364 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "stack": { + "id": "stack", + "path": "stack", + "children": { + "EventBridgeApi": { + "id": "EventBridgeApi", + "path": "stack/EventBridgeApi", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/EventBridgeApi/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::GraphQLApi", + "aws:cdk:cloudformation:props": { + "authenticationType": "API_KEY", + "name": "EventBridgeApi" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnGraphQLApi", + "version": "0.0.0" + } + }, + "Schema": { + "id": "Schema", + "path": "stack/EventBridgeApi/Schema", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::GraphQLSchema", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "definition": "schema {\n query: Query\n mutation: Mutation\n}\n\ntype Query {\n event(id:ID!): Event\n}\n\ntype Mutation {\n emitEvent(id: ID!, name: String): PutEventsResult!\n}\n\ntype Event {\n id: ID!\n name: String!\n}\n\ntype Entry {\n ErrorCode: String\n ErrorMessage: String\n EventId: String\n}\n\ntype PutEventsResult {\n Entries: [Entry!]\n FailedEntry: Int\n}" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnGraphQLSchema", + "version": "0.0.0" + } + }, + "DefaultApiKey": { + "id": "DefaultApiKey", + "path": "stack/EventBridgeApi/DefaultApiKey", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::ApiKey", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnApiKey", + "version": "0.0.0" + } + }, + "LogGroup": { + "id": "LogGroup", + "path": "stack/EventBridgeApi/LogGroup", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "EventBridgeDs": { + "id": "EventBridgeDs", + "path": "stack/EventBridgeApi/EventBridgeDs", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "stack/EventBridgeApi/EventBridgeDs/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "stack/EventBridgeApi/EventBridgeDs/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "stack/EventBridgeApi/EventBridgeDs/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "appsync.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + }, + "DefaultPolicy": { + "id": "DefaultPolicy", + "path": "stack/EventBridgeApi/EventBridgeDs/ServiceRole/DefaultPolicy", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/EventBridgeApi/EventBridgeDs/ServiceRole/DefaultPolicy/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Policy", + "aws:cdk:cloudformation:props": { + "policyDocument": { + "Statement": [ + { + "Action": "events:PutEvents", + "Effect": "Allow", + "Resource": { + "Fn::GetAtt": [ + "DestinationEventBus776315F0", + "Arn" + ] + } + } + ], + "Version": "2012-10-17" + }, + "policyName": "EventBridgeApiEventBridgeDsServiceRoleDefaultPolicyF1047C06", + "roles": [ + { + "Ref": "EventBridgeApiEventBridgeDsServiceRoleF433388F" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Policy", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "stack/EventBridgeApi/EventBridgeDs/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::DataSource", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "name": "EventBridgeDs", + "type": "AMAZON_EVENTBRIDGE", + "eventBridgeConfig": { + "eventBusArn": { + "Fn::GetAtt": [ + "DestinationEventBus776315F0", + "Arn" + ] + } + }, + "serviceRoleArn": { + "Fn::GetAtt": [ + "EventBridgeApiEventBridgeDsServiceRoleF433388F", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnDataSource", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.EventBridgeDataSource", + "version": "0.0.0" + } + }, + "EventResolver": { + "id": "EventResolver", + "path": "stack/EventBridgeApi/EventResolver", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/EventBridgeApi/EventResolver/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::Resolver", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "EventBridgeApi398AE60D", + "ApiId" + ] + }, + "fieldName": "emitEvent", + "typeName": "Mutation", + "dataSourceName": "EventBridgeDs", + "kind": "UNIT", + "requestMappingTemplate": "{\"version\" : \"2018-05-29\", \"operation\": \"PutEvents\", \"events\" : [{ \"source\": \"integ.appsync.eventbridge\", \"detailType\": \"Mutation.emitEvent\", \"detail\": $util.toJson($context.arguments) }]}", + "responseMappingTemplate": "$util.toJson($ctx.result)" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnResolver", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.Resolver", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.GraphqlApi", + "version": "0.0.0" + } + }, + "DestinationEventBus": { + "id": "DestinationEventBus", + "path": "stack/DestinationEventBus", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/DestinationEventBus/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Events::EventBus", + "aws:cdk:cloudformation:props": { + "name": "stackDestinationEventBus3059F22F" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_events.CfnEventBus", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_events.EventBus", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "stack/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "stack/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "api": { + "id": "api", + "path": "api", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "api/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "api/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.270" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "api/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "api/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "api/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.270" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.ts new file mode 100644 index 0000000000000..8fb9ccb1323a7 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-eventbridge.ts @@ -0,0 +1,30 @@ +import * as path from 'path'; +import * as cdk from 'aws-cdk-lib'; +import * as appsync from 'aws-cdk-lib/aws-appsync'; +import * as events from 'aws-cdk-lib/aws-events'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'stack'); + +const api = new appsync.GraphqlApi(stack, 'EventBridgeApi', { + name: 'EventBridgeApi', + schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.eventbridge.graphql')), +}); + +const bus = new events.EventBus(stack, 'DestinationEventBus', {}); + +const dataSource = api.addEventBridgeDataSource('EventBridgeDs', bus); + +dataSource.createResolver('EventResolver', { + typeName: 'Mutation', + fieldName: 'emitEvent', + requestMappingTemplate: appsync.MappingTemplate.fromString('{"version" : "2018-05-29", "operation": "PutEvents", "events" : [{ "source": "integ.appsync.eventbridge", "detailType": "Mutation.emitEvent", "detail": $util.toJson($context.arguments) }]}'), + responseMappingTemplate: appsync.MappingTemplate.fromString('$util.toJson($ctx.result)'), +}); + +new IntegTest(app, 'api', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-appsync/README.md b/packages/aws-cdk-lib/aws-appsync/README.md index 5ba37810dc3cd..38c0c1c599482 100644 --- a/packages/aws-cdk-lib/aws-appsync/README.md +++ b/packages/aws-cdk-lib/aws-appsync/README.md @@ -232,6 +232,93 @@ httpDs.createResolver('MutationCallStepFunctionResolver', { }); ``` +### EventBridge +Integrating AppSync with EventBridge enables developers to use EventBridge rules to route commands for GraphQl mutations +that need to perform any one of a variety of asynchronous tasks. More broadly, it enables teams to expose an event bus +as a part of a GraphQl schema. + +GraphQL schema file `schema.graphql`: + +```gql +schema { + query: Query + mutation: Mutation +} + +type Query { + event(id:ID!): Event +} + +type Mutation { + emitEvent(id: ID!, name: String): PutEventsResult! +} + +type Event { + id: ID! + name: String! +} + +type Entry { + ErrorCode: String + ErrorMessage: String + EventId: String +} + +type PutEventsResult { + Entries: [Entry!] + FailedEntry: Int +} +``` + +GraphQL request mapping template `request.vtl`: + +``` +{ + "version" : "2018-05-29", + "operation": "PutEvents", + "events" : [ + { + "source": "integ.appsync.eventbridge", + "detailType": "Mutation.emitEvent", + "detail": $util.toJson($context.arguments) + } + ] +} +``` + +GraphQL response mapping template `response.vtl`: + +``` +$util.toJson($ctx.result)' +``` + +This response mapping template simply converts the EventBridge PutEvents result to JSON. +For details about the response see the +[documentation](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEvents.html). +Additional logic can be added to the response template to map the response type, or to error in the event of failed +events. More information can be found +[here](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-eventbridge.html). + +CDK stack file `app-stack.ts`: + +```ts +const api = new appsync.GraphqlApi(stack, 'EventBridgeApi', { + name: 'EventBridgeApi', + schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.eventbridge.graphql')), +}); + +const bus = new events.EventBus(stack, 'DestinationEventBus', {}); + +const dataSource = api.addEventBridgeDataSource('NoneDS', bus); + +dataSource.createResolver('EventResolver', { + typeName: 'Mutation', + fieldName: 'emitEvent', + requestMappingTemplate: appsync.MappingTemplate.fromFile('request.vtl'), + responseMappingTemplate: appsync.MappingTemplate.fromFile('response.vtl'), +}); +``` + ### Amazon OpenSearch Service AppSync has builtin support for Amazon OpenSearch Service (successor to Amazon diff --git a/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts b/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts index 32d285de99c17..9c2cffb75d9a5 100644 --- a/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts +++ b/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts @@ -11,6 +11,7 @@ import { BaseAppsyncFunctionProps, AppsyncFunction } from './appsync-function'; import { CfnDataSource } from './appsync.generated'; import { IGraphqlApi } from './graphqlapi-base'; import { BaseResolverProps, Resolver } from './resolver'; +import { IEventBus } from "../../aws-events"; /** * Base properties for an AppSync datasource @@ -79,6 +80,14 @@ export interface ExtendedDataSourceProps { * @default - No config */ readonly httpConfig?: CfnDataSource.HttpConfigProperty | IResolvable; + + /** + * configuration for EventBridge Datasource + * + * @default - No config + */ + readonly eventBridgeConfig?: CfnDataSource.EventBridgeConfigProperty | IResolvable + /** * configuration for Lambda Datasource * @@ -280,6 +289,31 @@ export class HttpDataSource extends BackedDataSource { } } +/** + * Properties for an AppSync EventBridge datasource + */ +export interface EventBridgeDataSourceProps extends BackedDataSourceProps { + /** + * The EventBridge EventBus + */ + readonly eventBus: IEventBus +} + +/** + * An AppSync datasource backed by EventBridge + */ +export class EventBridgeDataSource extends BackedDataSource { + constructor(scope: Construct, id: string, props: EventBridgeDataSourceProps) { + super(scope, id, props, { + type: 'AMAZON_EVENTBRIDGE', + eventBridgeConfig: { + eventBusArn: props.eventBus.eventBusArn, + }, + }); + props.eventBus.grantPutEventsTo(this); + } +} + /** * Properties for an AppSync Lambda datasource */ diff --git a/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts b/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts index c13ba65f983c0..483ffe91f9e94 100644 --- a/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts +++ b/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts @@ -5,8 +5,19 @@ import { IDomain as IOpenSearchDomain } from '../../aws-opensearchservice'; import { IServerlessCluster } from '../../aws-rds'; import { ISecret } from '../../aws-secretsmanager'; import { CfnResource, IResource, Resource } from '../../core'; -import { DynamoDbDataSource, HttpDataSource, LambdaDataSource, NoneDataSource, RdsDataSource, AwsIamConfig, ElasticsearchDataSource, OpenSearchDataSource } from './data-source'; +import { + DynamoDbDataSource, + HttpDataSource, + LambdaDataSource, + NoneDataSource, + RdsDataSource, + AwsIamConfig, + ElasticsearchDataSource, + OpenSearchDataSource, + EventBridgeDataSource +} from './data-source'; import { Resolver, ExtendedResolverProps } from './resolver'; +import { IEventBus } from "../../aws-events"; /** * Optional configuration for data sources @@ -86,6 +97,15 @@ export interface IGraphqlApi extends IResource { */ addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions): HttpDataSource; + + /** + * Add an EventBridge data source to this api + * @param id The data source's id + * @param eventBus The EventBridge EventBus on which to put events + * @param options The optional configuration for this data source + */ + addEventBridgeDataSource(id: string, eventBus: IEventBus, options?: DataSourceOptions): EventBridgeDataSource + /** * add a new Lambda data source to this API * @@ -266,6 +286,21 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi { }); } + /** + * Add an EventBridge data source to this api + * @param id The data source's id + * @param eventBus The EventBridge EventBus on which to put events + * @param options The optional configuration for this data source + */ + addEventBridgeDataSource(id: string, eventBus: IEventBus, options?: DataSourceOptions): EventBridgeDataSource { + return new EventBridgeDataSource(this, id, { + api: this, + eventBus, + name: options?.name, + description: options?.description + }); + } + /** * add a new OpenSearch data source to this API * diff --git a/packages/aws-cdk-lib/aws-appsync/test/appsync-eventbridge.test.ts b/packages/aws-cdk-lib/aws-appsync/test/appsync-eventbridge.test.ts new file mode 100644 index 0000000000000..de49a2219a50f --- /dev/null +++ b/packages/aws-cdk-lib/aws-appsync/test/appsync-eventbridge.test.ts @@ -0,0 +1,101 @@ +import * as path from 'path'; +import { Template } from '../../assertions'; +import * as eventBridge from '../../aws-events'; +import * as cdk from '../../core'; +import * as appsync from '../lib'; + +// GLOBAL GIVEN +let stack: cdk.Stack; +let api: appsync.GraphqlApi; +let eventBus: eventBridge.EventBus; + +beforeEach(() => { + stack = new cdk.Stack(); + api = new appsync.GraphqlApi(stack, 'baseApi', { + name: 'api', + schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.test.graphql')), + }); + eventBus = new eventBridge.EventBus(stack, 'targetEventBus', { + eventBusName: 'EventBus', + }); +}); + +describe('EventBridge Data Source Configuration', () => { + test('The Datasource policy is configured to put events to the event bus', () => { + // WHEN + api.addEventBridgeDataSource('ds', eventBus); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { + PolicyDocument: { + Version: '2012-10-17', + Statement: [{ + Action: 'events:PutEvents', + Effect: 'Allow', + Resource: { + 'Fn::GetAtt': ['targetEventBus07F5DAC9', 'Arn'], + }, + }], + }, + }); + }); + + test('The EventBridge configuration contains the event bus arn', () => { + // WHEN + api.addEventBridgeDataSource('ds', eventBus); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', { + EventBridgeConfig: { + EventBusArn: { + 'Fn::GetAtt': ['targetEventBus07F5DAC9', 'Arn'], + }, + }, + }); + }); + + test('The default configuration produces a name identical to the id', () => { + // WHEN + api.addEventBridgeDataSource('ds', eventBus); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', { + Type: 'AMAZON_EVENTBRIDGE', + Name: 'ds', + }); + }); + + test('A custom name is used when provided', () => { + // WHEN + api.addEventBridgeDataSource('id', eventBus, { name: 'custom' }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', { + Type: 'AMAZON_EVENTBRIDGE', + Name: 'custom', + }); + }); + + test('A custom description is used when provided', () => { + // WHEN + api.addEventBridgeDataSource('ds', eventBus, { name: 'custom', description: 'custom description' }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', { + Type: 'AMAZON_EVENTBRIDGE', + Name: 'custom', + Description: 'custom description', + }); + }); + + test('An error occurs when creating multiple EventBridge data sources with the same name', () => { + // WHEN + const when = () => { + api.addEventBridgeDataSource('ds', eventBus); + api.addEventBridgeDataSource('ds', eventBus); + }; + + // THEN + expect(when).toThrow('There is already a Construct with name \'ds\' in GraphqlApi [baseApi]'); + }); +});