aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 7.4 kB
JavaScript
;var _a,_b,_c,_d;Object.defineProperty(exports,"__esModule",{value:!0}),exports.AwsCustomResource=exports.AwsCustomResourcePolicy=exports.PhysicalResourceId=exports.PhysicalResourceIdReference=void 0;const jsiiDeprecationWarnings=require("../../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),fs=require("fs"),path=require("path"),iam=require("../../../aws-iam"),lambda=require("../../../aws-lambda"),cdk=require("../../../core"),core_1=require("../../../core"),cxapi=require("../../../cx-api"),constructs_1=require("constructs"),runtime_1=require("./runtime");class PhysicalResourceIdReference{constructor(){this.creationStack=cdk.captureStackTrace()}toJSON(){return runtime_1.PHYSICAL_RESOURCE_ID_REFERENCE}resolve(_){try{jsiiDeprecationWarnings.aws_cdk_lib_IResolveContext(_)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.resolve),error}return runtime_1.PHYSICAL_RESOURCE_ID_REFERENCE}toString(){return runtime_1.PHYSICAL_RESOURCE_ID_REFERENCE}}exports.PhysicalResourceIdReference=PhysicalResourceIdReference,_a=JSII_RTTI_SYMBOL_1,PhysicalResourceIdReference[_a]={fqn:"aws-cdk-lib.custom_resources.PhysicalResourceIdReference",version:"2.70.0"};class PhysicalResourceId{constructor(responsePath,id){this.responsePath=responsePath,this.id=id}static fromResponse(responsePath){return new PhysicalResourceId(responsePath,void 0)}static of(id){return new PhysicalResourceId(void 0,id)}}exports.PhysicalResourceId=PhysicalResourceId,_b=JSII_RTTI_SYMBOL_1,PhysicalResourceId[_b]={fqn:"aws-cdk-lib.custom_resources.PhysicalResourceId",version:"2.70.0"};class AwsCustomResourcePolicy{constructor(statements,resources){this.statements=statements,this.resources=resources}static fromStatements(statements){return new AwsCustomResourcePolicy(statements,void 0)}static fromSdkCalls(options){try{jsiiDeprecationWarnings.aws_cdk_lib_custom_resources_SdkCallsPolicyOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.fromSdkCalls),error}return new AwsCustomResourcePolicy([],options.resources)}}exports.AwsCustomResourcePolicy=AwsCustomResourcePolicy,_c=JSII_RTTI_SYMBOL_1,AwsCustomResourcePolicy[_c]={fqn:"aws-cdk-lib.custom_resources.AwsCustomResourcePolicy",version:"2.70.0"},AwsCustomResourcePolicy.ANY_RESOURCE=["*"];class AwsCustomResource extends constructs_1.Construct{constructor(scope,id,props){super(scope,id);try{jsiiDeprecationWarnings.aws_cdk_lib_custom_resources_AwsCustomResourceProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,AwsCustomResource),error}if(!props.onCreate&&!props.onUpdate&&!props.onDelete)throw new Error("At least `onCreate`, `onUpdate` or `onDelete` must be specified.");if(!props.role&&!props.policy)throw new Error("At least one of `policy` or `role` (or both) must be specified.");if(props.onCreate&&!props.onCreate.physicalResourceId)throw new Error("'physicalResourceId' must be specified for 'onCreate' call.");if(!props.onCreate&&props.onUpdate&&!props.onUpdate.physicalResourceId)throw new Error("'physicalResourceId' must be specified for 'onUpdate' call when 'onCreate' is omitted.");for(const call of[props.onCreate,props.onUpdate,props.onDelete])call?.physicalResourceId?.responsePath&&AwsCustomResource.breakIgnoreErrorsCircuit([call],"PhysicalResourceId.fromResponse");if(includesPhysicalResourceIdRef(props.onCreate?.parameters))throw new Error("`PhysicalResourceIdReference` must not be specified in `onCreate` parameters.");this.props=props;const provider=new lambda.SingletonFunction(this,"Provider",{code:lambda.Code.fromAsset(path.join(__dirname,"runtime"),{exclude:["*.ts"]}),runtime:lambda.Runtime.NODEJS_14_X,handler:"index.handler",uuid:AwsCustomResource.PROVIDER_FUNCTION_UUID,lambdaPurpose:"AWS",timeout:props.timeout||cdk.Duration.minutes(2),role:props.role,logRetention:props.logRetention,functionName:props.functionName,vpc:props.vpc,vpcSubnets:props.vpcSubnets});this.grantPrincipal=provider.grantPrincipal;const installLatestAwsSdk=props.installLatestAwsSdk??this.node.tryGetContext(cxapi.AWS_CUSTOM_RESOURCE_LATEST_SDK_DEFAULT)??!0;installLatestAwsSdk&&props.installLatestAwsSdk===void 0&&core_1.Annotations.of(this).addWarning(["installLatestAwsSdk was not specified, and defaults to true. You probably do not want this.",`Set the global context flag '${cxapi.AWS_CUSTOM_RESOURCE_LATEST_SDK_DEFAULT}' to false to switch this behavior off project-wide,`,"or set the property explicitly to true if you know you need to call APIs that are not in Lambda's built-in SDK version."].join(" "));const create=props.onCreate||props.onUpdate;if(this.customResource=new cdk.CustomResource(this,"Resource",{resourceType:props.resourceType||"Custom::AWS",serviceToken:provider.functionArn,pascalCaseProperties:!0,properties:{create:create&&this.encodeJson(create),update:props.onUpdate&&this.encodeJson(props.onUpdate),delete:props.onDelete&&this.encodeJson(props.onDelete),installLatestAwsSdk}}),props.policy){const statements=[];if(props.policy.statements.length!==0)for(const statement of props.policy.statements)statements.push(statement);else for(const call of[props.onCreate,props.onUpdate,props.onDelete])if(call&&call.assumedRoleArn==null){const statement=new iam.PolicyStatement({actions:[awsSdkToIamAction(call.service,call.action)],resources:props.policy.resources});statements.push(statement)}else if(call&&call.assumedRoleArn!=null){const statement=new iam.PolicyStatement({actions:["sts:AssumeRole"],resources:[call.assumedRoleArn]});statements.push(statement)}const policy=new iam.Policy(this,"CustomResourcePolicy",{statements});provider.role!==void 0&&policy.attachToRole(provider.role),this.customResource.node.addDependency(policy)}}static breakIgnoreErrorsCircuit(sdkCalls,caller){for(const call of sdkCalls)if(call?.ignoreErrorCodesMatching)throw new Error(`\`${caller}\` cannot be called along with \`ignoreErrorCodesMatching\`.`)}getResponseFieldReference(dataPath){return AwsCustomResource.breakIgnoreErrorsCircuit([this.props.onCreate,this.props.onUpdate],"getData"),this.customResource.getAtt(dataPath)}getResponseField(dataPath){return AwsCustomResource.breakIgnoreErrorsCircuit([this.props.onCreate,this.props.onUpdate],"getDataString"),this.customResource.getAttString(dataPath)}encodeJson(obj){return cdk.Lazy.uncachedString({produce:()=>cdk.Stack.of(this).toJsonString(obj)})}}exports.AwsCustomResource=AwsCustomResource,_d=JSII_RTTI_SYMBOL_1,AwsCustomResource[_d]={fqn:"aws-cdk-lib.custom_resources.AwsCustomResource",version:"2.70.0"},AwsCustomResource.PROVIDER_FUNCTION_UUID="679f53fa-c002-430c-b0da-5b7982bd2287";let getAwsSdkMetadata=(()=>{let _awsSdkMetadata;return function(){return _awsSdkMetadata||(_awsSdkMetadata=JSON.parse(fs.readFileSync(path.join(__dirname,"sdk-api-metadata.json"),"utf-8")))}})();function includesPhysicalResourceIdRef(obj){if(obj===void 0)return!1;let foundRef=!1;return JSON.stringify(obj,(_,v)=>(v===runtime_1.PHYSICAL_RESOURCE_ID_REFERENCE&&(foundRef=!0),v)),foundRef}function awsSdkToIamAction(service,action){const srv=service.toLowerCase(),awsSdkMetadata=getAwsSdkMetadata(),iamService=awsSdkMetadata[srv]&&awsSdkMetadata[srv].prefix||srv,iamAction=action.charAt(0).toUpperCase()+action.slice(1);return`${iamService}:${iamAction}`}