import { LambdaConfig } from "./lambda-config" ; const aws = require ( "aws-sdk" ) ; /**
 * Create AWS roles for the lambdas
 */ export class LambdaAws { private iam : any = null ; private lambda : any = null ; private lambdaConfig : LambdaConfig = null ; public static create ( lambdaConfig ) { let instance = new LambdaAws ( ) ; instance . lambdaConfig = lambdaConfig ; let aws_security = { accessKeyId : lambdaConfig . AWS_ACCESS_KEY_ID , secretAccessKey : lambdaConfig . AWS_SECRET_ACCESS_KEY , region : "us-east-1" } ; aws . config . update ( aws_security ) ; instance . iam = new aws . IAM ( { apiVersion : "2016-03-01" } ) ; instance . lambda = new aws . Lambda ( { apiVersion : "2016-03-01" } ) ; return instance ; } param roleName returns { Promise < T > } public createRole ( roleName ) { return new Promise ( ( resolve , reject ) => { let roleRrn = null ; let assumeRolePolicy = { Version : "2012-10-17" , Statement : [ { Sid : "" , Effect : "Allow" , Principal : { Service : "lambda.amazonaws.com" } , Action : "sts:AssumeRole" } ] } ; let createParams = { AssumeRolePolicyDocument : JSON . stringify ( assumeRolePolicy , null , 2 ) , RoleName : roleName } ; let createPromise = this . iam . createRole ( createParams ) . promise ( ) ; createPromise . then ( ( data ) => { roleRrn = data . Role . Arn ; return this . putRolePolicy ( roleName ) ; } ) . then ( ( data ) => { resolve ( roleRrn ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } param roleName returns { Promise < T > } public getRole ( roleName ) { return new Promise ( ( resolve , reject ) => { let getRolePromise = this . iam . getRole ( { "RoleName" : roleName } ) . promise ( ) ; getRolePromise . then ( ( data ) => { resolve ( data . Role . Arn ) ; } ) . catch ( ( err ) => { if ( err . code === "NoSuchEntity" ) { resolve ( null ) ; } else { reject ( err ) ; } } ) ; } ) ; } param roleName returns { Promise < T > } public deleteRole ( roleName ) { return new Promise ( ( resolve , reject ) => { this . deleteRolePolicy ( roleName ) . then ( ( data ) => { return this . iam . deleteRole ( { "RoleName" : roleName } ) . promise ( ) ; } ) . then ( ( data ) => { resolve ( null ) ; } ) . catch ( ( err ) => { if ( err . code === "NoSuchEntity" ) { resolve ( null ) ; } else { reject ( err ) ; } } ) ; } ) ; } param roleName returns { Promise < T > } private deleteRolePolicy ( roleName ) { return new Promise ( ( resolve , reject ) => { let policyName = roleName + "-access" ; this . iam . deleteRolePolicy ( { "PolicyName" : policyName , "RoleName" : roleName } ) . promise ( ) . then ( ( data ) => { resolve ( data ) ; } ) . catch ( ( err ) => { if ( err . code === "NoSuchEntity" ) { resolve ( err . code ) ; } else { reject ( err ) ; } } ) ; } ) ; } param roleName returns { Promise < T > } private putRolePolicy ( roleName ) { return new Promise ( ( resolve , reject ) => { let rolePolicy = { Version : "2012-10-17" , Statement : [ { Sid : "" , Action : [ "dynamodb:DeleteItem" , "dynamodb:GetItem" , "dynamodb:PutItem" , "dynamodb:Query" , "dynamodb:Scan" , "dynamodb:UpdateItem" ] , Effect : "Allow" , Resource : "*" } , { Effect : "Allow" , Action : [ "logs:*" ] , Resource : "arn:aws:logs:*:*:*" } , { Effect : "Allow" , Action : [ "s3:GetObject" ] , Resource : "arn:aws:s3:::'$source_bucket'/*" } , { Effect : "Allow" , Action : [ "s3:PutObject" ] , Resource : "arn:aws:s3:::'$target_bucket'/*" } ] } ; let putRolePolicyParams = { PolicyDocument : JSON . stringify ( rolePolicy , null , 2 ) , PolicyName : roleName + "-access" , RoleName : roleName } ; let putRolePromise = this . iam . putRolePolicy ( putRolePolicyParams ) . promise ( ) ; putRolePromise . then ( ( data ) => { resolve ( data ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } public invokeLambda ( functionName , payload ) { return new Promise ( ( resolve , reject ) => { let params = { FunctionName : functionName , InvocationType : "RequestResponse" , LogType : "None" , Payload : JSON . stringify ( payload ) } ; let invokePromise = this . lambda . invoke ( params ) . promise ( ) ; invokePromise . then ( ( data ) => { resolve ( data ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } public deleteFunction ( functionName ) { return new Promise ( ( resolve , reject ) => { this . lambda . deleteFunction ( { "FunctionName" : functionName } ) . promise ( ) . then ( ( data ) => { resolve ( data ) ; } ) . catch ( ( err ) => { if ( err . code === "NoSuchEntity" ) { resolve ( err . code ) ; } else { reject ( err ) ; } } ) ; } ) ; } }