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.
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.
Topics
Deploy your game
If your game is new and has never been deployed, follow these steps.
To deploy your game for the first time
-
In the GameSparks navigation pane, choose Dev.
-
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
-
Open the Functions page
of the Lambda console. -
Choose Create function.
-
On the Create function page, choose Author from scratch.
-
Under Basic information, do the following:
-
For Function name, enter NumberAddition.
-
For Runtime, choose Python 3.9.
-
-
Choose Create function.
-
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 }
-
Choose Deploy.
-
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
-
Go to the IAM Create policy page
, and then choose the JSON tab. -
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. -
Choose Next: Tags, and then Next: Review.
-
On the Review policy page, do the following:
-
For Name, enter RunNumberAdditionFunction.
-
Choose Create policy.
-
-
In the GameSparks console
, go to your game. -
In the navigation pane, choose Dev.
-
In the Dev stage configuration panel, choose View in IAM console.
The IAM console displays the IAM role for your Dev stage.
-
On the Permissions tab, choose Add permissions, Attach policies.
-
Under Other permissions policies, search for the RunNumberAdditionFunction policy.
-
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
-
In the GameSparks console
, go to your game. -
In the navigation pane, choose Dev. Then, under Configuration, choose Cloud code.
-
Create a new request named NumberAddition.
-
Select Enable client permission.
-
In the expanded Game-defined request fields pane, do the following:
-
For Request fields, add the following fields:
Field name Shape Required input1
Integer
Yes
input2
Integer
Yes
-
For Response fields, add the following field:
Field name Shape Required result
Integer
Yes
-
-
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)});
-
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
-
In the navigation pane, under Dev, Configuration, choose Test harness.
-
On the Test harness page, choose Deploy as new snapshot.
-
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.
-
Choose your NumberAddition request.
-
Choose Populate example.
{ "input2": 1, "input1": 1 }
-
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.