Skip to content

Commit

Permalink
fix(assets): SAM asset metadata missing from log retention and custom…
Browse files Browse the repository at this point in the history
… resource provider functions (#17551)

----
Following up on issue #14593 and PR #1433. 
It seems that log retention and customer resource provider constructs create the corresponding lambda functions using ```CfnResource()``` which means that the asset metadata isn't appended to the output template. 

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mildaniel authored Nov 19, 2021
1 parent 3af985b commit a90e959
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-logs/lib/log-retention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class LogRetentionFunction extends CoreConstruct implements cdk.ITaggable {
});
this.functionArn = resource.getAtt('Arn');

asset.addResourceMetadata(resource, 'Code');

// Function dependencies
role.node.children.forEach((child) => {
if (cdk.CfnResource.isCfnResource(child)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-s3-assets": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"constructs": "^3.3.69"
},
"homepage": "https://github.com/aws/aws-cdk",
Expand All @@ -108,6 +109,7 @@
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-s3-assets": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"constructs": "^3.3.69"
},
"engines": {
Expand Down
28 changes: 27 additions & 1 deletion packages/@aws-cdk/aws-logs/test/log-retention.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as path from 'path';
import '@aws-cdk/assert-internal/jest';
import { ABSENT } from '@aws-cdk/assert-internal';
import { ABSENT, ResourcePart } from '@aws-cdk/assert-internal';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { LogRetention, RetentionDays } from '../lib';

/* eslint-disable quote-props */
Expand Down Expand Up @@ -175,4 +177,28 @@ describe('log retention', () => {
});

});

test('asset metadata added to log retention construct lambda function', () => {
// GIVEN
const stack = new cdk.Stack();
stack.node.setContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT, true);
stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, true);

const assetLocation = path.join(__dirname, '../', '/lib', '/log-retention-provider');

// WHEN
new LogRetention(stack, 'MyLambda', {
logGroupName: 'group',
retention: RetentionDays.ONE_MONTH,
});

// Then
expect(stack).toHaveResource('AWS::Lambda::Function', {
Metadata: {
'aws:asset:path': assetLocation,
'aws:asset:property': 'Code',
},
}, ResourcePart.CompleteDefinition);

});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { AssetStaging } from '../asset-staging';
import { FileAssetPackaging } from '../assets';
Expand Down Expand Up @@ -189,8 +190,10 @@ export class CustomResourceProvider extends CoreConstruct {
sourcePath: props.codeDirectory,
});

const assetFileName = staging.relativeStagedPath(stack);

const asset = stack.synthesizer.addFileAsset({
fileName: staging.relativeStagedPath(stack),
fileName: assetFileName,
sourceHash: staging.assetHash,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
});
Expand Down Expand Up @@ -242,6 +245,11 @@ export class CustomResourceProvider extends CoreConstruct {

handler.addDependsOn(role);

if (this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)) {
handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PATH_KEY, assetFileName);
handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY, 'Code');
}

this.serviceToken = Token.asString(handler.getAtt('Arn'));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { App, AssetStaging, CustomResourceProvider, CustomResourceProviderRuntime, DockerImageAssetLocation, DockerImageAssetSource, Duration, FileAssetLocation, FileAssetSource, ISynthesisSession, Size, Stack } from '../../lib';
import { toCloudFormation } from '../util';

Expand Down Expand Up @@ -122,6 +123,28 @@ describe('custom resource provider', () => {

});

test('asset metadata added to custom resource that contains code definition', () => {
// GIVEN
const stack = new Stack();
stack.node.setContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT, true);
stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, true);

// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12_X,
});

// Then
const lambda = toCloudFormation(stack).Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
expect(lambda).toHaveProperty('Metadata');
expect(lambda.Metadata).toEqual({
'aws:asset:path': `${__dirname}/mock-provider`,
'aws:asset:property': 'Code',
});

});

test('custom resource provided creates asset in new-style synthesis with relative path', () => {
// GIVEN

Expand Down

0 comments on commit a90e959

Please sign in to comment.