Import exists Lambda@Edge function into a new CloudFront made by AWS CDK(TypeScript)
AWS CDK can create a new CloudFront distribution, but the definition code is a little bit different to other t […]
目次
AWS CDK can create a new CloudFront distribution, but the definition code is a little bit different to other tools like CloudFormation or SAM.
CloudFormation example
By using CloudFormation, we can just refer to the input Parameters to import the exists Lambda@Edge function.
Parameters:
OriginDomain:
Description: website url
Type: String
OriginRequestFunctionARN:
Description: Lambda edge arn ( Origin Request )
Type: String
OriginRequestFunctionVersion:
Description: Lambda edge version ( Origin Request )
Type: String
Resources:
Distribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: !Ref OriginDomain
Id: myOrigin
CustomOriginConfig:
HTTPPort: '80'
HTTPSPort: '443'
OriginProtocolPolicy: http-only
Enabled: 'true'
Comment:
!Join
- ''
- - rabify CDN (
- !Ref OriginDomain
- )
DefaultRootObject: !Ref DefaultRootObject
CacheBehaviors:
- TargetOriginId: myOrigin
LambdaFunctionAssociations:
- EventType: 'origin-request'
LambdaFunctionARN:
!Join
- ':'
- - !Ref OriginRequestFunctionARN
- !Ref OriginRequestFunctionVersion
...
AWS CDK requires to create a new Lambda Function Version instance
By using AWS CDK(TypeScript), we can not use the same style. Because the parameter LambdaFunctionAssociations
request to put a lambda.IVersion
class.
export interface LambdaFunctionAssociation {
/**
* The lambda event type defines at which event the lambda
* is called during the request lifecycle
*/
readonly eventType: LambdaEdgeEventType;
/**
* A version of the lambda to associate
*/
readonly lambdaFunction: lambda.IVersion;
}
It’s not a string. So we have to create a lambda.Version
instance.
import * as cdk from '@aws-cdk/core';
import { Version } from '@aws-cdk/aws-lambda';
import {
CloudFrontWebDistribution,
LambdaFunctionAssociation,
LambdaEdgeEventType
} from '@aws-cdk/aws-cloudfront';
export class CfStackStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// CFN Parameters
const originDomain = new cdk.CfnParameter(this, 'OriginDomain', {
type: 'String',
description: 'Website url'
})
const originRequestFunctionARN = new cdk.CfnParameter(this, 'OriginRequestFunctionARN', {
type: 'string',
description: 'Lambda edge arn ( Origin Request )',
})
const originRequestFunctionVersion = new cdk.CfnParameter(this, 'OriginRequestFunctionVersion', {
type: 'string',
description: 'Lambda version',
})
// Function
const edgeOriginRequest = Version.fromVersionArn(this, 'OriginRequestVersion', [
originRequestFunctionARN.valueAsString,
originRequestFunctionVersion.valueAsString
].join(':'))
// Associations
const lambdaFunctionAssociations: LambdaFunctionAssociation[] = [{
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: edgeOriginRequest,
}]
// CloudFront
new CloudFrontWebDistribution(this, 'CloudFront', {
originConfigs: [{
customOriginSource: {
domainName: originDomain.valueAsString,
originProtocolPolicy: OriginProtocolPolicy.MATCH_VIEWER,
originReadTimeout: Duration.seconds(30)
},
behaviors: [{
compress: true,
minTtl: Duration.seconds(43200),
pathPattern: '*.jpg',
forwardedValues: {
headers: ['Authorization', 'Host'],
queryString: true,
queryStringCacheKeys: ['d', 'v'],
cookies: {
forward: 'none'
}
},
allowedMethods: CloudFrontAllowedMethods.GET_HEAD,
cachedMethods: CloudFrontAllowedCachedMethods.GET_HEAD,
lambdaFunctionAssociations
...
Then, you can get the same CloudFormation YAML output by using cdk synth
.
LambdaFunctionAssociations:
- EventType: origin-request
LambdaFunctionARN:
Fn::Join:
- ""
- - Ref: OriginRequestFunctionARN
- ":"
- Ref: OriginRequestFunctionVersion
Conclusion
Use the Version.fromVersionArn
to import your exists Lambda@edge function into your AWS CDK stack.