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(lambda): docker image function fails when insightsVersion is specified #16781

Merged
merged 4 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Expand Up @@ -220,6 +220,10 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
* Specify the version of CloudWatch Lambda insights to use for monitoring
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights.html
*
* When used with `DockerImageFunction` or `DockerImageCode`, the Docker image should have
* the Lambda insights agent installed.
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-Getting-Started-docker.html
*
* @default - No Lambda Insights
*/
readonly insightsVersion?: LambdaInsightsVersion;
Expand Down Expand Up @@ -782,9 +786,7 @@ export class Function extends FunctionBase {
}

// Configure Lambda insights
if (props.insightsVersion !== undefined) {
this.configureLambdaInsights(props.insightsVersion);
}
this.configureLambdaInsights(props);
}

/**
Expand Down Expand Up @@ -912,8 +914,15 @@ Environment variables can be marked for removal when used in Lambda@Edge by sett
*
* https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
*/
private configureLambdaInsights(insightsVersion: LambdaInsightsVersion): void {
this.addLayers(LayerVersion.fromLayerVersionArn(this, 'LambdaInsightsLayer', insightsVersion.layerVersionArn));
private configureLambdaInsights(props: FunctionProps): void {
if (props.insightsVersion === undefined) {
return;
}
if (props.runtime !== Runtime.FROM_IMAGE) {
// Layers cannot be added to Lambda container images. The image should have the insights agent installed.
// See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-Getting-Started-docker.html
this.addLayers(LayerVersion.fromLayerVersionArn(this, 'LambdaInsightsLayer', props.insightsVersion.layerVersionArn));
}
this.role?.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchLambdaInsightsExecutionRolePolicy'));
}

Expand Down
40 changes: 40 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/lambda-insights.test.ts
@@ -1,5 +1,6 @@
import '@aws-cdk/assert-internal/jest';
import { MatchStyle } from '@aws-cdk/assert-internal';
import * as ecr from '@aws-cdk/aws-ecr';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';

Expand Down Expand Up @@ -313,4 +314,43 @@ describe('lambda-insights', () => {
// On synthesis it should not throw an error
expect(() => app.synth()).not.toThrow();
});

test('insights layer is skipped for container images and the role is updated', () => {
const stack = new cdk.Stack();
new lambda.DockerImageFunction(stack, 'MyFunction', {
code: lambda.DockerImageCode.fromEcr(ecr.Repository.fromRepositoryArn(stack, 'MyRepo',
'arn:aws:ecr:us-east-1:0123456789:repository/MyRepo')),
insightsVersion: lambda.LambdaInsightsVersion.VERSION_1_0_98_0,
});

expect(stack).toCountResources('AWS::Lambda::LayerVersion', 0);

expect(stack).toHaveResourceLike('AWS::IAM::Role', {
'AssumeRolePolicyDocument': {
'Statement': [
{
'Action': 'sts:AssumeRole',
'Principal': {
'Service': 'lambda.amazonaws.com',
},
},
],
},
'ManagedPolicyArns': [
{ },
{
'Fn::Join': [
'',
[
'arn:',
{
'Ref': 'AWS::Partition',
},
':iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy',
],
],
},
],
});
});
});