UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 3.92 kB
"use strict";var f=Object.create,i=Object.defineProperty,I=Object.getOwnPropertyDescriptor,g=Object.getOwnPropertyNames,S=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty,w=(o,e)=>{for(var t in e)i(o,t,{get:e[t],enumerable:!0})},l=(o,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of g(e))!A.call(o,n)&&n!==t&&i(o,n,{get:()=>e[n],enumerable:!(s=I(e,n))||s.enumerable});return o},m=(o,e,t)=>(t=o!=null?f(S(o)):{},l(e||!o||!o.__esModule?i(t,"default",{value:o,enumerable:!0}):t,o)),P=o=>l(i({},"__esModule",{value:!0}),o),O={};w(O,{autoDeleteHandler:()=>E,handler:()=>_}),module.exports=P(O);var c=require("@aws-sdk/client-lambda"),u=require("@aws-sdk/client-synthetics"),y=m(require("https")),R=m(require("url")),a={sendHttpRequest:b,log:F,includeStackTraces:!0,userHandlerIndex:"./index"},p="AWSCDK::CustomResourceProviderFramework::CREATE_FAILED",L="AWSCDK::CustomResourceProviderFramework::MISSING_PHYSICAL_ID";function C(o){return async(e,t)=>{let s={...e,ResponseURL:"..."};if(a.log(JSON.stringify(s,void 0,2)),e.RequestType==="Delete"&&e.PhysicalResourceId===p){a.log("ignoring DELETE event caused by a failed CREATE event"),await d("SUCCESS",e);return}try{let n=await o(s,t),r=D(e,n);await d("SUCCESS",r)}catch(n){let r={...e,Reason:a.includeStackTraces?n.stack:n.message};r.PhysicalResourceId||(e.RequestType==="Create"?(a.log("CREATE failed, responding with a marker physical resource id so that the subsequent DELETE will be ignored"),r.PhysicalResourceId=p):a.log(`ERROR: Malformed event. "PhysicalResourceId" is required: ${JSON.stringify(e)}`)),await d("FAILED",r)}}}function D(o,e={}){let t=e.PhysicalResourceId??o.PhysicalResourceId??o.RequestId;if(o.RequestType==="Delete"&&t!==o.PhysicalResourceId)throw new Error(`DELETE: cannot change the physical resource ID from "${o.PhysicalResourceId}" to "${e.PhysicalResourceId}" during deletion`);return{...o,...e,PhysicalResourceId:t}}async function d(o,e){let t={Status:o,Reason:e.Reason??o,StackId:e.StackId,RequestId:e.RequestId,PhysicalResourceId:e.PhysicalResourceId||L,LogicalResourceId:e.LogicalResourceId,NoEcho:e.NoEcho,Data:e.Data};a.log("submit response to cloudformation",t);let s=JSON.stringify(t),n=R.parse(e.ResponseURL),r={hostname:n.hostname,path:n.path,method:"PUT",headers:{"content-type":"","content-length":Buffer.byteLength(s,"utf8")}};await T({attempts:5,sleep:1e3},a.sendHttpRequest)(r,s)}async function b(o,e){return new Promise((t,s)=>{try{let n=y.request(o,r=>t());n.on("error",s),n.write(e),n.end()}catch(n){s(n)}})}function F(o,...e){console.log(o,...e)}function T(o,e){return async(...t)=>{let s=o.attempts,n=o.sleep;for(;;)try{return await e(...t)}catch(r){if(s--<=0)throw r;await x(Math.floor(Math.random()*n)),n*=2}}}async function x(o){return new Promise(e=>setTimeout(e,o))}var h="aws-cdk:auto-delete-underlying-resources",N=new c.LambdaClient({}),H=new u.SyntheticsClient({}),_=C(E);async function E(o){switch(o.RequestType){case"Create":return{PhyscialResourceId:o.ResourceProperties?.CanaryName};case"Update":return{PhysicalResourceId:(await k(o)).PhysicalResourceId};case"Delete":return q(o.ResourceProperties?.CanaryName)}}async function k(o){return{PhysicalResourceId:o.ResourceProperties?.CanaryName}}async function q(o){if(console.log(`Deleting lambda function associated with ${o}`),!o)throw new Error("No CanaryName was provided.");try{let e=await H.send(new u.GetCanaryCommand({Name:o}));if(e.Canary===void 0||e.Canary.Id===void 0||e.Canary.EngineArn===void 0)return;if(!U(e.Canary.Tags)){console.log(`Canary does not have '${h}' tag, skipping deletion.`);return}let t=e.Canary.EngineArn.split(":");t.at(-1)?.includes(e.Canary.Id)||t.pop();let s=t.join(":");console.log(`Deleting lambda ${s}`),await N.send(new c.DeleteFunctionCommand({FunctionName:s}))}catch(e){if(e.name!=="ResourceNotFoundException")throw e}}function U(o){return o?Object.keys(o).some(e=>e===h&&o[e]==="true"):!1}