aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

appsync: support adding auth provider for AppSync Events after creating an api

Open tmokmss opened this issue 1 year ago • 3 comments

Describe the feature

It'd be great if EventApi has a public method to add auth provider. (e.g. EventApi.addAuthProvider.)

Use Case

Sometimes we want to create resources required for auth (e.g. Cognito UserPool) after the creation of event API. For example, constrcutA contains an AppSync Events API and constructB contains a cognito user pool, and constructB has depdendency on constructA. Currently it is difficult to achieve because EventApi expects all the resources to configure auth provider is available when it is defined by the authorizationConfig construct prop.

Proposed Solution

add EventApi.addAuthProvider(AppSyncAuthProvider) method.

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

CDK version used

2.181.0

Environment details (OS name and version, etc.)

macOS

tmokmss avatar Mar 09 '25 12:03 tmokmss

Yes, this makes sense to me and follow similar patterns to other CDK constructs. Thank you and we welcome PRs.

pahud avatar Mar 10 '25 11:03 pahud

Re: AppSync support adding auth provider for AppSync Events after creation (#33714)

@tmokmss checked current aws-cdk-lib (2.267.0) — EventApi still doesn't expose a way to add auth providers after construction.

CfnApi.eventConfig is a genuine settable object, and you can append to authProviders with the shipped @aws-cdk/cfn-property-mixins package's CfnApiPropsMixin.

The fix

import * as cdk from 'aws-cdk-lib';
import * as appsync from 'aws-cdk-lib/aws-appsync';
import { aws_appsync as appsyncMixins } from '@aws-cdk/cfn-property-mixins';

const api = new appsync.EventApi(this, 'EventApi', {
  apiName: 'my-event-api',
  authorizationConfig: {
    authProviders: [{ authorizationType: appsync.AppSyncAuthorizationType.API_KEY }],
  },
});

const cfnApi = api.node.defaultChild as appsync.CfnApi;
const currentEventConfig = cfnApi.eventConfig as appsync.CfnApi.EventConfigProperty;

api.with(new appsyncMixins.CfnApiPropsMixin(
  {
    eventConfig: {
      ...currentEventConfig,
      authProviders: [...currentEventConfig.authProviders, { authType: 'AWS_IAM' }],
    },
  },
  { strategy: cdk.PropertyMergeStrategy.override() },
));

ArrayMergeStrategy.append() only takes effect for an array that is a DIRECT value of one of the mixin's top-level CFN_PROPERTY_KEYS -- combine()'s array-strategy option is never threaded into the generic deepMerge() it uses to recurse into nested objects, so an array nested one level down (like authProviders inside eventConfig) is always replaced wholesale regardless of the configured array strategy. Since authProviders lives inside eventConfig, you have to read the current eventConfig, splice the new provider into its authProviders array yourself, and pass the whole thing back with override() (same read-and-respread requirement as any nested-object property).

Confirmed working

I verified this with a real synth: starting from an EventApi created with only an API_KEY auth provider, EventConfig.AuthProviders correctly ends up with both API_KEY and AWS_IAM after applying the mixin -- the original combine({arrays: append()}) approach, by contrast, silently DROPPED the existing API_KEY provider (confirmed via synth), because append() never actually engaged on this nested array. Note connectionAuthModes/defaultPublishAuthModes/defaultSubscribeAuthModes live in the same eventConfig object -- extend those the same way if you also need to allow connections/pub/sub under the new auth mode.


This response was posted by an AI agent operating on behalf of @mrgrain.

mrgrain avatar Sep 08 '26 14:09 mrgrain

I'd like to implement EventApi.addAuthProvider() natively, since the mixin route above needs the caller to re-read and re-splice eventConfig by hand.

Plan: render eventConfig lazily instead of in the constructor, move the auth validation (duplicate provider types, connection/publish/subscribe modes must have a matching provider) to node.addValidation, and keep the per-provider side effects (API key creation into apiKeys, the Lambda authorizer invoke permission, Cognito/OIDC config) in one place shared by the constructor and the new method. Unit tests in appsync-event-api.test.ts and an extension of the existing events integ test.

Is a native method still wanted now that the cfn-property-mixins workaround exists? Happy to proceed either way.

gauranshahuja avatar Sep 09 '26 12:09 gauranshahuja