aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 12.8 kB
JavaScript
"use strict";var __decorate=exports&&exports.__decorate||function(decorators,target,key,desc){var c=arguments.length,r=c<3?target:desc===null?desc=Object.getOwnPropertyDescriptor(target,key):desc,d;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(decorators,target,key,desc);else for(var i=decorators.length-1;i>=0;i--)(d=decorators[i])&&(r=(c<3?d(r):c>3?d(target,key,r):d(target,key))||r);return c>3&&r&&Object.defineProperty(target,key,r),r},_a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.EdgeFunction=void 0;var jsiiDeprecationWarnings=()=>{var tmp=require("../../../.warnings.jsii.js");return jsiiDeprecationWarnings=()=>tmp,tmp};const JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti");var iam=()=>{var tmp=require("../../../aws-iam");return iam=()=>tmp,tmp},lambda=()=>{var tmp=require("../../../aws-lambda");return lambda=()=>tmp,tmp},ssm=()=>{var tmp=require("../../../aws-ssm");return ssm=()=>tmp,tmp},core_1=()=>{var tmp=require("../../../core");return core_1=()=>tmp,tmp},metadata_resource_1=()=>{var tmp=require("../../../core/lib/metadata-resource");return metadata_resource_1=()=>tmp,tmp},cross_region_string_param_reader_provider_generated_1=()=>{var tmp=require("../../../custom-resource-handlers/dist/aws-cloudfront/cross-region-string-param-reader-provider.generated");return cross_region_string_param_reader_provider_generated_1=()=>tmp,tmp};class EdgeFunction extends core_1().Resource{constructor(scope,id,props){super(scope,id),this.isBoundToVpc=!1;try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudfront_experimental_EdgeFunctionProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,EdgeFunction),error}(0,metadata_resource_1().addConstructMetadata)(this,props);const regionIsUsEast1=!core_1().Token.isUnresolved(this.env.region)&&this.env.region==="us-east-1",{edgeFunction,edgeArn}=regionIsUsEast1?this.createInRegionFunction(props):this.createCrossRegionFunction(id,props);this.edgeArn=edgeArn,this.functionArn=edgeArn,this._edgeFunction=edgeFunction,this.functionName=this._edgeFunction.functionName,this.grantPrincipal=this._edgeFunction.role,this.permissionsNode=this._edgeFunction.permissionsNode,this.version=lambda().extractQualifierFromArn(this.functionArn),this.architecture=this._edgeFunction.architecture,this.resourceArnsForGrantInvoke=this._edgeFunction.resourceArnsForGrantInvoke,this.node.defaultChild=this._edgeFunction}get lambda(){return this._edgeFunction}get currentVersion(){return this}addAlias(aliasName,options={}){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_AliasOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addAlias),error}return new(lambda()).Alias(this._edgeFunction,`Alias${aliasName}`,{aliasName,version:this._edgeFunction.currentVersion,...options})}get connections(){throw new(core_1()).ValidationError("Lambda@Edge does not support connections",this)}get latestVersion(){throw new(core_1()).ValidationError("$LATEST function version cannot be used for Lambda@Edge",this)}addEventSourceMapping(id,options){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_EventSourceMappingOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addEventSourceMapping),error}return this.lambda.addEventSourceMapping(id,options)}addPermission(id,permission){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_Permission(permission)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addPermission),error}return this.lambda.addPermission(id,permission)}addToRolePolicy(statement){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_PolicyStatement(statement)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addToRolePolicy),error}return this.lambda.addToRolePolicy(statement)}grantInvoke(identity){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_IGrantable(identity)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantInvoke),error}return this.lambda.grantInvoke(identity)}grantInvokeLatestVersion(identity){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_IGrantable(identity)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantInvokeLatestVersion),error}return this.lambda.grantInvokeLatestVersion(identity)}grantInvokeVersion(identity,version){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_IGrantable(identity),jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_IVersion(version)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantInvokeVersion),error}return this.lambda.grantInvokeVersion(identity,version)}grantInvokeUrl(identity){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_IGrantable(identity)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantInvokeUrl),error}return this.lambda.grantInvokeUrl(identity)}grantInvokeCompositePrincipal(compositePrincipal){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_iam_CompositePrincipal(compositePrincipal)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantInvokeCompositePrincipal),error}return this.lambda.grantInvokeCompositePrincipal(compositePrincipal)}metric(metricName,props){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_MetricOptions(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.metric),error}return this.lambda.metric(metricName,{...props,region:EdgeFunction.EDGE_REGION})}metricDuration(props){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_MetricOptions(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.metricDuration),error}return this.lambda.metricDuration({...props,region:EdgeFunction.EDGE_REGION})}metricErrors(props){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_MetricOptions(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.metricErrors),error}return this.lambda.metricErrors({...props,region:EdgeFunction.EDGE_REGION})}metricInvocations(props){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_MetricOptions(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.metricInvocations),error}return this.lambda.metricInvocations({...props,region:EdgeFunction.EDGE_REGION})}metricThrottles(props){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_MetricOptions(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.metricThrottles),error}return this.lambda.metricThrottles({...props,region:EdgeFunction.EDGE_REGION})}addEventSource(source){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_IEventSource(source)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addEventSource),error}return this.lambda.addEventSource(source)}configureAsyncInvoke(options){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_EventInvokeConfigOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.configureAsyncInvoke),error}return this.lambda.configureAsyncInvoke(options)}addFunctionUrl(options){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_lambda_FunctionUrlOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addFunctionUrl),error}return this.lambda.addFunctionUrl(options)}createInRegionFunction(props){const edgeFunction=new(lambda()).Function(this,"Fn",props);return addEdgeLambdaToRoleTrustStatement(edgeFunction.role),{edgeFunction,edgeArn:edgeFunction.currentVersion.edgeArn}}createCrossRegionFunction(id,props){const parameterNamePrefix="cdk/EdgeFunctionArn";if(core_1().Token.isUnresolved(this.env.region))throw new(core_1()).ValidationError("stacks which use EdgeFunctions must have an explicitly set region",this);const sanitizedPath=this.node.path.replace(/[^\/\w.-]/g,"_"),parameterName=`/${parameterNamePrefix}/${this.env.region}/${sanitizedPath}`,functionStack=this.edgeStack(props.stackId),edgeFunction=new(lambda()).Function(functionStack,id,props);addEdgeLambdaToRoleTrustStatement(edgeFunction.role);const version=edgeFunction.currentVersion;new(ssm()).StringParameter(edgeFunction,"Parameter",{parameterName,stringValue:version.edgeArn});const edgeArn=this.createCrossRegionArnReader(parameterNamePrefix,parameterName,version);return{edgeFunction,edgeArn}}createCrossRegionArnReader(parameterNamePrefix,parameterName,version){const parameterArnPrefix=this.stack.formatArn({service:"ssm",region:EdgeFunction.EDGE_REGION,resource:"parameter",resourceName:parameterNamePrefix+"/*"}),resourceType="Custom::CrossRegionStringParameterReader",serviceToken=cross_region_string_param_reader_provider_generated_1().CrossRegionStringParamReaderProvider.getOrCreate(this,resourceType,{policyStatements:[{Effect:"Allow",Resource:parameterArnPrefix,Action:["ssm:GetParameter"]}]});return new(core_1()).CustomResource(this,"ArnReader",{resourceType,serviceToken,properties:{Region:EdgeFunction.EDGE_REGION,ParameterName:parameterName,RefreshToken:core_1().Lazy.uncachedString({produce:()=>{const cfn=version.node.defaultChild;return this.stack.resolve(cfn.logicalId)}})}}).getAttString("FunctionArn")}edgeStack(stackId){const stage=core_1().Stage.of(this);if(!stage)throw new(core_1()).ValidationError("stacks which use EdgeFunctions must be part of a CDK app or stage",this);const edgeStackId=stackId??`edge-lambda-stack-${this.stack.node.addr}`;let edgeStack=stage.node.tryFindChild(edgeStackId);return edgeStack||(edgeStack=new(core_1()).Stack(stage,edgeStackId,{env:{region:EdgeFunction.EDGE_REGION,account:core_1().Stack.of(this).account}})),this.stack.addDependency(edgeStack),edgeStack}}exports.EdgeFunction=EdgeFunction,_a=JSII_RTTI_SYMBOL_1,EdgeFunction[_a]={fqn:"aws-cdk-lib.aws_cloudfront.experimental.EdgeFunction",version:"2.185.0"},EdgeFunction.EDGE_REGION="us-east-1",__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addAlias",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addEventSourceMapping",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addPermission",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addToRolePolicy",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"grantInvoke",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"grantInvokeLatestVersion",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"grantInvokeVersion",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"grantInvokeUrl",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"grantInvokeCompositePrincipal",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"metric",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"metricDuration",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"metricErrors",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"metricInvocations",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"metricThrottles",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addEventSource",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"configureAsyncInvoke",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],EdgeFunction.prototype,"addFunctionUrl",null);function addEdgeLambdaToRoleTrustStatement(role){if(iam().Role.isRole(role)&&role.assumeRolePolicy){const statement=new(iam()).PolicyStatement,edgeLambdaServicePrincipal=new(iam()).ServicePrincipal("edgelambda.amazonaws.com");statement.addPrincipals(edgeLambdaServicePrincipal),statement.addActions(edgeLambdaServicePrincipal.assumeRoleAction),role.assumeRolePolicy.addStatements(statement)}}