我在使用 CDK/CloudFormation/CLI 将自定义属性设置为只读时遇到问题。它们是在用户池中定义的并且是可变的,我可以在控制台中很好地完成它,但不能以任何其他方式完成。
我尝试过使用 CDK (java)
ClientAttributes writeAttributes = new ClientAttributes();
和
ClientAttributes writeAttributes = new ClientAttributes().withCustomAttributes();
以及相应的 CloudFormation YAML
WriteAttributes:[]
但它们仍然是可写的。如果我尝试定位单个属性而不是全部空白,则会收到“更新客户端时指定的写入属性无效”。无论 CDK、CloudFormation YAML 还是 CLI,结果都是相同的(根据尝试方法使用“custom:”前缀进行调整)
建议?
提前致谢,
首先,确保您拥有最新版本的 AWS CDK 并检查 AWS 文档中是否有任何更新。
根据 CDK API 参考文档,您可以通过以下方式设置只读范围:
我还提供了以下代码片段,可以帮助您:
// Required import statements
import software.amazon.awscdk.core.App;
import ....
import ....
public class TestCognitoStack extends Stack {
public TestCognitoStack(final App parent, final String name) {
super(parent, name);
// Create a Cognito User Pool
UserPool userPool = ....
// Add custom attributes to the User Pool
userPool.addCustomAttribute("...");
// Create a User Pool Client
UserPoolClient userPoolClient = ...
// Make custom attributes read-only
ClientAttributes readOnlyAttributes = ClientAttributes.builder()
.customAttributes(Map.of("customAttribute1", true, "customAttribute2", true))
.build();
userPoolClient.setWriteAttributes(readOnlyAttributes);
}
}
在此示例中,setWriteAttributes 方法用于将用户池客户端的自定义属性设置为只读。确保将占位符值替换为您的实际值。