【发布时间】:2022-08-09 04:00:59
【问题描述】:
我正在使用aws-cdk-lib (2.13.0)。这是我的代码的 sn-p:
import { App, Stack } from \'aws-cdk-lib\';
import { Secret } from \'aws-cdk-lib/aws-secretsmanager\';
export class CognitoStack extends Stack {
constructor(scope: App) {
super(scope, \'cognito\');
const secret = this.getSecret(\'google\');
console.log({ secret });
}
public getSecret(path: string) {
const secret = Secret.fromSecretNameV2(this, `Secret${path}`, path);
console.log({ path, secret, secretArn: secret.secretArn, string: secret.secretValue.toString() });
return secret.secretValue.toJSON();
}
}
生成的日志如下所示:
{
path: \'google\',
secret: <ref *1> SecretBase {
node: Node {
host: [Circular *1],
_locked: false,
_children: {},
_context: {},
_metadata: [],
_dependencies: Set(0) {},
_validations: [Array],
id: \'Secretgoogle\',
scope: [CognitoStack]
},
stack: CognitoStack {
node: [Node],
_missingContext: [],
_stackDependencies: {},
templateOptions: {},
_logicalIds: [LogicalIDs],
account: \'${Token[AWS.AccountId.4]}\',
region: \'${Token[AWS.Region.8]}\',
environment: \'aws://unknown-account/unknown-region\',
terminationProtection: undefined,
_stackName: \'cognito\',
tags: [TagManager],
artifactId: \'cognito\',
templateFile: \'cognito.template.json\',
_versionReportingEnabled: true,
synthesizer: [DefaultStackSynthesizer],
[Symbol(@aws-cdk/core.DependableTrait)]: [Object]
},
env: {
account: \'${Token[AWS.AccountId.4]}\',
region: \'${Token[AWS.Region.8]}\'
},
_physicalName: undefined,
_allowCrossEnvironment: false,
physicalName: \'${Token[TOKEN.332]}\',
encryptionKey: undefined,
secretName: \'google\',
secretArn: \'arn:${Token[AWS.Partition.7]}:secretsmanager:${Token[AWS.Region.8]}:${Token[AWS.AccountId.4]}:secret:google\',
autoCreatePolicy: false,
[Symbol(@aws-cdk/core.DependableTrait)]: { dependencyRoots: [Array] }
},
secretArn: \'arn:${Token[AWS.Partition.7]}:secretsmanager:${Token[AWS.Region.8]}:${Token[AWS.AccountId.4]}:secret:google\',
string: \'${Token[TOKEN.333]}\'
}
{ secret: \'<unresolved-token>\' }
npx cdk diff sandbox-cognito 的结果如下所示:
Stack sandbox-cognito
Resources
[~] AWS::Cognito::UserPoolIdentityProvider Google GoogleAF1E99FA
└─ [~] ProviderDetails
├─ [-] Removed: .client_id
└─ [-] Removed: .client_secret
这意味着它正在删除我能够手动设置的 client_id/client_secret。现在我试图从一个秘密加载值,它不起作用。
问题是我无法解析 JSON(请注意日志中的 <unresolved-token>。我认为它尚未解决,但我不确定如何解决......它正在尝试解析这个字符串文字:${Token[TOKEN.333]},而不是秘密值。我怎样才能得到秘密字符串的结果?
标签: typescript aws-cdk aws-secrets-manager