aws-amplify / docs

AWS Amplify Framework Documentation
https://docs.amplify.aws
Apache License 2.0
482 stars 1.04k forks source link

Event Bridge example with L2 Rule construct instead of L1 Cfn #7807

Open OperationalFallacy opened 3 months ago

OperationalFallacy commented 3 months ago

Environment information

npx ampx info
System:
  OS: macOS 14
  CPU: (8) arm64 Apple M2
Binaries:
  Node: 20.15.0 - ~/.n/bin/node
  Yarn: 4.3.1 - ~/.n/bin/yarn
  npm: 10.7.0 - ~/.n/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.1.0
  aws-amplify: 6.3.8
  aws-cdk: 2.147.2
  aws-cdk-lib: 2.147.2
  typescript: 5.5.2
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

Regarding EventBridge example for data sources

https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/connect-eventbridge-datasource/

I'm trying to setup this example with L2 construct Rule (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.Rule.html)

This is the class, where api is backend.data.resources.graphqlApi

export class FileSearchEventBus extends Construct {
  constructor(scope: Construct, id: string, api: IGraphqlApi) {
    super(scope, id)

    const eventBus = new EventBus(this, "filesearch", {})

    api.addEventBridgeDataSource("EventBridgeSrc", eventBus)
    console.debug("EventBridgeSrc is added")

    const policyStatement = new PolicyStatement({
      effect: Effect.ALLOW,
      actions: ["appsync:GraphQL"],
      resources: [`${api.arn}/types/Mutation/*`]
    })

    const eventBusRole = new Role(this, "AppSyncInvokeRole", {
      assumedBy: new ServicePrincipal("events.amazonaws.com"),
      inlinePolicies: {
        PolicyStatement: new PolicyDocument({
          statements: [policyStatement]
        })
      }
    })

    new Rule(this, "FileSearch", {
      eventBus: eventBus,
      ruleName: "broadcastFileSearchUpdates",
      eventPattern: {
        source: ["amplify.filesearch"],
        detailType: ["FileSearchChange"],
        detail: {
          fileId: [{ exists: true }],
          status: ["PENDING", "ATTACHED", "DETACHED"],
          message: [{ exists: true }]
        }
      },
      targets: [
        new AppSync(api, {
          variables: RuleTargetInput.fromObject({
            fileId: EventField.fromPath("$.detail.fileId"),
            status: EventField.fromPath("$.detail.status"),
            message: EventField.fromPath("$.detail.message")
          }),
          graphQLOperation: `
            mutation PublishFileSearchFromEventBridge(
              $fileId: String!
              $status: String!
              $message: String!
            ) {
              publishFileSearchFromEventBridge(fileId: $fileId, status: $status, message: $message) {
                fileId
                status
                message
              }
            }
          `,
          eventRole: eventBusRole
        })
      ]
    })
  }
}

I define data backend as this

export const data = defineData({
  name: "data",
  schema,
  authorizationModes: {
    defaultAuthorizationMode: "userPool"
  }
})

The stack breaks. Error: You must have AWS_IAM authorization mode enabled on your API to configure an AppSync target

This is the check on construct here, https://github.com/aws/aws-cdk/blob/v2.147.3/packages/aws-cdk-lib/aws-events-targets/lib/appsync.ts#L49

The modes on backend's app sync is empty.

Question: is it still possible to use L2 for the example you have? It seems to support all props. Was this the reason you've use Cnf?

OperationalFallacy commented 3 months ago

Also, looking at the amplify example page

import { aws_events } from "aws-cdk-lib"; <<--- this is incorrect import, should be import { CfnRule } from "aws-cdk-lib/aws-events"

// Create an EventBridge rule to route events to the AppSync API const rule = new aws_events.CfnRule(eventStack, "MyOrderRule"

ykethan commented 3 months ago

HeyπŸ‘‹ thanks for raising this! I'm going to transfer this over to our API repository for better assistance πŸ™‚

AnilMaktala commented 3 months ago

Hey @OperationalFallacy πŸ‘‹ , Thanks for raising this! I'm going to transfer this over to our Doc's repository for better assistance πŸ™‚