|
| 1 | +// tslint:disable no-console |
| 2 | +import { StepFunctions } from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies |
| 3 | +import * as https from 'https'; |
| 4 | +import * as url from 'url'; |
| 5 | + |
| 6 | +const CREATE_FAILED_PHYSICAL_ID_MARKER = 'AWSCDK::StateMachineProvider::CREATE_FAILED'; |
| 7 | +const MISSING_PHYSICAL_ID_MARKER = 'AWSCDK::StateMachineProvider::MISSING_PHYSICAL_ID'; |
| 8 | + |
| 9 | +interface ExecutionResult { |
| 10 | + ExecutionArn: string; |
| 11 | + Input: AWSLambda.CloudFormationCustomResourceEvent & { PhysicalResourceId?: string }; |
| 12 | + Name: string; |
| 13 | + Output?: AWSLambda.CloudFormationCustomResourceResponse; |
| 14 | + StartDate: number; |
| 15 | + StateMachineArn: string; |
| 16 | + Status: 'RUNNING' | 'SUCCEEDED' | 'FAILED' | 'TIMED_OUT' | 'ABORTED'; |
| 17 | + StopDate: number; |
| 18 | +} |
| 19 | + |
| 20 | +interface FailedExecutionEvent { |
| 21 | + Error: string; |
| 22 | + Cause: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface CloudFormationResponse { |
| 26 | + StackId: string; |
| 27 | + RequestId: string; |
| 28 | + PhysicalResourceId?: string; |
| 29 | + LogicalResourceId: string; |
| 30 | + ResponseURL: string; |
| 31 | + Data?: any; |
| 32 | + NoEcho?: boolean; |
| 33 | + Reason?: string; |
| 34 | +} |
| 35 | + |
| 36 | +export async function cfnResponseSuccess(event: ExecutionResult, _context: AWSLambda.Context) { |
| 37 | + console.log('Event: %j', event); |
| 38 | + await respond('SUCCESS', { |
| 39 | + ...event.Input, |
| 40 | + PhysicalResourceId: event.Output?.PhysicalResourceId ?? event.Input.PhysicalResourceId ?? event.Input.RequestId, |
| 41 | + Data: event.Output?.Data ?? {}, |
| 42 | + NoEcho: event.Output?.NoEcho, |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +export async function cfnResponseFailed(event: FailedExecutionEvent, _context: AWSLambda.Context) { |
| 47 | + console.log('Event: %j', event); |
| 48 | + |
| 49 | + const parsedCause = JSON.parse(event.Cause); |
| 50 | + const executionResult: ExecutionResult = { |
| 51 | + ...parsedCause, |
| 52 | + Input: JSON.parse(parsedCause.Input), |
| 53 | + }; |
| 54 | + console.log('Execution result: %j', executionResult); |
| 55 | + |
| 56 | + let physicalResourceId = executionResult.Output?.PhysicalResourceId; |
| 57 | + if (!physicalResourceId) { |
| 58 | + // special case: if CREATE fails, which usually implies, we usually don't |
| 59 | + // have a physical resource id. in this case, the subsequent DELETE |
| 60 | + // operation does not have any meaning, and will likely fail as well. to |
| 61 | + // address this, we use a marker so the provider framework can simply |
| 62 | + // ignore the subsequent DELETE. |
| 63 | + if (executionResult.Input.RequestType === 'Create') { |
| 64 | + console.log('CREATE failed, responding with a marker physical resource id so that the subsequent DELETE will be ignored'); |
| 65 | + physicalResourceId = CREATE_FAILED_PHYSICAL_ID_MARKER; |
| 66 | + } else { |
| 67 | + console.log(`ERROR: Malformed event. "PhysicalResourceId" is required: ${JSON.stringify(event)}`); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + await respond('FAILED', { |
| 72 | + ...executionResult.Input, |
| 73 | + Reason: `${event.Error}: ${event.Cause}`, |
| 74 | + PhysicalResourceId: physicalResourceId, |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +export async function startExecution(event: AWSLambda.CloudFormationCustomResourceEvent, _context: AWSLambda.Context) { |
| 79 | + try { |
| 80 | + console.log('Event: %j', event); |
| 81 | + |
| 82 | + if (!process.env.STATE_MACHINE_ARN) { |
| 83 | + throw new Error('Missing STATE_MACHINE_ARN.'); |
| 84 | + } |
| 85 | + |
| 86 | + // ignore DELETE event when the physical resource ID is the marker that |
| 87 | + // indicates that this DELETE is a subsequent DELETE to a failed CREATE |
| 88 | + // operation. |
| 89 | + if (event.RequestType === 'Delete' && event.PhysicalResourceId === CREATE_FAILED_PHYSICAL_ID_MARKER) { |
| 90 | + console.log('ignoring DELETE event caused by a failed CREATE event'); |
| 91 | + await respond('SUCCESS', event); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const stepFunctions = new StepFunctions(); |
| 96 | + await stepFunctions.startExecution({ |
| 97 | + stateMachineArn: process.env.STATE_MACHINE_ARN, |
| 98 | + input: JSON.stringify(event), |
| 99 | + }).promise(); |
| 100 | + } catch (err) { |
| 101 | + console.log(err); |
| 102 | + await respond('FAILED', { |
| 103 | + ...event, |
| 104 | + Reason: err.message, |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function respond(status: 'SUCCESS' | 'FAILED', event: CloudFormationResponse) { |
| 110 | + const json: AWSLambda.CloudFormationCustomResourceResponse = { |
| 111 | + Status: status, |
| 112 | + Reason: event.Reason ?? status, |
| 113 | + PhysicalResourceId: event.PhysicalResourceId || MISSING_PHYSICAL_ID_MARKER, |
| 114 | + StackId: event.StackId, |
| 115 | + RequestId: event.RequestId, |
| 116 | + LogicalResourceId: event.LogicalResourceId, |
| 117 | + NoEcho: event.NoEcho ?? false, |
| 118 | + Data: event.Data, |
| 119 | + }; |
| 120 | + |
| 121 | + console.log('Responding: %j', json); |
| 122 | + |
| 123 | + const responseBody = JSON.stringify(json); |
| 124 | + |
| 125 | + const parsedUrl = url.parse(event.ResponseURL); |
| 126 | + const requestOptions = { |
| 127 | + hostname: parsedUrl.hostname, |
| 128 | + path: parsedUrl.path, |
| 129 | + method: 'PUT', |
| 130 | + headers: { 'content-type': '', 'content-length': responseBody.length }, |
| 131 | + }; |
| 132 | + |
| 133 | + return new Promise((resolve, reject) => { |
| 134 | + try { |
| 135 | + const request = https.request(requestOptions, resolve); |
| 136 | + request.on('error', reject); |
| 137 | + request.write(responseBody); |
| 138 | + request.end(); |
| 139 | + } catch (e) { |
| 140 | + reject(e); |
| 141 | + } |
| 142 | + }); |
| 143 | +} |
0 commit comments