UNPKG

simplify-cli

Version:
97 lines (94 loc) 3.23 kB
AWSTemplateFormatVersion: '2010-09-09' Description: CFN Template for Lambda Randomness. Parameters: Environment: Type: String Description: Name of an environment. 'dev', 'staging', 'prod' and any name. AllowedPattern: ^.*[^0-9]$ ConstraintDescription: Must end with non-numeric character. Resources: LambdaRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: - sts:AssumeRole Effect: Allow Principal: Service: - lambda.amazonaws.com Version: 2012-10-17 ManagedPolicyArns: - arn:aws:iam::aws:policy/AWSLambdaExecute Path: / LambdaFunction: Type: AWS::Lambda::Function Properties: Runtime: nodejs12.x Code: ZipFile: | var BIG_INT_1 = process.env.BIG_INT_1 || 8543785353454 var BIG_INT_2 = process.env.BIG_INT_2 || 795683477236463256 Number.prototype.toFixedSpecial = function (n) { var str = this.toFixed(n); if (str.indexOf('e+') < 0) return str; return str.replace('.', '').split('e+').reduce(function (p, b) { return p + Array(b - p.length + 2).join(0); }) + Array(n + 1).join(0); }; var randprng_lcg = function (n, seed) { var results = [] var timestamp = new Date().getTime() var a = BIG_INT_1, b = BIG_INT_2, m = 1 / timestamp var lastrng = (a * seed + b) % m; [...Array(n).keys()].forEach(i => { timestamp = new Date().getTime().toString() m = 1 / timestamp var fraction = (a * lastrng + b) % m var expnumber = (fraction).toExponential().replace('-', '') var sequence = Number(expnumber).toFixedSpecial(16).replace('.', '') lastrng = sequence; results.push(parseInt(sequence.slice(0, 6))) }) return results } module.exports.handler = function (event, context, callback) { var startedT = Date.now() var _100rnd = randprng_lcg(100, parseInt(Buffer.from(context.awsRequestId, 'utf8').toString('hex'), 16)) var valueRnd = _100rnd[parseInt(Math.random() * 100)] / 1000 setTimeout(function () { callback(null, { message: `Done with ${Date.now() - startedT} ms`, elapsed: valueRnd, randoms: _100rnd }) }, valueRnd) } Handler: index.handler MemorySize: 128 Timeout: 10 Role: Fn::GetAtt: - LambdaRole - Arn Environment: Variables: ENV: Fn::Sub: ${Environment} TZ: UTC Outputs: Region: Value: !Ref AWS::Region StackId: Value: !Ref AWS::StackId LambdaRoleARN: Description: Role for Lambda execution. Value: Fn::GetAtt: - LambdaRole - Arn FunctionName: Value: Ref: LambdaFunction FunctionARN: Description: Lambda function ARN. Value: Fn::GetAtt: - LambdaFunction - Arn