Created
November 6, 2020 11:41
-
-
Save nicolaracco/9297dc2bca0a5bbb48965faa0966e03f to your computer and use it in GitHub Desktop.
Use lambda@edge in cdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cdk from '@aws-cdk/core'; | |
import * as s3 from '@aws-cdk/aws-s3'; | |
import * as lambda from '@aws-cdk/aws-lambda'; | |
export interface AppStackProps extends cdk.StackProps { | |
lambdaParamName: string; | |
} | |
export class AppStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, props: AppStackProps) { | |
super(scope, id, props); | |
const lambdaVersion = this.findEdgeVersionFromParamname('LambdaEdge', props.lambdaEdgeParamName); | |
// do stuff with lambda | |
} | |
private findEdgeVersionFromParamname(id: string, parameterName: string): lambda.IVersion { | |
const provider = new EdgeFunctionProvider(this, `${id}Provider`, { parameterName }); | |
return provider.functionVersion; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cdk from '@aws-cdk/core'; | |
import * as lambda from '@aws-cdk/aws-lambda'; | |
import * as customResources from '@aws-cdk/custom-resources'; | |
export interface EdgeFunctionProviderProps { | |
readonly parameterName: string; | |
} | |
export class EdgeFunctionProvider extends cdk.Construct { | |
public readonly functionVersion: lambda.IVersion; | |
constructor(scope: cdk.Construct, id: string, props: EdgeFunctionProviderProps) { | |
super(scope, id); | |
const cr = new customResources.AwsCustomResource(this, 'Resource', { | |
onUpdate: { | |
service: 'SSM', | |
action: 'getParameter', | |
parameters: { | |
Name: props.parameterName, | |
}, | |
region: 'us-east-1', | |
physicalResourceId: customResources.PhysicalResourceId.of(Date.now().toString()), // Update physical id to always fetch the latest version | |
}, | |
policy: customResources.AwsCustomResourcePolicy.fromSdkCalls({ | |
resources: [this.ssmParameterArn(props.parameterName)], | |
}), | |
}); | |
this.functionVersion = lambda.Version.fromVersionArn(this, 'Function', cr.getResponseField('Parameter.Value')); | |
} | |
private ssmParameterArn(parameterName: string): string { | |
// remove leading slash from param name | |
const paramNameWithoutSlash = parameterName.match(/^\/?([^\/].+)/)![1] | |
return cdk.Stack.of(this).formatArn({ | |
service: 'ssm', | |
region: 'us-east-1', | |
resource: 'parameter', | |
resourceName: paramNameWithoutSlash, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cdk from '@aws-cdk/core'; | |
import * as lambda from '@aws-cdk/aws-lambda'; | |
import * as ssm from '@aws-cdk/aws-ssm'; | |
import * as iam from '@aws-cdk/aws-iam'; | |
interface CreateEdgeFunctionProps { | |
parameterName: string; | |
role: iam.IRole; | |
} | |
export class EdgeStack extends cdk.Stack { | |
readonly lambdaParamName: string; | |
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const role = this.createEdgeRole('EdgeExecutionRole'); | |
this.lambdaParamName = `/${id}/html-router/version-arn` | |
this.createEdgeFunction('HtmlRouter', 'functions/router-lambda-edge', { parameterName: this.lambdaParamName, role }); | |
} | |
private createEdgeRole(id: string): iam.Role { | |
return new iam.Role(this, id, { | |
assumedBy: new iam.CompositePrincipal( | |
new iam.ServicePrincipal('edgelambda.amazonaws.com'), | |
new iam.ServicePrincipal('lambda.amazonaws.com'), | |
), | |
managedPolicies: [ | |
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'), | |
], | |
}) | |
} | |
private createEdgeFunction(id: string, path: string, { parameterName, role }: CreateEdgeFunctionProps): void { | |
const lambdaFn = new lambda.Function(this, id, { | |
code: lambda.Code.fromAsset(path), | |
handler: 'index.handler', | |
runtime: lambda.Runtime.NODEJS_12_X, | |
role, | |
}); | |
new ssm.StringParameter(this, `${id}Param`, { | |
parameterName, | |
stringValue: lambdaFn.currentVersion.edgeArn, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'source-map-support/register'; | |
import * as cdk from '@aws-cdk/core'; | |
import { VueMonitorEdgeStack } from '../edge-stack'; | |
const app = new cdk.App(); | |
const edge = new EdgeStack(app, 'edge-functions', { | |
env: { | |
region: 'us-east-1', | |
}, | |
}); | |
const app = new AppStack(app, 'app', { | |
lambdaEdgeParamName: edge.lambdaParamName, | |
}); | |
app.addDependency(edge); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment