AWS for Games Blog

Managing your game studio on AWS part three

Game studios need scalable and efficient management processes to evolve their operations. In our previous posts, we discussed the importance of AWS Control Tower, resource tagging, cost allocation and more. In part 3, we discuss managing Infrastructure as Code and how your studio can use it to create your game’s infrastructure. We will walk through a demo to deploy a multiplayer game in your AWS environment. This will leverage the AWS Cloud Development Kit (AWS CDK), AWS CloudFormation, and AWS CodePipeline. Finally, we will clean up the environment to avoid incurring additional expenses.

Managing your studio environments as code

Infrastructure as Code allows you to define your AWS infrastructure similar to how you write application code. Application code has a defined format and syntax. It has rules that must be followed to successfully create a product. Code is stored in a version management or source control system that logs a history of code development, changes, and bug fixes. When code is compiled or built into applications, we expect a consistent application to be created, through a consistent and repeatable process.

Practicing infrastructure as code means applying the same rigor of application code development to infrastructure provisioning. All configurations are defined in a declarative way and stored in a source control system such as AWS CodeCommit. Infrastructure provisioning, orchestration, and deployment support the use of infrastructure as code. Services like CloudFormation in conjunction with frameworks like the AWS CDK allow you to provision your infrastructure using familiar programming languages. These include C#, Python, or simpler declarative files using JSON or YAML.

This is a powerful tool in the hands of game studios. Let’s look at a typical multi-player title that requires complex backend services including networking and databases. Typically, those resources are deployed and managed manually which consume time and resources inside your studio. Using AWS CDK and CloudFormation you define and deploy entire stacks of infrastructure in a declarative manner. This AWS CDK App is stored in a version control system that allows you to track changes, initiate rollbacks, and review your infrastructure. This is used to define your game server infrastructure or backend services in an easily repeatable and verifiable manner.

Let’s take a deeper dive into both CloudFormation and AWS CDK to illustrate the full benefits of using each with your studio.

Infrastructure as code using CloudFormation

CloudFormation creating an environment from one template. Shows CloudFront and Route 53. They access S3, ELB, and DynamoDB.

Example of CloudFormation creating an environment from one template

CloudFormation lets you model, provision, and manage AWS and third-party resources by treating infrastructure as code. Game Studios can deploy and update compute, database, and other resources in a simple, declarative style. This abstracts away the complexity of specific resource APIs.

CloudFormation uses templates, written in JSON or YAML declarative code files. These describe the intended state of all the resources you want to deploy to support your game and its backend services. This reduces the overall time it takes to setup resources for a new environment. You’re able to use the same template in multiple regions ensuring you have consistent deployments throughout all of your environments.

Because CloudFormation stores the templates as a text file, you’re able to track changes and instantiate them into an existing version control system. This lets you know the changes made, who made them, and when they were made. If you reverse changes to your infrastructure, you use a previous version of your template. Finally, CloudFormation is integrated with AWS CDK and lets you create infrastructure with some of the most popular programming languages today.

Using AWS CDK to manage your infrastructure

Use preconfigured application components model your application, and provision your application with AWS CloudFormation.

The AWS CDK is a framework for defining cloud infrastructure as code and provisioning it through CloudFormation. The AWS CDK allows you to code in one of its supported programming languages such as TypeScript, JavaScript, Python, Java, C# and Go. This allows you to use familiar languages to describe and build AWS resources by generating CloudFormation templates to deploy infrastructure.

As you design AWS CDK applications, it is important to keep best-practices in mind. AWS CDK code is organized into packages that contain one or more Constructs. A Construct is the basic building block of a CDK application. A Construct represents a “cloud component”, such as an S3 bucket, encapsulating the respective CloudFormation structure needed to create that component. You use Constructs to encapsulate reusable cloud components, abstract away complexity, and enforce best-practices. Your code repository should hold no more than one CDK application. Group stacks with one or many Constructs to be maintained by one team. Strive to use a consistent naming convention for your stacks and Constructs. Use a consistent directory structure for your CDK application.

When you build a CDK application, create a pipeline that deploys to your different environments (each isolated in a different AWS Region or account). You use CodeBuild to build your CDK applications, and you use CodePipeline to automate your deployments. The resiliency and security of your CDK application should be part of the initial design. Use AWS Identity and Access Management (IAM) to control access to your AWS resources. Use separate stacks for your application deployments, if required. For instance, create a stack for your computing resources and a separate stack for your database resources.

For more details on best-practices please see the following references and guides:

  1. https://docs.thinkwithwp.com/cdk/v2/guide/best-practices.html
  2. https://thinkwithwp.com/blogs/devops/best-practices-for-developing-cloud-applications-with-aws-cdk/

Multiplayer game deployment demo

Purpose

The demo section will walk you through deploying a Minecraft server to a single account within your AWS environment. Upon successful deployment, you can connect to this server to play the game if you have Minecraft: Java Edition installed on your computer. You will complete the demo section by cleaning up any services and files deployed to avoid ongoing charges. The entire demo will take 1-2 hours or longer depending on how long you play the game.

Assumptions

You have access to an AWS account with the appropriate permissions to deploy the sample application. You have experience using an IDE to modify application code. You are familiar with git and basic commands to clone a repository, add and commit modified files on your development machine, and push updates to a code repository.

Prerequisites

The sample code assumes the application is being deployed into the us-west-2 region; however, this is configurable within the code depending on your preferred region. You will need Administrator or sufficient access to any accounts referenced in the tutorial. Please see the following document for how to setup an IAM administrative user account: https://docs.thinkwithwp.com/SetUp/latest/UserGuide/setup-createadminuser.html

The following are prerequisites for your development machine:

Preparing the sample code

Create an AWS CodeCommit repository named: cdk-container-sample

Screenshot showing creation of a code repository named cdk-container-sample.
Download the sample code from GitHub: aws-multiplayer-game-deployment-demo

Upload the sample code to your cdk-container-sample repository in AWS CodeCommit. Please see the AWS CodeCommit repository document for creating a repository and adding files: https://docs.thinkwithwp.com/codecommit/latest/userguide/how-to-create-repository.html

Once you have cloned the repository from AWS CodeCommit to your development machine, open the sample code in your favorite IDE.

Sample code in the VS Code explorer window. File view is selected and shows the cdk.context.json file highlighted.

Deploying the sample code

Update a few values in the sample code before deploying.

Open the cdk.context.json file and update the 3 values based on your environment:

  • default_repo_branch_name – The name of your AWS CodeCommit repository branch
  • dev_account_id – The AWS account ID where you want to deploy the game server
  • deployment_region – The AWS region where you want to deploy the game server
    • If you change the region from us-west-2 you must update the region in the bin/game-demo-pipeline.ts file and change the region in command line parameters in later steps

Code block in the cdk.context.json file with three parameters: default_repo_branch_name, dev_account_id, and deployment_region.

The main code that creates the code pipeline for deployment is lib/game-demo-pipeline-stack.ts

Comments are included in the code that describe each step and additional options for deploying to multiple accounts.

The security group for accessing the game is open to any IP to allow friends to join if desired. You can lock this down to specific IPs by modifying the security group after the resources are created.

Save your changes and run the following commands in a terminal to begin building the application:

  • Install dependencies:

npm install

  • Run the build:

npm run build

Now ‘bootstrap’ the account to give the CDK permission to deploy resources to the account. The following command will bootstrap the account and provide the execution policy for an Administrator role to execute the initial deployment.

  • Run CDK bootstrap on DevAccount1 using the same account you put in the cdk.context.json dev_account_id field. Ensure that you are using an Administrative User that has access to the account. Replace <DevAccount1> below with the respective account ID and update the region if you choose something other than us-west-2:
npx cdk bootstrap aws://&lt;DevAccount1&gt;/us-west-2 --cloudformation-execution-policies "arn:aws:iam::aws:policy/AdministratorAccess"

CDK execution progress showing resources being created.

Save all changes to the code files and push the updated code to your AWS CodeCommit repository (This is done after bootstrapping because that process modifies the cdk.context.json file):

Terminal window showing git commands to add changed files, commit the changes, and push the changes to CodeCommit.

Now provision the CodePipeline to deploy the application in your account.

npx cdk deploy GameDemoPipelineStack

Creation the GameDemoPipelineStack asking for permission to deploy the changes.

In the command above, “GameDemoPipelineStack” is the stack that is created by the CDK, and it will deploy the CodePipeline to DevAccount1 using CloudFormation. Once the CodePipeline deploys successfully, it will begin to run.

Successful completion of the CloudFormation stack deployment. Deployment time of 255 seconds and total time of 274 seconds.

AWS CodePipeline Console

AWS CodePipeline console showing successful steps for Source and Build.

AWS CloudFormation

AWS CloudFormation console showing the stacks that were created during this demo.

Once the CloudFormation stack GameDemoPipelineStage-MyFargateDockerStack shows UPDATE_COMPLETE, your game server is running. Go to the AWS ECS console and click on the GameDemoPipelineStage-xxx cluster.

The link to the GameDemoPipelineStage-MyFargateDocker-Stack as seen in the AWS ECS console under clusters.

Click on the Tasks tab and click the Task Id:

AWS ECS tasks showing the deployed task is in a running state.

Click on the Network bindings tab to see the IP and Port listed under the External link column:

Container details with the Network bindings tab selected. Shows the IP and Port listed under the External link column.

You can now connect to this server using your Minecraft (Java) client.

From the Minecraft Java Client main screen, click on Multiplayer.

Click Add Server and paste in the IP and Port copied from the External link field into the Server Address:

Minecraft Add Server screen showing where the IP and Port from network bindings should be added.

Click done, select your server, and click Join Server to begin playing.

Enjoy playing the game and invite up to 9 friends to join you.

Cleaning up your environment

When you are done playing the game, you want to cleanup all of the artifacts that were deployed into your environment during this demo to avoid continued charges.

Go to the AWS Console and navigate to CloudFormation.

Choose the GameDemoPipelineStage-MyFargateDockerStack and click the Delete button.

CloudFormation stacks showing selection of the GameDemoPipelineStage-MyFargateDockerStack and the Delete button location.

Once deleted, repeat the process for the GameDemoPipelineStack and the CDKToolkit.

If you encounter a DELETE_FAILED on one of the stacks, choose to delete it again and it will show you the resource that it couldn’t delete and give you a link where you can delete it manually:

Screenshot of resources that failed to delete. You can select to retain the resources and then remove the them manually.

For completeness, check for any S3 buckets that may not have been deleted:

  • cdk-*
  • gamedemopipelinestack*

The last piece to cleanup is the CodeCommit repository with the sample code:

In CodeCommit, with the cdk-container-sample repository selected. Click the Delete repository button to remove it.

We hope you enjoyed working through the demo.

Running your game studio as code

As Game Studios accelerate development of their titles, they need an easier way to manage and deploy their infrastructure in a scalable manner. By following the best-practice of running your Infrastructure as Code you are able to quickly test, deploy and scale game servers and backend services. Integrating and utilizing the AWS CDK and Infrastructure as Code services aids in your management and scaling of your AWS environments throughout the different development cycles of your game.

The AWS CDK is integrated with popular open-sourced tools, including Hashicorp’s Terraform, and can be implemented into existing templates and Infrastructure as Code practices. If want to learn more about Infrastructure as Code on AWS, look at the Introduction to DevOps on AWS whitepaper.

If you want to read more about best-practices for deploying game workloads on AWS, look at the Well Architected Framework Games Lens.

Adam Hatfield

Adam Hatfield

Daniel Whitehead

Daniel Whitehead

Brian Dowhan

Brian Dowhan

JP Velasco

JP Velasco

JP Velasco is a Senior Solutions Architect with Amazon Web Services (AWS). He helps customers navigate their AWS journey by solving complex architecture challenges, leveraging his deep technical expertise across a wide range of cloud services. When not guiding clients, JP enjoys hiking the scenic trails of the Pacific Northwest and staying up-to-date on the latest cloud innovations.

Serge Poueme

Serge Poueme

Serge Poueme is a technology professional with over 15 years of experience driving strategic cloud initiatives and digital transformation. As a Senior Solutions Architect at AWS, he works with gaming customers on cloud adoption, cloud native architectures, and solution guidance for game servers hosting. Based in Montreal, Serge is passionate about technology, music, books, movies, and video games.