Search code examples
amazon-web-servicesaws-cdkaws-api-gateway

CDK: How to get apigateway key value (ie x-api-key: *20 Chars*)


I'm unable to find out how to get the api key out of an apigateway key. I can get its ID and its ARN but not the value. I know you can specify the value when creating the key, but not how to retrieve it once created--short of logging into the AWS GUI and finding it that way.

I've looked at the documentation for aws-apigateway.ApiKey and couldn't find any way to get the value. https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.ApiKey.html I've also looked at kms keys since you can get their value, but I don't know if it's usable in the context of an API Gateway usage plan (not included in code below).

Failing the ability to get the value, is there a way to generate a value that won't change, or will persist? I'm using an ephemeral Jenkins node to run the CDK.

    const apiGateway  = require('@aws-cdk/aws-apigateway');
...
    const apiKey = new apiGateway.ApiKey(this, 'api-key', {
      apiKeyName: 'my-api-key',
    });
...
    new cdk.CfnOutput(this, 'x-api-key-apiKey_id', {
      value: apiKey.keyId
      });
    new cdk.CfnOutput(this, 'x-api-key-apiKey_keyArn', {
      value: apiKey.keyArn
      });

Solution

  • We can't retrieve the auto generated key via cdk/cloudformation without a custom resource. But we can generate the key , store it in a secret manager or an ssm secret and use that to create api key.

    const secret = new secretsmanager.Secret(this, 'Secret', {
        generateSecretString: {
            generateStringKey: 'api_key',
            secretStringTemplate: JSON.stringify({ username: 'web_user' }),
            excludeCharacters: ' %+~`#$&*()|[]{}:;<>?!\'/@"\\',
        },
    });
    this.restApi.addApiKey('ApiKey', {
        apiKeyName: `web-app-key`,
        value: secret.secretValueFromJson('api_key').toString(),
    });