Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-cdk/aws-cognito-identitypool-alpha: Rule based role mapping Circular Dependency #33725

Open
1 task
sarflux opened this issue Mar 10, 2025 · 4 comments · May be fixed by #33820
Open
1 task

aws-cdk/aws-cognito-identitypool-alpha: Rule based role mapping Circular Dependency #33725

sarflux opened this issue Mar 10, 2025 · 4 comments · May be fixed by #33820
Labels
@aws-cdk/aws-cognito-identitypool bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@sarflux
Copy link

sarflux commented Mar 10, 2025

Describe the bug

Describe the bug

We are encountering a circular dependency when configuring rule-based role mapping for an Amazon Cognito Identity Pool using AWS CDK.

The problem arises because:

IAM Role Trust Policy requires the Identity Pool ID (aud condition).
Identity Pool Role Mapping requires the IAM Role ARN during initialization.

This creates a dependency loop where:

The Identity Pool cannot be created without the IAM Role.
The IAM Role cannot be created without the Identity Pool ID.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

CDK should allow defining role mappings without causing a circular dependency when the IAM role trust policy depends on the Identity Pool ID.

Current Behavior

There is no way to reference the identity pool ID in the IAM role before its creation

Reproduction Steps

client_role_principal = iam.WebIdentityPrincipal(
            "cognito-identity.amazonaws.com",
            {
                "StringEquals": {"cognito-identity.amazonaws.com:aud": "<Identity Pool ID>"},
                "ForAnyValue:StringLike": {"cognito-identity.amazonaws.com:amr": "authenticated"},
            },
        )

self.myapp_role = iam.Role(self, f"{client_name}Role-{stage}", assumed_by=client_role_principal)


role_mappings = [
    cognito_idp.IdentityPoolRoleMapping(
        mapping_key="myapp",
        provider_url=cognito_idp.IdentityPoolProviderUrl.user_pool(
            self.cognito_user_pool, self.myapp
        ),
        rules=[
            cognito_idp.RoleMappingRule(
                claim="aud",
                claim_value=self.myapp_client.user_pool_client_id,
                mapped_role=self.myapp_role,  # Requires IAM Role ARN
                match_type=cognito_idp.RoleMappingMatchType.EQUALS,
            )
        ]
    )
]

cognito_identity_pool = cognito_idp.IdentityPool(
    self,
    "MyIdentityPool",
    allow_classic_flow=False,
    allow_unauthenticated_identities=False,
    authentication_providers=cognito_idp.IdentityPoolAuthenticationProviders(
        user_pools=[
            cognito_idp.UserPoolAuthenticationProvider(
                user_pool=self.cognito_user_pool,
                user_pool_client=self.myapp_client,
            )
        ],
    ),
    role_mappings=role_mappings,  # Requires IAM Role ARN
)

Possible Solution

CDK could support lazy evaluation for IAM roles in role mappings, allowing:

The Identity Pool to be created first.
The IAM role mapping to be applied afterward without breaking dependencies.

Additional Information/Context

No response

CDK CLI Version

2.1001.0 (build 130445d)

Framework Version

No response

Node.js Version

v20.18.0

OS

Ubuntu 22.04.5 LTS

Language

Python

Language Version

Python 3.12.7

Other information

No response

@sarflux sarflux added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 10, 2025
@pahud pahud self-assigned this Mar 12, 2025
@pahud
Copy link
Contributor

pahud commented Mar 12, 2025

Hi,

Thank you for the report.

The Problem

When setting up Amazon Cognito Identity Pool with rule-based role mapping, we encounter a "chicken and egg" situation:

flowchart TD
%% Problem Section
subgraph Problem["The Problem"]
direction TB
A[Identity Pool with Role Mapping] -->|needs| B[IAM Role ARN]
B -->|needs| C[Identity Pool ID]
C -->|circular dependency| A
end
Loading
  1. Identity Pool Side:
  • When you create an Identity Pool with rule-based role mappings
  • Each rule needs to specify which IAM Role should be assigned
  • This means the IAM Role must exist first
  1. IAM Role Side:
  • The IAM Role's trust policy needs to specify which Identity Pool can assume it
  • It does this by including the Identity Pool ID in its conditions:
{
  "StringEquals": {
    "cognito-identity.amazonaws.com:aud": "us-east-1:12345678-abcd-efgh..."
  }
}
  • This means the Identity Pool must exist first

This creates a circular dependency:

  1. Can't create Identity Pool without IAM Role
  2. Can't create IAM Role without Identity Pool ID

We break this circular dependency by splitting the process into three sequential steps:

flowchart TD
%% Solution Section
subgraph Solution["Breaking the Chain"]
    direction TB
    D[Create Identity Pool] -->|get| E[Identity Pool ID]
    E -->|use in| F[Create IAM Role]
    F -->|use both in| G[Create Role Mapping]
    E -->|use both in| G
end

classDef default fill:#2d2d2d,stroke:#7f7f7f,color:white
Loading

Step 1: Create Identity Pool First

  • Create the Identity Pool without any role mappings
  • This gives us the Identity Pool ID we need

Step 2: Create IAM Role

  • Now that we have the Identity Pool ID
  • Create the IAM Role with the correct trust policy
  • Use the Identity Pool ID in the trust policy conditions

Step 3: Create Role Attachment

  • Use a separate CfnIdentityPoolRoleAttachment resource
  • This connects the IAM Role to the Identity Pool
  • Configure the rule-based mappings here
  • We can do this because we now have both:
    • The Identity Pool ID
    • The IAM Role ARN

By separating the role attachment into its own resource, we avoid the circular dependency and allow CloudFormation to create the resources in the correct order.

Let me know if it works for you.

@pahud pahud added the p3 label Mar 12, 2025
@pahud pahud removed their assignment Mar 12, 2025
@pahud pahud added effort/medium Medium work item – several days of effort response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 12, 2025
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 14, 2025
@sarflux
Copy link
Author

sarflux commented Mar 14, 2025

Hi @pahud , Thank you for your response.

Your understanding of the problem and the resolution look good to me.

Using the L1 construct for creating the role attachment, I can synth this correctly, but haven't tested with a deployment as I am still developing.

Would there be support to do this with the aws-cognito-identitypool-alpha library in the future?

I will let you know if I face any other problems.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 15, 2025
@sarflux
Copy link
Author

sarflux commented Mar 21, 2025

Hi,

Thanks for your response and the proposed solution. I investigated the suggested approach, but encountered an error when trying to create the AWS::Cognito::IdentityPoolRoleAttachment separately:

'AWS::Cognito::IdentityPoolRoleAttachment' with identifier 'us-east-1:' already exists.

This indicates that the Identity Pool Role Attachment cannot be created later as a separate resource, likely because the Identity Pool is already associated with a role attachment. Given this constraint, an alternative approach may be required to avoid the circular dependency while ensuring that the role mappings are correctly established.

Let me know if you have any suggestions or if there are any recommended workarounds for this scenario.

Best,
Sarthak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cognito-identitypool bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants