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 Amazon GameSparks and Using messages to implement game logic in Amazon GameSparks.
Topics
Deploy your game
If your game is new and you've never deployed it, follow these steps.
To deploy your game for the first time
-
In the GameSparks console
, choose the name of your game. -
In the navigation pane, choose Dev.
-
On the Dev page, under Snapshot, choose Deploy as new snapshot.
-
In the Deploy as new snapshot dialog box, optionally enter a description, and then choose Save.
Create a Lambda 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 NumberAddition
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. You need this ARN 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 grant permission by using an AWS Identity and Access Management (IAM) policy. Create an IAM policy named RunNumberAdditionFunction policy and attach it to your Dev stage IAM role.
To create and attach an IAM policy
-
Open the IAM Create policy page
. -
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
Replace the value of
Resource
with the ARN that you copied in the previous section. -
Choose Next: Tags, and then choose Next: Review.
-
On the Review policy page, do the following:
-
For Name, enter RunNumberAdditionFunction.
-
Choose Create policy.
-
-
In the GameSparks console
, choose the name of your game. -
In the navigation pane, choose Dev.
-
Under Dev stage configuration, choose View in IAM console.
The IAM console opens to 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 using 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 message waits for a response from the Lambda function before proceeding to the next instruction.
Create the request
To run your Lambda function, create a request and use the Lambda.Invoke
cloud code function.
To create the NumberAddition
request
-
In the GameSparks console
, choose the name of your game. -
In the navigation pane, choose Dev. Then, under Configuration, choose Cloud code.
-
On the Cloud code page, choose Create message.
-
In the Create message dialog box, do the following:
-
Choose Request.
-
For Name, enter
NumberAddition
. -
(Optional) Enter a Description.
-
Choose Create.
-
-
Select Player.
-
In the expanded Game-defined request fields pane, add the following fields:
Request fields
Field name Shape Required input1
Integer
Yes
input2
Integer
Yes
Response fields
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 using the test harness.
To deploy and test your custom request
-
In the GameSparks console
, choose the name of your game. -
In the navigation pane, choose Dev. Then, under Configuration, choose Test harness.
-
On the Test harness page, if you see the Deploy as new snapshot button, then choose it.
-
In the Deploy as new snapshot dialog box, optionally enter a description, and then choose Save.
GameSparks saves and deploys the snapshot. After this is done, you can proceed.
-
-
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
For more information about the Lambda.Invoke
cloud code function, see Lambda.Invoke.
Run a function asynchronously using 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
For more information about this cloud code function, see Lambda.InvokeEvent.
Learn more about Lambda
For more information about what you can do with Lambda functions, see the AWS Lambda Developer Guide.