Skip to content

aws-cdk: Ability to add metadata to assets and retain original source path of assets #27415

Open
@sagar794

Description

@sagar794

Describe the feature

  1. The ability to add metadata to assets and have that metadata available for consumption post cdk synth
  2. For CDK to automatically add the original source file path regardless of whether the asset is staged.

Use Case

I want my CI system to validate the origin of the assets that were added to the cdk.out directory during the cdk synth execution. Today, when Assets are staged, they are copied over to cdk.out as part of cdk synth. Within the <stackId>.asset.json we can see the path to the staged asset, but not the original source path.

For example, when viewing this <stackId>.asset.json file I know the staged asset is named asset.97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip, but not which file on the local was used to create this asset.

{
  "version": "33.0.0",
  "files": {
    "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df": {
      "source": {
        "path": "asset.97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",   
        "packaging": "file"
      },
      "destinations": {
        "<aws-account-number>-<aws-region>": {
          "bucketName": "<bucket-name>",
          "objectKey": "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",
          "region": "<aws-region>"
        }
      }
    },
  "dockerImages": {}
}

The motivation for this feature is to be able to determine the origin of the asset using the <stackId>.assets.json file.

We also have created a function that allows us to download assets from a trusted external source, but would like to be able to audit the external source used to create the asset later.

Proposed Solution

If possible to safely add new keys to the <stackId>.asset.json files, then I would propose adding a new metadata key where metadata by CDK and custom metadata added by users could reside post cdk synth.

{
  "version": "33.0.0",
  "files": {
    "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df": {
      "source": {
        "path": "asset.97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",   
        "packaging": "file",
        "metadata": {
         "@aws-cdk/originalSourcePath": "relative/path/to/file",
         "someUserKey": "someUserValue"
        }
      },
      "destinations": {
        "<aws-account-number>-<aws-region>": {
          "bucketName": "<bucket-name>",
          "objectKey": "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",
          "region": "<aws-region>"
        }
      }
    },
  "dockerImages": {}
}

As for the original source path, I would expect that data to be added by CDK.

For lambda.fromAsset('path/to/file') I would expect "@aws-cdk/originalSourcePath": "path/to/file" to be in the metadata section. For constructs like NodejsFunction it would be a bit tricker since entry is not a required field in the props, but would like to know what value CDK resolved to for that. The same logic would follow for all other ways assets can get added with CDK.

Users should be able to add their own metadata to assets as well. This could possibly be done by adding a new metadata parameter to AssetProps for the Asset construct. All functions which create assets could then pass the metadata as an input. For my use case, this would be used to add the trusted external source URL that was used as metadata which could get audited later.

Alternatively, the original source path could be it's own key similar to path and packaging outside of the metadata section (i.e. originalSourcePath).

{
  "version": "33.0.0",
  "files": {
    "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df": {
      "source": {
        "path": "asset.97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",
        "originalSourcePath": "relative/path/to/file",   
        "packaging": "file",
        "metadata": {
         "someUserKey": "someUserValue"
        }
      },
      "destinations": {
        "<aws-account-number>-<aws-region>": {
          "bucketName": "<bucket-name>",
          "objectKey": "97c324c84f5d023be4edee540cb2cb401a49f115d01ed403b288f6cb412771df.zip",
          "region": "<aws-region>"
        }
      }
    },
  "dockerImages": {}
}

Other Information

This issue is similar, but not what I am looking for here since this is adding metadata to the CloudFormation template.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.93.0

Environment details (OS name and version, etc.)

macOS Ventura (13.6)

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/assetsRelated to the @aws-cdk/assets packageeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions