从使用 CDK 创建的 AWS AppSync API 获取生成的 API 密钥
我正在尝试从我正在创建 AppSync API 的堆栈中访问数据。我希望能够使用生成的 Stacks 的 url
和 apiKey
,但我遇到了对它们进行编码/标记化的问题。
在我的堆栈中,我将一些字段设置为已部署堆栈的输出:
this.ApiEndpoint = graphAPI.url;
this.Authorization = graphAPI.graphqlApi.apiKey;
当尝试访问这些属性时,我得到类似 ${Token[TOKEN.209]}
的内容,而不是值。
如果我尝试像这样解析令牌: this.resolve(graphAPI.graphqlApi.apiKey)
我会得到 { 'Fn::GetAtt': [ 'AppSyncAPIApiDefaultApiKey537321373E', 'ApiKey '] }
。
但我想以字符串形式检索密钥本身,例如 da2-10lksdkxn4slcrahnf4ka5zpeemq5i。
我将如何实际提取这些属性的字符串值?
I'm trying to access data from my stack where I'm creating an AppSync API. I want to be able to use the generated Stacks' url
and apiKey
but I'm running into issues with them being encoded/tokenized.
In my stack I'm setting some fields to the outputs of the deployed stack:
this.ApiEndpoint = graphAPI.url;
this.Authorization = graphAPI.graphqlApi.apiKey;
When trying to access these properties I get something like ${Token[TOKEN.209]}
and not the values.
If I'm trying to resolve the token like so: this.resolve(graphAPI.graphqlApi.apiKey)
I instead get { 'Fn::GetAtt': [ 'AppSyncAPIApiDefaultApiKey537321373E', 'ApiKey' ] }
.
But I would like to retrieve the key itself as a string, like da2-10lksdkxn4slcrahnf4ka5zpeemq5i
.
How would I go about actually extracting the string values for these properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此类令牌的实际值仅在部署时可用-时间。在此之前,您可以在 CDK 代码中的构造之间安全地传递这些令牌属性,但在部署之前它们是不透明的占位符。根据您的使用案例,以下选项之一可以帮助检索部署时值:
如果您为变量定义 CloudFormation 输出,CDK 将(除了在 CloudFormation 中创建它)在 cdk 部署之后>,将其值打印到控制台,并可选择将其写入使用
--outputs-file
标志传递的json
文件。或者,您可以编写一个脚本来使用 listGraphqlApis 和 listApiKeys 来自appsync JS SDK 客户端。您可以在本地运行脚本,或者对于高级用例,将脚本包装在 CDK 自定义资源 构造用于部署时集成。
The actual values of such Tokens are available only at deploy-time. Before then you can safely pass these token properties between constructs in your CDK code, but they are opaque placeholders until deployed. Depending on your use case, one of these options can help retrieve the deploy-time values:
If you define CloudFormation Outputs for a variable, CDK will (apart from creating it in CloudFormation), will, after
cdk deploy
, print its value to the console and optionally write it to ajson
file you pass with the--outputs-file
flag.Alternatively, you can write a script to fetch the data post-deploy using the listGraphqlApis and listApiKeys commands from the appsync JS SDK client. You can run the script locally or, for advanced use cases, wrap the script in a CDK Custom Resource construct for deploy-time integration.
感谢 @fedonev,我能够像这样提取 API 密钥和 url:
Thanks to @fedonev I was able to extract the API key and url like so: