mongodb / awscdk-resources-mongodbatlas

MongoDB Atlas AWS CDK Resources
Apache License 2.0
35 stars 17 forks source link

[Bug]: `CfnTriggerProps` shows required properties as optional, appears to be out of date #222

Closed darksinge closed 7 months ago

darksinge commented 7 months ago

Is there an existing issue for this?

CDK package version

3.1.0

CFN Resource version

trigger: v1.4.0

CFN Resource Region

us-east-1

Current Behavior

I had a big ole description written up for another issue related to creating triggers, then figured out the problem. But, while I'm here I figured I'd complain about something else 🤓.

I copy/pasted this example, (modifying the event processor section to use EventBridge in AWS), and got the following error after deploying with cdk deploy:

CREATE_FAILED        | MongoDB::Atlas::Trigger | ThirdPartyIntegration
Resource handler returned message: "POST https://realm.mongodb.com/api/admin/v3.0/groups/xxxxxxxxxxxxxxxxxxxxxxxx/apps/xxxxxxxxxxxxxxxxxxxxxxxx/trigger
s: 400 (request "") a trigger requires a name" (RequestToken: 79d8e8f8-261f-c721-83eb-e4149fd4de95, HandlerErrorCode: InvalidRequest)

Indeed, after specifying a name for the trigger, the deployment succeeded.

const trigger = new CfnTrigger(this, 'ThirdPartyIntegration', {
  ...,
  name: '<trigger_name>', // 👍🏻
  ...
})

Reviewing the type definition for CfnTriggerProps, I see:

export interface CfnTriggerProps {
    ...
    readonly name?: string;
    ...
}

Maybe it's required if using event bridge as the "event processor"? At any rate, the CDK docs and admin API docs do not seem to be in agreement.


Deployments take a long time, especially when dealing with rollbacks and addressing one hiccup at a time. Ensuring required fields are marked as such makes a difference. Thanks for your attention on this matter!

AWS CDK code to reproduce the issue

// lib/CdkTestingStack.ts
import * as cdk from 'aws-cdk-lib'
import { Construct } from 'constructs'
import { CfnTrigger, DatabaseConfigOperationTypes } from 'awscdk-resources-mongodbatlas'

interface AtlasStackProps {
  readonly projId: string
  readonly profile: string
  readonly appId: string
  readonly dbName: string
  readonly collection: string
  readonly serviceId: string
  readonly functionId: string
  readonly functionName: string
}

export class CdkTestingStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const atlasProps = this.getContextProps()

    const trigger = new CfnTrigger(this, 'ThirdPartyIntegration', {
      projectId: atlasProps.projId,
      profile: atlasProps.profile,
      type: 'DATABASE',
      appId: atlasProps.appId,
      // name: 'my-trigger-name-that-I-wish-was-optional',
      databaseTrigger: {
        operationTypes: [DatabaseConfigOperationTypes.INSERT],
        database: atlasProps.dbName,
        collection: atlasProps.collection,
        serviceId: atlasProps.serviceId,
      },
      eventProcessors: {
        awseventbridge: {
          awsConfig: {
            region: props?.env?.region,
            accountId: props?.env?.account,
          },
        },
      },
    })
  }

  getContextProps(): AtlasStackProps {
    const projId = this.node.tryGetContext('projId')
    if (!projId) {
      throw 'No context value specified for projId. Please specify via the cdk context.'
    }
    const appId = this.node.tryGetContext('appId')
    const profile = this.node.tryGetContext('profile') ?? 'default'
    const dbName = this.node.tryGetContext('dbName')
    const collection = this.node.tryGetContext('collection')
    const serviceId = this.node.tryGetContext('serviceId')

    return {
      projId,
      profile,
      appId,
      dbName,
      collection,
      serviceId,
    }
  }
}

// bin/app.ts
#!/usr/bin/env node
import 'source-map-support/register'
import * as cdk from 'aws-cdk-lib'
import { CdkTestingStack } from '../lib/CdkTestingStack'

const app = new cdk.App()

new CdkTestingStack(app, 'TriggerReproStack', {
  env: { account: '123456781011', region: 'us-east-1' },
})

Steps To Reproduce

  1. In an empty folder, run npx cdk init app --language=typescript
  2. Copy/paste the example code into lib/CdkTestingStack.ts and bin/app.ts, respectively.
  3. Obtain context values from atlas cloud (i.e., appId, profile, dbName, etc.) and run the following command:
    $ npx cdk deploy --context appId=xxxxxxxxxxxxxxxxxxxxxxxx \
     --context profile=default \
     --context dbName=mydb \
     --context collection=mycoll \
     --context serviceId=xxxxxxxxxxxxxxxxxxxxxxxx \
     --context projId=xxxxxxxxxxxxxxxxxxxxxxxx

cdk synth

> trigger-repro@0.1.0 cdk /Users/craig.blackburn/projects/ys/atlas-cdk-demo/apps/trigger-repro
> cdk "synth" "--profile" "dev" "--context" "appId=xxxxxxxxxxxxxxxxxxxxxxxx" "--context" "profile=default" "--context" "dbName=sample_training" "--context" "collection=zips" "--context" "projId=xxxxxxxxxxxxxxxxxxxxxxxx" "--context" "serviceId=xxxxxxxxxxxxxxxxxxxxxxxx"

Resources:
  ThirdPartyIntegration:
    Type: MongoDB::Atlas::Trigger
    Properties:
      Profile: default
      DatabaseTrigger:
        ServiceId: xxxxxxxxxxxxxxxxxxxxxxxx
        Database: sample_training
        Collection: zips
        OperationTypes:
          - INSERT
      Type: DATABASE
      EventProcessors:
        AWSEVENTBRIDGE:
          AWSConfig:
            AccountId: "xxxxxxxxxxxx"
            Region: us-east-1
      AppId: xxxxxxxxxxxxxxxxxxxxxxxx
      ProjectId: xxxxxxxxxxxxxxxxxxxxxxxx
    Metadata:
      aws:cdk:path: TriggerReproStack/ThirdPartyIntegration
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/zPSMzSy1DNQTCwv1k1OydbNyUzSqw4uSUzO1nFOywtKLc4vLUpOrdXJy09J1csq1i8ztNAzBGnIKs7M1C0qzSvJzE3VC4LQAH1hULFNAAAA
    Metadata:
      aws:cdk:path: TriggerReproStack/CDKMetadata/Default
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    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.

Code of Conduct

github-actions[bot] commented 7 months ago

Thanks for opening this issue! Please make sure to provide the following information to help us reproduce the issue:

Thanks for opening this issue. The ticket CLOUDP-231117 was created for internal tracking.

lantoli commented 7 months ago

thanks @darksinge for opening this issue.

there is a mistake in the associated CloudFormation resource where Name is not specified as required. The required fields should be projectId, appId, name and type.

sorry for the inconvenience, we're working on it and it will be fixed in the next CDK release.