Using AWS Lambda functions in your Amazon GameSparks game - Amazon GameSparks

Amazon GameSparks is currently in preview. Changes might be made to this service and to this documentation. We don’t recommend using this service for production workloads.

Using AWS Lambda functions in your Amazon GameSparks game

You can invoke AWS Lambda functions from Amazon GameSparks. With Lambda functions, you can:

  • Leverage other resources, including AWS services and third-party services.

  • Reuse your functions in multiple games across multiple AWS accounts.

  • Implement your functions in multiple programming languages.

This topic shows you how to set up and call a sample Lambda function.

Note

If you are new to GameSparks, we recommend that you begin with Getting started using GameSparks and Using messages to implement game logic in Amazon GameSparks.

Deploy your game

If your game is new and has never been deployed, follow these steps.

To deploy your game for the first time

  1. In the GameSparks navigation pane, choose Dev.

  2. Choose Deploy as new snapshot, enter First snapshot for the description, and then choose Save.

Create the function

Get started calling a Lambda function from your cloud code by creating a sample function named NumberAddition with Python 3.9 syntax.

To create the sample Lambda function

  1. Open the Functions page of the Lambda console.

  2. Choose Create function.

  3. On the Create function page, choose Author from scratch.

  4. Under Basic information, do the following:

    1. For Function name, enter NumberAddition.

    2. For Runtime, choose Python 3.9.

  5. Choose Create function.

  6. On the Code tab, under Code source, replace the code in lambda_function with the following:

    import json def lambda_handler(event, context): num1 = event['num1'] num2 = event['num2'] return { "result": num1 + num2 }
  7. Choose Deploy.

  8. On the Function overview pane, copy the Function ARN so that you can use it in the next section.

An Amazon Resource Name (ARN) is a string that uniquely identifies a resource such as a game, a stage, a Lambda function, or a DynamoDB table. For more information, see Amazon Resource Names (ARNs) in the AWS General Reference.

Give your game permission to access your function

Before your GameSparks game can run your Lambda function, you must provide access using an AWS Identity and Access Management (IAM) policy. Here you’ll create an IAM policy named RunNumberAdditionFunction policy and attach it to your Dev stage IAM role.

To create and attach an IAM policy

  1. Go to the IAM Create policy page, and then choose the JSON tab.

  2. Replace the existing JSON policy with the following policy.

    { "Version": "2012-10-17", "Statement": [ { "Sid": "RunNumberAdditionFunction", "Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-1:111122223333:function:NumberAddition" } ] }
    Important

    For Resource replace the value with the function ARN that you copied in the previous section.

  3. Choose Next: Tags, and then Next: Review.

  4. On the Review policy page, do the following:

    1. For Name, enter RunNumberAdditionFunction.

    2. Choose Create policy.

  5. In the GameSparks console, go to your game.

  6. In the navigation pane, choose Dev.

  7. In the Dev stage configuration panel, choose View in IAM console.

    The IAM console displays the IAM role for your Dev stage.

  8. On the Permissions tab, choose Add permissions, Attach policies.

  9. Under Other permissions policies, search for the RunNumberAdditionFunction policy.

  10. In the search results, select the policy, and then choose Attach policies.

The Dev stage of your game now has permission to call the RunNumberAdditionFunction Lambda function.

Run your function synchronously with Lambda().Invoke()

If your Lambda function runs quickly and you need the data returned to your message, use the Lambda().Invoke() cloud code function to run your Lambda function synchronously. Your cloud code message waits for a response from the Lambda function before proceeding to the next instruction.

Create a request

To run your Lambda function, you’ll create a custom request.

To create the request

  1. In the GameSparks console, go to your game.

  2. In the navigation pane, choose Dev. Then, under Configuration, choose Cloud code.

  3. Create a new request named NumberAddition.

  4. Select Enable client permission.

  5. In the expanded Game-defined request fields pane, do the following:

    1. For Request fields, add the following fields:

      Field name Shape Required

      input1

      Integer

      Yes

      input2

      Integer

      Yes

    2. For Response fields, add the following field:

      Field name Shape Required

      result

      Integer

      Yes

  6. For Request handler, add the following code:

    GameSparks().Logging().Debug("In NumberAddition request handler"); const response = GameSparks().Lambda("NumberAddition").Invoke( { "num1": message.input1, "num2": message.input2 } ); GameSparks().Logging().Debug("Result from Lambda is:"); GameSparks().Logging().Debug(JSON.stringify(response.Payload)); return GameSparks().Messaging().Response({"result": parseInt(response.Payload.result)});
  7. Choose Save.

Deploy and test your request

Now that you’ve created your request, you’re ready to deploy and test it.

To deploy and test your custom request

  1. In the navigation pane, under Dev, Configuration, choose Test harness.

  2. On the Test harness page, choose Deploy as new snapshot.

  3. In the Deploy as new snapshot dialog box, choose Save.

    GameSparks saves and deploys the snapshot. After this is done, proceed to the next step.

  4. Choose your NumberAddition request.

  5. Choose Populate example.

    {
    	"input2": 1,
    	"input1": 1
    }
  6. Choose Send message.

In the Log inspector, you should see results like the following:

✓ Response received

Id: e2e5bfdc-abb1-4848-8bfe-fa06ad6e59ce
Type: Custom.Game.NumberAddition
Category: Response
ResponseTo: 20bd188d-7db4-49e1-a089-ca73433d10a6

{"result":2}

ⓘ Request sent

Id: 20bd188d-7db4-49e1-a089-ca73433d10a6
Type: Custom.Game.NumberAddition
Category: Request

{"input2":1,"input1":1}

Learn more about Lambda().Invoke()

The Lambda().Invoke() function corresponds to the Lambda Invoke API operation with an InvocationType of RequestResponse. For more information, see Invoke in the AWS Lambda Developer Guide.

Run a function asynchronously with InvokeEvent()

You can run your Lambda function asynchronously. This causes your cloud code message to continue to the next instruction without waiting for a response from the Lambda function. To run a Lambda function asynchronously, use the Lambda().InvokeEvent() cloud code function.

GameSparks().Lambda("NumberAddition").InvokeEvent( { "num1": message.input1, "num2": message.input2 } );

Learn more about Lambda().InvokeEvent()

The Lambda().InvokeEvent() function corresponds to the Lambda Invoke API operation with InvocationType of Event. For more information, see Invoke and Asynchronous invocation in the AWS Lambda Developer Guide.

Learn more

For more information about what you can do with Lambda functions see the AWS Lambda Developer Guide.