UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 2.27 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CacheBehavior=void 0;const iam=require("../../../aws-iam"),cache_policy_1=require("../cache-policy"),distribution_1=require("../distribution");class CacheBehavior{constructor(originId,props){this.props=props,this.originId=originId,this.validateEdgeLambdas(props.edgeLambdas),this.grantEdgeLambdaFunctionExecutionRole(props.edgeLambdas)}_renderBehavior(){return{pathPattern:this.props.pathPattern,targetOriginId:this.originId,allowedMethods:this.props.allowedMethods?.methods,cachedMethods:this.props.cachedMethods?.methods,cachePolicyId:(this.props.cachePolicy??cache_policy_1.CachePolicy.CACHING_OPTIMIZED).cachePolicyId,compress:this.props.compress??!0,originRequestPolicyId:this.props.originRequestPolicy?.originRequestPolicyId,responseHeadersPolicyId:this.props.responseHeadersPolicy?.responseHeadersPolicyId,smoothStreaming:this.props.smoothStreaming,viewerProtocolPolicy:this.props.viewerProtocolPolicy??distribution_1.ViewerProtocolPolicy.ALLOW_ALL,functionAssociations:this.props.functionAssociations?.map(association=>({functionArn:association.function.functionArn,eventType:association.eventType.toString()})),lambdaFunctionAssociations:this.props.edgeLambdas?.map(edgeLambda=>({lambdaFunctionArn:edgeLambda.functionVersion.edgeArn,eventType:edgeLambda.eventType.toString(),includeBody:edgeLambda.includeBody})),trustedKeyGroups:this.props.trustedKeyGroups?.map(keyGroup=>keyGroup.keyGroupId)}}validateEdgeLambdas(edgeLambdas){const includeBodyEventTypes=[distribution_1.LambdaEdgeEventType.ORIGIN_REQUEST,distribution_1.LambdaEdgeEventType.VIEWER_REQUEST];if(edgeLambdas&&edgeLambdas.some(lambda=>lambda.includeBody&&!includeBodyEventTypes.includes(lambda.eventType)))throw new Error("'includeBody' can only be true for ORIGIN_REQUEST or VIEWER_REQUEST event types.")}grantEdgeLambdaFunctionExecutionRole(edgeLambdas){!edgeLambdas||edgeLambdas.length===0||edgeLambdas.forEach(edgeLambda=>{const role=edgeLambda.functionVersion.role;role&&iam.Role.isRole(role)&&role.assumeRolePolicy&&role.assumeRolePolicy.addStatements(new iam.PolicyStatement({actions:["sts:AssumeRole"],principals:[new iam.ServicePrincipal("edgelambda.amazonaws.com")]}))})}}exports.CacheBehavior=CacheBehavior;