How to Resolve Circular Dependency in CloudFormation with Cognito and API

0
6
Asked By CuriousCoder42 On

I'm working on a CloudFormation template using AWS::Serverless, which includes an AWS::Serverless::Api and an AWS::Cognito::UserPoolClient. The issue arises because the Rest API references the UserPool as an authorizer, while the UserPoolClient needs to refer back to the Rest API for the callback URL. Additionally, my lambda function requires environment variables that contain the Cognito client ID and secret. I've tried to set it up in this order: CognitoUserPool -> RestAPI -> CognitoUserPoolClient -> Lambda Function, but I keep hitting a circular dependency error during changeset generation. Is there a way to untangle this circular dependency? Is there a method to inject a common parameter, like the API URL base, into the template?

3 Answers

Answered By TechWhiz90 On

You might want to add DependsOn attributes to clarify the order for CloudFormation. Try adding DependsOn: RestAPI to your SwaggerUI resource and DependsOn: CognitoUserPool to the RestAPI resource. Just remember that DependsOn is not a property; it should be added alongside Properties. This approach could help resolve the confusion CF has with the dependencies.

Answered By DevEnthusiast23 On

When you mention 'SwaggerUI resource', are you referring to the AWS::Serverless::Function? I tried adding Dependencies earlier, but it still didn't solve the circular dependency issue, even with the linter indicating they might be unnecessary.

Answered By CloudGuru77 On

Have you thought about setting the callback URL using a Lambda function that invokes the AWS CLI after the Rest API is created? It could work without having to manage a multi-stage deployment, but you could also use a separate template for the Cognito part with a DependsOn condition to ensure the dependencies are created in the right order.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.