aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws-backup: Unable to consume AwsCustomResource as parameter for BackupVault

Open tdq132 opened this issue 4 years ago • 2 comments

Describe the bug

I am attempting to consume a custom_resources.AwsCustomResource output as the encryption_key for a new aws_backup.BackupVault.

In summary, the AwsCustomResource takes a KMS key alias and returns the original KMS key ARN.

When referencing the AwsCustomResource with method get_response_field_reference, CDK appears to ignore the parameter and has no encryption configuration for the resource in in cdk.out.

Expected Behavior

When I am referencing the AwsCustomResource within aws_backup.BackupVault, it should be able to use the returned value.

Current Behavior

The parameter encryption_key is ignored completely.

Reproduction Steps

(truncated, but should hopefully provide enough detail!)

# Custom resource to lookup the actual KMS key ARN from the Alias
KmsKeyArn = custom_resources.AwsCustomResource(
    self,
    f"KmsKeyIdLookup",
    resource_type="Custom::KmsKeyIdLookup",
    on_create=custom_resources.AwsSdkCall(
        physical_resource_id=custom_resources.PhysicalResourceId.from_response(
            "KeyMetadata.Arn",
        ),
        parameters={"KeyId": KmsKeyLookup.key_id},
        service="KMS",
        action="describeKey",
    ),
    policy=custom_resources.AwsCustomResourcePolicy.from_sdk_calls(
        resources=["*"],
    ),
)


# Creating a backup vault
vault = backup.BackupVault(
    self,
    f"{params['VaultName']}",
    backup_vault_name=params["VaultName"],
    encryption_key=KmsKeyArn.get_response_field_reference("KeyMetadata.Arn"),  # Referencing AwsCustomResource - this does not work and it is ignored
    access_policy=vault_access_policy,
)

Possible Solution

I have overridden encryption_key_arn with the following:

cfn_vault = typing.cast(backup.CfnBackupVault, vault.node.default_child)
cfn_vault.encryption_key_arn = KmsKeyArn.get_response_field_reference("KeyMetadata.Arn").to_string()

Additional Information/Context

No response

CDK CLI Version

2.8.0 (build 8a5eb49)

Framework Version

No response

Node.js Version

v16.14.2

OS

Ubuntu 20.04 on WSL

Language

Python

Language Version

Python 3.8.10

Other information

No response

tdq132 avatar Apr 29 '22 01:04 tdq132

Since aws_backup.BackupVault.encryption_key takes in an IKey, I suggest trying something like this:

encryption_key=kms.Key.from_key_arn(self, 'encryption_key', KmsKeyArn.get_response_field_reference("KeyMetadata.Arn").to_string())

peterwoodworth avatar May 03 '22 23:05 peterwoodworth

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar May 06 '22 00:05 github-actions[bot]