|
| 1 | +# RFC: AWS Lambda - Metadata about Code Assets |
| 2 | + |
| 3 | +As described in [#1432](https://github.com/awslabs/aws-cdk/issues/1432), in order to support local debugging, |
| 4 | +debuggers like [SAM CLI](https://github.com/awslabs/aws-sam-cli) need to be able to find the code of a Lambda |
| 5 | +function locally. |
| 6 | + |
| 7 | +The current implementation of assets uses CloudFormation Parameters which represent the S3 bucket and key of the |
| 8 | +uploaded asset, which makes it impossible for local tools to reason about (without traversing the cx protocol with |
| 9 | +many heuristics). |
| 10 | + |
| 11 | +## Approach |
| 12 | + |
| 13 | +We will automatically embed CloudFormation metadata on `AWS::Lambda::Function` (and any other) resources which use |
| 14 | +local assets for code. The metadata will allow tools like SAM CLI to find the code locally for local invocations. |
| 15 | + |
| 16 | +Given a CDK app with an AWS Lambda function defined like so: |
| 17 | + |
| 18 | +```ts |
| 19 | +new lambda.Function(this, 'MyHandler', { |
| 20 | + // ... |
| 21 | + code: lambda.Code.asset('/path/to/handler') |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +The synthesized `AWS::Lambda::Function` resource will include a "Metadata" entry as follows: |
| 26 | + |
| 27 | +```js |
| 28 | +{ |
| 29 | + "Type": "AWS::Lambda::Function", |
| 30 | + "Properties": { |
| 31 | + "Code": { |
| 32 | + // current asset magic |
| 33 | + } |
| 34 | + }, |
| 35 | + "Metadata": { |
| 36 | + "aws:asset:property": "Code", |
| 37 | + "aws:asset:path": "/path/to/handler" |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Local debugging tools like SAM CLI will be able to traverse the template and look up the `aws:asset` metadata |
| 43 | +entries, and use them to process the template so it will be compatible with their inputs. |
| 44 | + |
| 45 | +## Design |
| 46 | + |
| 47 | +We will add a new method to the `Asset` class called `addResourceMetadata(resource, propName)`. This method will |
| 48 | +take in an L1 resource (`cdk.Resource`) and a property name (`string`) and will add template metadata as |
| 49 | +described above. |
| 50 | + |
| 51 | +This feature will be enabled by a context key `aws:cdk:enable-asset-metadata`, which will be enabled by default in |
| 52 | +the CDK Toolkit. The switch `--asset-metadata` (or `--no-asset-metadata`) can be used to control this behavior, as |
| 53 | +well as through the key `assetMetadata` in `cdk.json`. Very similar design to how path metadata is controlled. |
| 54 | + |
| 55 | +## Alternatives Considered |
| 56 | + |
| 57 | +We considered alternatives that will "enforce" the embedding of metadata when an asset is referenced by a resource. Since |
| 58 | +a single asset can be referenced by multiple resources, it means that the _relationship_ is what should trigger the |
| 59 | +metadata addition. There currently isn't support in the framework for such hooks, but there is a possiblility that |
| 60 | +the changes in [#1436](https://github.com/awslabs/aws-cdk/pull/1436) might enable hooking into the relationnship, and then we might be able to use this mechanism to produce the metadata. |
| 61 | + |
| 62 | +Having said that, the need to embed asset metadata on resources is mainly confined to authors of L2 constructs, and not applicable for the general user population, so the value of automation is not high. |
| 63 | + |
| 64 | +## What about L1 resources? |
| 65 | + |
| 66 | +If users directly use L1 resources such as `lambda.CfnFunction` or `serverless.CfnFunction` and wish to use local CDK assets with tooling, they will have to explicitly add metadata: |
| 67 | + |
| 68 | +```ts |
| 69 | +const asset = new assets.ZipDirectoryAsset(this, 'Foo', { |
| 70 | + path: '/foo/boom' |
| 71 | +}); |
| 72 | + |
| 73 | +const resource = new serverless.CfnFunction(this, 'Func', { |
| 74 | + codeUri: { |
| 75 | + bucket: asset.s3BucketName, |
| 76 | + key: asset.s3ObjectKey |
| 77 | + }, |
| 78 | + runtime: 'nodejs8.10', |
| 79 | + handler: 'index.handler' |
| 80 | +}); |
| 81 | + |
| 82 | +resource.addResourceMetadata(resource, 'CodeUri'); |
| 83 | +``` |
0 commit comments