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): Use Alias ARN directly #2091

Merged
merged 7 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 21 additions & 15 deletions packages/@aws-cdk/aws-lambda/lib/alias.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import iam = require('@aws-cdk/aws-iam');
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import cdk = require('@aws-cdk/cdk');
import { FunctionBase, FunctionImportProps, IFunction } from './function-base';
import { Version } from './lambda-version';
import { CfnAlias } from './lambda.generated';
import { Permission } from './permission';

/**
* Properties for a new Lambda alias
Expand Down Expand Up @@ -68,11 +67,6 @@ export class Alias extends FunctionBase {
*/
public readonly functionArn: string;

/**
* Role associated with this alias
*/
public readonly role?: iam.IRole | undefined;

protected readonly canCreatePermissions: boolean = true; // Not used anyway

/**
Expand All @@ -85,7 +79,7 @@ export class Alias extends FunctionBase {

this.underlyingLambda = props.version.lambda;

new CfnAlias(this, 'Resource', {
const alias = new CfnAlias(this, 'Resource', {
name: props.aliasName,
description: props.description,
functionName: this.underlyingLambda.functionName,
Expand All @@ -95,8 +89,25 @@ export class Alias extends FunctionBase {

// Not actually the name, but an ARN can be used in all places
// where the name is expected, and an ARN can refer to an Alias.
this.functionName = `${props.version.lambda.functionArn}:${props.aliasName}`;
this.functionArn = `${props.version.lambda.functionArn}:${props.aliasName}`;
this.functionName = `${this.underlyingLambda.functionName}:${props.aliasName}`;
this.functionArn = alias.aliasArn;
}

/**
* Role associated with this alias
*/
public get role() {
return this.underlyingLambda.role;
}

public metric(metricName: string, props?: cloudwatch.MetricCustomization): cloudwatch.Metric {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
return super.metric(metricName, {
dimensions: {
FunctionName: this.underlyingLambda.functionName,
Resource: this.functionArn
},
...(props || {})
});
}

public export(): FunctionImportProps {
Expand All @@ -105,11 +116,6 @@ export class Alias extends FunctionBase {
};
}

public addPermission(name: string, permission: Permission) {
// Forward addPermission() to the underlying Lambda object
this.underlyingLambda.addPermission(name, permission);
}

/**
* Calculate the routingConfig parameter from the input props
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export abstract class FunctionBase extends cdk.Construct implements IFunction {
new CfnPermission(this, id, {
action,
principal,
functionName: this.functionName,
functionName: this.functionArn,
eventSourceToken: permission.eventSourceToken,
sourceAccount: permission.sourceAccount,
sourceArn: permission.sourceArn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction3BAA72D1"
"Fn::GetAtt": [
"MyFunction3BAA72D1",
"Arn"
]
},
"Principal": "s3.amazonaws.com",
"SourceAccount": {
Expand All @@ -129,7 +132,10 @@
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction3BAA72D1"
"Fn::GetAtt": [
"MyFunction3BAA72D1",
"Arn"
]
},
"Principal": "s3.amazonaws.com",
"SourceAccount": {
Expand Down
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-lambda/test/integ.events.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunc8A243A2C"
"Fn::GetAtt": [
"MyFunc8A243A2C",
"Arn"
]
},
"Principal": "events.amazonaws.com",
"SourceArn": {
Expand All @@ -81,7 +84,10 @@
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunc8A243A2C"
"Fn::GetAtt": [
"MyFunc8A243A2C",
"Arn"
]
},
"Principal": "events.amazonaws.com",
"SourceArn": {
Expand Down
44 changes: 43 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/test.alias.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');

import { beASupersetOfTemplate, expect, haveResource } from '@aws-cdk/assert';
import { AccountPrincipal } from '@aws-cdk/aws-iam';
import { Stack } from '@aws-cdk/cdk';
Expand Down Expand Up @@ -123,10 +125,50 @@ export = {

// THEN
expect(stack).to(haveResource('AWS::Lambda::Permission', {
FunctionName: stack.node.resolve(fn.functionName),
FunctionName: {
Ref: "Alias325C5727"
},
Principal: "123456"
}));

test.done();
},

'metric adds Resource: aliasArn to dimensions'(test: Test) {
const stack = new Stack();

// GIVEN
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NodeJS610,
});

const version = fn.addVersion('1');
const alias = new lambda.Alias(stack, 'Alias', { aliasName: 'prod', version });

// WHEN
new cloudwatch.Alarm(stack, 'Alarm', {
metric: alias.metric('Test'),
alarmName: 'Test',
threshold: 1,
evaluationPeriods: 1
});

expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
Dimensions: [{
Name: "FunctionName",
Value: {
Ref: "MyLambdaCCE802FB"
}
}, {
Name: "Resource",
Value: {
Ref: "Alias325C5727"
}
}]
}));

test.done();
}
};
23 changes: 18 additions & 5 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ export = {
"Handler": "bar",
"Role": {
"Fn::GetAtt": [
"MyLambdaServiceRole4539ECB6",
"Arn"
"MyLambdaServiceRole4539ECB6",
"Arn"
]
},
"Runtime": "python2.7"
Expand All @@ -168,7 +168,10 @@ export = {
"Properties": {
"Action": "lambda:*",
"FunctionName": {
"Ref": "MyLambdaCCE802FB"
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
"Principal": "s3.amazonaws.com",
"SourceAccount": {
Expand Down Expand Up @@ -269,14 +272,24 @@ export = {

expect(stack).to(haveResource('AWS::Lambda::Permission', {
"Action": "lambda:InvokeFunction",
"FunctionName": { "Ref": lambdaId },
"FunctionName": {
"Fn::GetAtt": [
lambdaId,
"Arn"
]
},
"Principal": "events.amazonaws.com",
"SourceArn": { "Fn::GetAtt": [ "Rule4C995B7F", "Arn" ] }
}));

expect(stack).to(haveResource('AWS::Lambda::Permission', {
"Action": "lambda:InvokeFunction",
"FunctionName": { "Ref": "MyLambdaCCE802FB" },
"FunctionName": {
"Fn::GetAtt": [
lambdaId,
"Arn"
]
},
"Principal": "events.amazonaws.com",
"SourceArn": { "Fn::GetAtt": [ "Rule270732244", "Arn" ] }
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export = {
// THEN: Lambda has permissions to be invoked by CWL
expect(stack).to(haveResource('AWS::Lambda::Permission', {
Action: "lambda:InvokeFunction",
FunctionName: { Ref: "MyLambdaCCE802FB" },
FunctionName: {
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
Principal: { "Fn::Join": ["", ["logs.", {Ref: "AWS::Region"}, ".amazonaws.com"]] }
}));

Expand Down