private getAllocateGamerFunction()

in infrastructure/cdk/lib/layer/processingLayer.ts [63:131]


    private getAllocateGamerFunction() {
        /**
    * This function requires access to 
    * SystemsManager
    *      process.env.SESSION_PARAMETER = /<getAppRefName>/session
    * DynamoDB Tables
    *      process.env.SESSION_CONTROL_TABLENAME = getAppRefName+'SessionControl'
    */
        let sessionParameter : any;
        let parameterNameForLambda : string;
        if (SESSION_PARAMETER) {
            sessionParameter =  this.properties.getParameter('parameter.session');
            parameterNameForLambda =  (sessionParameter).name;
        }
        else {
            sessionParameter = { parameterName : '/'+this.properties.getApplicationName().toLocaleLowerCase()+'/session'};
            parameterNameForLambda = sessionParameter.parameterName;
        }
        let sessionControlTable : Table = <Table> this.properties.getParameter('table.sessioncontrol');
        if (sessionParameter && sessionControlTable) {
            let createdFunction: Lambda.Function =
                new Lambda.Function(this, this.properties.getApplicationName() + 'AllocateGamerFn', {
                    runtime:Lambda.Runtime.NODEJS_14_X,
                    handler: 'index.handler',
                    code: Lambda.Code.fromAsset(path.join(lambdasLocation,'allocateGamer')),
                    environment: {
                        'SESSION_CONTROL_TABLENAME': sessionControlTable.tableName,
                        'SESSION_PARAMETER': parameterNameForLambda
                    }
                    , functionName: this.properties.getApplicationName() + 'AllocateGamerFn'
                    , description: 'This function supports the allocation of gamers when the game is to start'
                    , memorySize: 128
                    , timeout: Duration.seconds(60)
                    , role: new IAM.Role(this, this.properties.getApplicationName() + 'AllocateGamerFn_Role', {
                        roleName: this.properties.getApplicationName() + 'AllocateGamerFn_Role'
                        , assumedBy: new IAM.ServicePrincipal('lambda.amazonaws.com')
                        , managedPolicies : [ ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole') ]
                        , inlinePolicies: {
                            'DynamoDBPermissions' :
                                new IAM.PolicyDocument({
                                    statements : [
                                        new IAM.PolicyStatement({
                                            resources : [  sessionControlTable.tableArn ]
                                            ,actions : [
                                                "dynamodb:GetItem",
                                                "dynamodb:UpdateItem",
                                                "dynamodb:Scan",
                                                "dynamodb:Query"
                                            ]
                                        })
                                    ]
                                }),
                            'SystemsManagerPermissions':
                                new IAM.PolicyDocument({
                                    statements : [                                    
                                        new IAM.PolicyStatement({
                                            resources: ['arn:aws:ssm:'+this.properties.region+':'+this.properties.accountId+':parameter'+sessionParameter.parameterName ],
                                            actions: [ 'ssm:GetParameter' , 'ssm:GetParameters']
                                        })
                                    ]
                                })
                        }
                    })
                }
            );
            return createdFunction;
        }
        else return undefined;
    }