-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
2,843 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { CloudFormationCustomResourceEvent, Context } from 'aws-lambda'; | ||
import * as https from 'node:https'; | ||
import * as url from 'node:url'; | ||
|
||
const physicalResourceId = 'CfnRollbackExercise'; | ||
|
||
export async function handler( | ||
event: CloudFormationCustomResourceEvent, | ||
context: Context, | ||
) { | ||
console.log(event); | ||
if (event.RequestType === 'Create') { | ||
await sendResponse(event, context, 'SUCCESS', {}, physicalResourceId); | ||
return; | ||
} else if (event.RequestType === 'Update' || event.RequestType === 'Delete') { | ||
const causeIssue = context.memoryLimitInMB !== '129'; | ||
if (causeIssue) { | ||
await sendResponse( | ||
event, | ||
context, | ||
'FAILED', | ||
{ reason: 'The Lambda function must have 129MB of memory' }, | ||
physicalResourceId, | ||
); | ||
} | ||
await sendResponse(event, context, 'SUCCESS', {}, physicalResourceId); | ||
return; | ||
} | ||
} | ||
|
||
/*** | ||
* This function is mostly copied from AWS's documentation | ||
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html | ||
*/ | ||
function sendResponse( | ||
event: CloudFormationCustomResourceEvent, | ||
context: Context, | ||
responseStatus: 'SUCCESS' | 'FAILED', | ||
responseData?: Record<string, unknown>, | ||
physicalResourceId?: string, | ||
noEcho?: boolean, | ||
) { | ||
return new Promise((resolve, reject) => { | ||
const responseBody = JSON.stringify({ | ||
Status: responseStatus, | ||
Reason: | ||
responseData?.reason || | ||
'See the details in CloudWatch Log Stream: ' + context.logStreamName, | ||
PhysicalResourceId: physicalResourceId || context.logStreamName, | ||
StackId: event.StackId, | ||
RequestId: event.RequestId, | ||
LogicalResourceId: event.LogicalResourceId, | ||
NoEcho: noEcho || false, | ||
Data: responseData, | ||
}); | ||
|
||
console.log('Response body:', responseBody); | ||
|
||
const parsedUrl = url.parse(event.ResponseURL); | ||
const options = { | ||
hostname: parsedUrl.hostname, | ||
port: 443, | ||
path: parsedUrl.path, | ||
method: 'PUT', | ||
headers: { | ||
'content-type': '', | ||
'content-length': responseBody.length, | ||
}, | ||
}; | ||
|
||
const request = https.request(options, function (response) { | ||
console.log('Status code', response.statusCode); | ||
resolve(context.done()); | ||
}); | ||
|
||
request.on('error', function (error) { | ||
console.log('send(..) failed executing https.request(..): ' + error); | ||
reject(context.done(error)); | ||
}); | ||
|
||
request.write(responseBody); | ||
request.end(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.