Skip to content

Commit 3ce1363

Browse files
committed
update examples
1 parent 1236273 commit 3ce1363

File tree

5 files changed

+133
-7
lines changed

5 files changed

+133
-7
lines changed

examples/ts/app/cloudformation.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { cloudFormation } from '@manwaring/lambda-wrapper';
22
import 'source-map-support/register';
3+
// import { send, SUCCESS } from './send';
4+
import { sendResponse } from './native-send';
35

4-
export const handler = cloudFormation(async ({ event, success, failure }) => {
6+
// export const handler = cloudFormation(async ({ event, success, failure }) => {
7+
// try {
8+
// success(event);
9+
// } catch (err) {
10+
// failure(err);
11+
// }
12+
// });
13+
14+
export const handler = (event, context, callback) => {
515
try {
6-
return success(event);
16+
// send(event, context, SUCCESS);
17+
sendResponse(event, context);
718
} catch (err) {
8-
return failure(err);
19+
console.log('Error', err);
20+
// send(event, context, SUCCESS);
21+
sendResponse(event, context);
922
}
10-
});
23+
};

examples/ts/app/native-send.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Send response to the pre-signed S3 URL
2+
export function sendResponse(event, context, responseStatus = 'SUCCESS', responseData = {}) {
3+
var responseBody = JSON.stringify({
4+
Status: responseStatus,
5+
Reason: 'See the details in CloudWatch Log Stream: ' + context.logStreamName,
6+
PhysicalResourceId: context.logStreamName,
7+
StackId: event.StackId,
8+
RequestId: event.RequestId,
9+
LogicalResourceId: event.LogicalResourceId,
10+
Data: responseData
11+
});
12+
13+
console.log('RESPONSE BODY:\n', responseBody);
14+
15+
var https = require('https');
16+
var url = require('url');
17+
18+
var parsedUrl = url.parse(event.ResponseURL);
19+
var options = {
20+
hostname: parsedUrl.hostname,
21+
port: 443,
22+
path: parsedUrl.path,
23+
method: 'PUT',
24+
headers: {
25+
'content-type': '',
26+
'content-length': responseBody.length
27+
}
28+
};
29+
30+
console.log('SENDING RESPONSE...\n', parsedUrl, responseBody);
31+
32+
var request = https.request(options, function(response) {
33+
console.log('STATUS: ' + response.statusCode);
34+
console.log('HEADERS: ' + JSON.stringify(response.headers));
35+
// Tell AWS Lambda that the function execution is done
36+
context.done();
37+
});
38+
39+
request.on('error', function(error) {
40+
console.log('sendResponse Error:' + error);
41+
// Tell AWS Lambda that the function execution is done
42+
context.done();
43+
});
44+
45+
// write data to request body
46+
request.write(responseBody);
47+
request.end();
48+
}

examples/ts/app/send.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { CloudFormationCustomResourceEvent, Context } from 'aws-lambda';
2+
import axios from 'axios';
3+
import * as retry from 'async-retry';
4+
5+
const MAX_RETRIES = 10;
6+
7+
type ResponseStatus = 'SUCCESS' | 'FAILED';
8+
export const SUCCESS: ResponseStatus = 'SUCCESS';
9+
export const FAILED: ResponseStatus = 'FAILED';
10+
11+
export function send(
12+
event: CloudFormationCustomResourceEvent,
13+
context: Context,
14+
Status: ResponseStatus,
15+
Data: any = {},
16+
physicalResourceId: any = false,
17+
NoEcho: any = false
18+
) {
19+
const body = JSON.stringify({
20+
Status,
21+
Reason: `See the details in CloudWatch Log Stream: ${context.logStreamName}`,
22+
PhysicalResourceId: physicalResourceId || context.logStreamName,
23+
StackId: event.StackId,
24+
RequestId: event.RequestId,
25+
LogicalResourceId: event.LogicalResourceId,
26+
NoEcho,
27+
Data
28+
});
29+
try {
30+
sendResponse(event.ResponseURL, body)
31+
.then(() => {
32+
console.log('Promise chain success');
33+
context.done();
34+
})
35+
.catch(err => {
36+
console.error('Promise chain error', err);
37+
context.done();
38+
});
39+
console.log('After promise chain');
40+
} catch (err) {
41+
console.log('Try catch error', err);
42+
}
43+
console.log('Reached end of sending calls');
44+
}
45+
46+
async function retrySendResponse(url, body): Promise<any> {
47+
console.log('Initiating retry call');
48+
return await retry(
49+
async (abort, attemptCount) => {
50+
console.log(`Making retry call ${attemptCount} of ${MAX_RETRIES}`);
51+
await sendResponse(url, body);
52+
},
53+
{ retries: MAX_RETRIES }
54+
);
55+
}
56+
57+
async function sendResponse(url, body): Promise<any> {
58+
const config = { headers: { 'content-type': '', 'content-length': body.length } };
59+
console.log('Making request with params', url, body, config);
60+
const response = await axios.put(url, body, config);
61+
console.log('Completed making request', response);
62+
return response;
63+
}

examples/ts/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
"deploy": "sls deploy"
88
},
99
"dependencies": {
10-
"source-map-support": "^0.5.10",
11-
"@manwaring/lambda-wrapper": "*"
10+
"@manwaring/lambda-wrapper": "*",
11+
"async-retry": "^1.2.3",
12+
"axios": "^0.19.0",
13+
"source-map-support": "^0.5.10"
1214
},
1315
"devDependencies": {
1416
"@types/aws-lambda": "^8.10.17",

examples/ts/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
service:
2-
name: lambda-wrapper-ts
2+
name: lambda-wrapper-ts3
33

44
plugins:
55
- serverless-webpack

0 commit comments

Comments
 (0)