从使用 CDK 创建的 AWS AppSync API 获取生成的 API 密钥

发布于 2025-01-09 11:10:38 字数 579 浏览 3 评论 0原文

我正在尝试从我正在创建 AppSync API 的堆栈中访问数据。我希望能够使用生成的 Stacks 的 urlapiKey,但我遇到了对它们进行编码/标记化的问题。

在我的堆栈中,我将一些字段设置为已部署堆栈的输出:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2025-01-16 11:10:38

此类令牌的实际值仅在部署时可用-时间。在此之前,您可以在 CDK 代码中的构造之间安全地传递这些令牌属性,但在部署之前它们是不透明的占位符。根据您的使用案例,以下选项之一可以帮助检索部署时值:

如果您为变量定义 CloudFormation 输出,CDK 将(除了在 CloudFormation 中创建它)在 cdk 部署之后>,将其值打印到控制台,并可选择将其写入使用 --outputs-file 标志传递的 json 文件。

// AppsyncStack.ts
new cdk.CfnOutput(this, 'ApiKey', {
  value: this.api.apiKey ?? 'UNDEFINED',
  exportName: 'api-key',
});
// at deploy-time, if you use a flag: --outputs-file cdk.outputs.json
{
  "AppsyncStack": {
    "ApiKey": "da2-ou5z5di6kjcophixxxxxxxxxx",
    "GraphQlUrl": "https://xxxxxxxxxxxxxxxxx.appsync-api.us-east-1.amazonaws.com/graphql"
  }
}

或者,您可以编写一个脚本来使用 listGraphqlApislistApiKeys 来自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 a json file you pass with the --outputs-file flag.

// AppsyncStack.ts
new cdk.CfnOutput(this, 'ApiKey', {
  value: this.api.apiKey ?? 'UNDEFINED',
  exportName: 'api-key',
});
// at deploy-time, if you use a flag: --outputs-file cdk.outputs.json
{
  "AppsyncStack": {
    "ApiKey": "da2-ou5z5di6kjcophixxxxxxxxxx",
    "GraphQlUrl": "https://xxxxxxxxxxxxxxxxx.appsync-api.us-east-1.amazonaws.com/graphql"
  }
}

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.

当梦初醒 2025-01-16 11:10:38

感谢 @fedonev,我能够像这样提取 API 密钥和 url:

const client = new AppSyncClient({ region: "eu-north-1" });
  const command = new ListGraphqlApisCommand({ maxResults: 1 });
  const res = await client.send(command);
  if (res.graphqlApis) {
    const apiKeysCommand = new ListApiKeysCommand({
      apiId: res.graphqlApis[0].apiId,
    });
    const apiKeyResponse = await client.send(apiKeysCommand);
    const urls = flatMap(res.graphqlApis[0].uris);
    if (apiKeyResponse.apiKeys && res.graphqlApis[0].uris) {
      sendSlackMessage(urls[1], apiKeyResponse.apiKeys[0].id || "");
    }
  }

Thanks to @fedonev I was able to extract the API key and url like so:

const client = new AppSyncClient({ region: "eu-north-1" });
  const command = new ListGraphqlApisCommand({ maxResults: 1 });
  const res = await client.send(command);
  if (res.graphqlApis) {
    const apiKeysCommand = new ListApiKeysCommand({
      apiId: res.graphqlApis[0].apiId,
    });
    const apiKeyResponse = await client.send(apiKeysCommand);
    const urls = flatMap(res.graphqlApis[0].uris);
    if (apiKeyResponse.apiKeys && res.graphqlApis[0].uris) {
      sendSlackMessage(urls[1], apiKeyResponse.apiKeys[0].id || "");
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文