Skip to content

Commit

Permalink
feat: allow to create a version from an arn
Browse files Browse the repository at this point in the history
This commit allows to reference a lambda from a different region and use
that as function association.
  • Loading branch information
KnisterPeter committed Jun 17, 2019
1 parent c1f2e76 commit 4c4e3a8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
12 changes: 1 addition & 11 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export interface LambdaFunctionAssociation {
/**
* A version of the lambda to associate or a resolvable resource
*/
readonly lambdaFunction: lambda.IVersion | cdk.IResolvable;
readonly lambdaFunction: lambda.IVersion;
}

export enum LambdaEdgeEventType {
Expand Down Expand Up @@ -719,10 +719,6 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
}

private toBehavior(input: BehaviorWithOrigin, protoPolicy?: ViewerProtocolPolicy) {
function isResolvable(resolvableOrVersion: cdk.IResolvable | lambda.IVersion): resolvableOrVersion is cdk.IResolvable {
return Boolean((resolvableOrVersion as cdk.IResolvable).resolve);
}

let toReturn = {
allowedMethods: this.METHOD_LOOKUP_MAP[input.allowedMethods || CloudFrontAllowedMethods.GET_HEAD],
cachedMethods: this.METHOD_LOOKUP_MAP[input.cachedMethods || CloudFrontAllowedCachedMethods.GET_HEAD],
Expand All @@ -745,12 +741,6 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
if (!fna.lambdaFunction) {
return undefined;
}
if (isResolvable(fna.lambdaFunction)) {
return {
eventType: fna.eventType,
lambdaFunctionArn: fna.lambdaFunction,
};
}
return {
eventType: fna.eventType,
lambdaFunctionArn: fna.lambdaFunction.versionArn,
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
},
"dependencies": {
"@aws-cdk/aws-certificatemanager": "^0.34.0",
"@aws-cdk/aws-cloudformation": "^0.34.0",
"@aws-cdk/aws-iam": "^0.34.0",
"@aws-cdk/aws-kms": "^0.34.0",
"@aws-cdk/aws-lambda": "^0.34.0",
Expand All @@ -81,6 +82,7 @@
"homepage": "https://github.com/awslabs/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-certificatemanager": "^0.34.0",
"@aws-cdk/aws-cloudformation": "^0.34.0",
"@aws-cdk/aws-iam": "^0.34.0",
"@aws-cdk/aws-kms": "^0.34.0",
"@aws-cdk/aws-lambda": "^0.34.0",
Expand Down
16 changes: 11 additions & 5 deletions packages/@aws-cdk/aws-cloudfront/test/test.basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, haveResource } from '@aws-cdk/assert';
import * as cloudformation from '@aws-cdk/aws-cloudformation';
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/cdk';
Expand Down Expand Up @@ -327,14 +328,15 @@ export = {
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');

const lambdaFunction = new lambda.Function(stack, 'Lambda', {
const lambdaFunction = new lambda.SingletonFunction(stack, 'Lambda', {
uuid: 'xxxx-xxxx-xxxx-xxxx',
code: lambda.Code.inline('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.Nodejs810
});

const lambdaVersion = new lambda.Version(stack, 'LambdaVersion', {
lambda: lambdaFunction
const customResource = new cloudformation.CustomResource(stack, 'CustomResource', {
provider: cloudformation.CustomResourceProvider.lambda(lambdaFunction)
});

new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
Expand All @@ -348,7 +350,11 @@ export = {
isDefaultBehavior: true,
lambdaFunctionAssociations: [{
eventType: LambdaEdgeEventType.OriginRequest,
lambdaFunction: cdk.Token.asAny(lambdaVersion.versionArn)
lambdaFunction: lambda.Version.fromVersionArn(
stack,
'LambdaEdgeVersion',
customResource.getAtt('Output').toString()
)
}]
}
]
Expand Down Expand Up @@ -394,7 +400,7 @@ export = {
{
"EventType": "origin-request",
"LambdaFunctionARN": {
"Ref": "LambdaVersionFA49E61E"
"Fn::GetAtt": ["CustomResource", "Output"]
}
}
]
Expand Down
34 changes: 33 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/lambda-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Construct, IResource, Resource } from '@aws-cdk/cdk';
import { Construct, Fn, IResource, Resource } from '@aws-cdk/cdk';
import { Function } from './function';
import { IFunction } from './function-base';
import { CfnVersion } from './lambda.generated';

Expand Down Expand Up @@ -82,10 +83,24 @@ export interface VersionAttributes {
*/
export class Version extends Resource implements IVersion {

/**
* @param scope The cdk scope creating this resource
* @param id The cdk id of this resource
* @param versionArn The version ARN to create this version from
*/
public static fromVersionArn(scope: Construct, id: string, versionArn: string): IVersion {
return Version.fromVersionAttributes(scope, id, {
version: extractVersionFromArn(versionArn),
lambda: Function.fromFunctionArn(scope, `${id}Function`, versionArn),
versionArn
});
}

public static fromVersionAttributes(scope: Construct, id: string, attrs: VersionAttributes): IVersion {
class Import extends Resource implements IVersion {
public readonly version = attrs.version;
public readonly lambda = attrs.lambda;
public readonly versionArn = attrs.versionArn;
}
return new Import(scope, id);
}
Expand All @@ -108,3 +123,20 @@ export class Version extends Resource implements IVersion {
this.lambda = props.lambda;
}
}

/**
* Given an opaque (token) ARN, returns a CloudFormation expression that extracts the version
* name from the ARN.
*
* Version ARNs look like this:
*
* arn:aws:lambda:region:account-id:function:function-name:version
*
* ..which means that in order to extract the `version` component from the ARN, we can
* split the ARN using ":" and select the component in index 7.
*
* @returns `FnSelect(7, FnSplit(':', arn))`
*/
function extractVersionFromArn(arn: string) {
return Fn.select(7, Fn.split(':', arn));
}

0 comments on commit 4c4e3a8

Please sign in to comment.