Development Blog

Pre Production: AWS Lambda, API Gateway and accessing list fleets from C++

Milestone Programming Pre-Production
After confirming that the server build runs correctly on an EC2 instance, the next step was integrating AWS data retrieval within Unreal C++. To begin, a simple scenario was chosen: fetching the list of currently available fleets (including both Anywhere and Managed fleets).

Understanding AWS Lambda

The first step was learning about AWS Lambda. Despite being called "serverless," Lambda still operates on servers. However, the key difference is that Lambda functions only execute when triggered, remaining inactive otherwise. This on-demand execution model improves efficiency and reduces costs.
Each Lambda function invocation is logged in Amazon CloudWatch, providing developers with a record of function executions for debugging and monitoring purposes.

Creating a Simple Lambda Function

To get started, a basic "Hello, World!" Lambda function was created:
export const handler = async (event) => {
  // TODO implement

  const newObject = {
    first : 12,
    second : "This is the second property",
    third : event
  };
  console.log("new object: ", JSON.stringify(newObject, null, 2));
  
  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  };
  return response;
};
The console.log statement allows logging useful information to CloudWatch, making debugging easier.

Fetching Fleet Lists Using Lambda

The next step was creating a Lambda function to retrieve a list of available fleets. Fortunately, AWS provides documentation on how to achieve this: ListFleets API Reference.
By default, AWS Lambda does not have permission to interact with other AWS services, such as GameLift. To allow Lambda to retrieve fleet information, an appropriate IAM policy must be added.

Adding a Policy to the Lambda Execution Role

To grant Lambda access to GameLift, follow these steps:
  1. Navigate to the Lambda function in the AWS Console.
  2. Go to the Configuration tab.
  3. Click on Permissions in the left-hand menu.
  4. Under the Role name header, click the linked role name. This opens the IAM role associated with the Lambda function.
  5. Click Add permissionsCreate inline policy.
  6. Switch to the JSON tab and enter the following policy:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "GameLiftListFleetsAccess",
      "Effect": "Allow",
      "Action": [
        "gamelift:ListFleets"
      ],
      "Resource": "*"
    }
  ]
}
  1. Click Review policy, provide a name, and save it.
With the Lambda function set up, the next step was enabling communication between Unreal Engine and AWS. This is achieved using Amazon API Gateway, which allows HTTP requests to be made from C++.
Since the goal is to retrieve information from AWS, the ideal request type to use is GET.

Designing the Unreal C++ Class

With this in mind, it was necessary to design the structure of the Unreal C++ class that would handle the API requests. The class would:
  1. Send HTTP POST requests to the API Gateway endpoint.
  2. Handle responses from AWS Lambda.
  3. Parse the fleet data returned by the API.
After following the design structures for the classes, the project has now access to the List Fleets information