Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion handlers/handleCustomResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,23 @@ export default async function handleCustomResource(event, context) {
logDebug('invocation', invocation);
logDebug('handleCustomResource', { requestType: event.RequestType, requestId: context.awsRequestId });
console.log('Custom Resource request:', event.RequestType);
return {};

if (!event.ResponseURL) {
throw new Error('Missing ResponseURL');
}

const responseBody = JSON.stringify({
Status: 'SUCCESS',
PhysicalResourceId: event.PhysicalResourceId || context.awsRequestId,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId
});

await fetch(event.ResponseURL, {
method: 'PUT',
body: responseBody
});

return;
}
33 changes: 33 additions & 0 deletions tests/handleCustomResource.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { jest } from '@jest/globals';
import handleCustomResource from '../handlers/handleCustomResource.js';

describe('handleCustomResource', () => {
test('PUTs response to ResponseURL', async () => {
const fetchMock = jest.spyOn(global, 'fetch').mockResolvedValue({ ok: true });
const event = {
RequestType: 'Create',
ResponseURL: 'https://example.com',
StackId: 's',
RequestId: 'r',
LogicalResourceId: 'l'
};
const context = { awsRequestId: 'id1' };
await handleCustomResource(event, context);
expect(fetchMock).toHaveBeenCalledWith('https://example.com', expect.objectContaining({ method: 'PUT' }));
const body = fetchMock.mock.calls[0][1].body;
expect(JSON.parse(body)).toMatchObject({
Status: 'SUCCESS',
PhysicalResourceId: 'id1',
StackId: 's',
RequestId: 'r',
LogicalResourceId: 'l'
});
fetchMock.mockRestore();
});

test('throws when ResponseURL missing', async () => {
const event = { RequestType: 'Create' };
const context = { awsRequestId: 'id1' };
await expect(handleCustomResource(event, context)).rejects.toThrow('Missing ResponseURL');
});
});
7 changes: 5 additions & 2 deletions tests/handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ describe('handler dispatch', () => {
});

test('handles Custom Resource event', async () => {
const event = { RequestType: 'Create', ResponseURL: 'https://example.com' };
const fetchMock = jest.spyOn(global, 'fetch').mockResolvedValue({ ok: true });
const event = { RequestType: 'Create', ResponseURL: 'https://example.com', StackId: 's', RequestId: 'r', LogicalResourceId: 'l' };
const context = { awsRequestId: '1' };
const result = await handler(event, context);
expect(result).toEqual({});
expect(result).toBeUndefined();
expect(fetchMock).toHaveBeenCalledWith('https://example.com', expect.objectContaining({ method: 'PUT' }));
fetchMock.mockRestore();
});

test('handles Cognito event', async () => {
Expand Down
Loading