Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
'use strict';
let clearAll = () => global.CONTEXT = undefined;
let replaceAllWith = ctx => global.CONTEXT = ctx;
let set = (key, value) => {
if (!key.startsWith("x-correlation-")) {
key = "x-correlation-" + key;
}
'use strict';
const co = require('co');
const _ = require('lodash');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
// Data loaded from S3 and chached in case of recursion.
let cached;
module.exports.handler = (event, context, callback) => {
let position = event.position || 0;
do {
... // process the tasks in small batches that can be completed in, say, less than 10s
// when there's less than 10s left, stop
} while (position < totalTaskCount && context.getRemainingTimeInMillis() > 10000);
if (position < totalTaskCount) {
let newEvent = Object.assign(event, { position });
{
"Comment": "Applying the Saga pattern with AWS Lambda and Step Functions",
"StartAt": "BookHotel",
"States": {
"BookHotel": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:{AccountID}:function:lambda-saga-dev-book-hotel",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
type Player = { Name: string; Score: int; Id: System.Guid }
type PlayerDTO =
{ Name: string; Score: int; Id: string }
with static member op_Implicit(p: Player) : PlayerDTO =
{ Name = p.Name; Score = p.Score; Id = p.Id.ToString() }
let p: Player = { Name = "Yan"; Score = 42; Id = System.Guid.NewGuid() }
let dto = PlayerDTO.op_Implicit(p)
using System;
public sealed class Player
{
public Player(string name, long score)
{
Name = name;
Score = score;
Id = Guid.NewGuid().ToString();
}
let publishSNS = segment => {
return new Promise((resolve, reject) => {
console.log('publishing to SNS topic');
let f = co.wrap(function* (subsegment) {
...
subsegment.addAnnotation('topic', topicArn);
subsegment.addMetadata('message', 'test');
let req = {
@theburningmonk
theburningmonk / utils.js
Created June 25, 2017 20:19
wraps the https module using the X-Ray SDK
'use strict';
const AWSXRay = require('aws-xray-sdk');
const https = AWSXRay.captureHTTPs(require('https'));
console.log(AWSXRay);
let request = (method, hostname, path) => {
const options = { hostname, port: 443, path, method };
// ... omitted for brevity
module.exports.handler = co.wrap(function* (event, context, callback) {
console.log(JSON.stringify(event));
console.log(JSON.stringify(context));
global.hostname = event.headers.Host;
global.accountId = event.requestContext.accountId;
global.requestId = event.requestContext.requestId;
'use strict';
const co = require('co');
const Promise = require('bluebird');
const AWS = require('aws-sdk');
const cloudwatch = Promise.promisifyAll(new AWS.CloudWatch());
let isColdstart = false;
let trackColdstart = co.wrap(function* (funcName) {