'pulumi dependsOn not wroking for role assignments

I'm using pulumi to deploy my Azure stack. I've created a "user assigned identity" for my storage account and defined a "custom role" and a "role assignment" as below. I also made the assignment dependsOn the role. But every time I deploy it I'll get an error:

azure:authorization:Assignment (my-role-assignment):
  error: Error loading Role Definition List: could not find role 'my-role'
const roleName = "my-role";
const role = new azure.authorization.RoleDefinition(
    roleName,
    {
        name: roleName,
        permissions: [ /* permissions here */ ],
        scope: scope,
        assignableScopes: [scope],
    },
    { parent: this }
);

new azure.authorization.Assignment(
    "my-role-assignment",
    {
        principalId: userAssignedIdentity.principalId,
        roleDefinitionName: roleName,
        scope: storageAccountId,
    },
    {
        parent: userAssignedIdentity,
        dependsOn: [role]
    }
);

It will work if I re-deploy the stack. But why is it always failing on the first try? How can I avoid it without inserting a long sleep in between?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source