Home > other >  Is there a way to remove code duplication when using secrets in AWS CDK?
Is there a way to remove code duplication when using secrets in AWS CDK?

Time:12-31

I have the following code in my CDK pipeline stack:

        const githubSecretId = "token"
        const secret = new secretsmanager.Secret(this, githubSecretId)

        const pipeline = new CodePipeline(this, "Example4BePipeline", {
            synth: new CodeBuildStep("Synth", {
                    input: CodePipelineSource.gitHub("username/example4-be", "main", {
                        authentication: cdk.SecretValue.secretsManager(githubSecretId)
                    }),
                    // other stuff
                }
            )
        })

        pipeline.node.addDependency(secret)

Is it possible to get the id from secret or somehow pass it to CodePipelineSource.gitHub to avoid having an extra variable githubSecretId? Are there any other APIs that could make this code shorter and more concise that I'm missing?

CodePudding user response:

secretsmanager.Secret's secretValue attribute returns theSecretValue type directly.

authentication: secret.secretValue

CodePudding user response:

cdk.SecretValue.secretsManager takes a physical name or ARN of the secret, not the logical ID (the "ID" mentioned in the docs is the physical ID. It would probably be a good idea to clarify this). You can get the former with secret.secretName and the latter with secret.secretArn.

Refer to @fedonev's answer for the solution in this particular case, though, it's much cleaner.

  • Related