Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assets): SAM asset metadata missing from log retention and custom resource provider functions #17551

Merged
merged 15 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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