Desktop and Application Streaming

Display session expiration and a countdown timer in Amazon AppStream 2.0

A common use case for Amazon AppStream 2.0 is streaming resource-intensive applications that perform long-running calculations or simulations. If the session ends while the simulation is running, user productivity and satisfaction suffer. Consider the following scenario: your users run simulations that normally take three hours to complete. To control costs, you configure your AppStream 2.0 fleet with a maximum session duration of eight hours. This allows two simulations per workday, plus time to review the results of the first simulation and adjust the parameters before starting the second one. If a user spends more than the allotted two hours reviewing results, their session will end before the second simulation completes. If they are aware that insufficient time remains in their session, they can proactively start a new session.

This post shows you how to display the session expiration and remaining time in a simple graphical window within the session.

Time to read 5 minutes
Time to complete 45 minutes
Cost to complete (estimated) $0.10 to run a stream.standard.medium image builder for one hour in the US East (N. Virginia) region. Approximately $0.01 per month for 10,000 streaming sessions, in addition to normal AppStream 2.0 streaming instance charges.
Learning level Advanced (300)
Services used Amazon AppStream 2.0, AWS CloudFormation, AWS Lambda

Overview of solution

A PowerShell script on the instance reads user metadata and passes it to a Lambda function. The Lambda function then calls the AppStream 2.0 API, validates the session details, and returns the MaxExpirationTime of the user’s session to the PowerShell script, which displays session information in a window.

Architecture diagram showing Amazon AppStream 2.0 instance, AWS Lamba function, and AppStream 2.0 API.

The date and time are displayed according to the time zone and locale configured by the user. The window updates once per minute. When the session has less than 10 minutes remaining, the Time remaining text becomes bold and the window remains in the foreground.

Walkthrough

The code for this solution is available on GitHub. A CloudFormation template deploys the Lambda function and optionally creates an IAM role for AppStream 2.0 fleets.

Prerequisites

For this walkthrough, you should have the following prerequisites:

  • An AWS account
  • Familiarity with creating an AppStream 2.0 image, fleet, and stack

Step 1: Deploy the CloudFormation stack

  1. Download the CloudFormation template.
  2. Sign in to the AWS Management Console and visit the CloudFormation console at https://console.aws.amazon.com/cloudformation/.
  3. This solution runs in a single AWS Region, for use with AppStream 2.0 fleets in the same Region. To deploy the solution in a different Region, use the Region selector in the console navigation bar.
  4. On the CloudFormation console landing page, choose Create stack.
  5. In the Specify template section, choose Upload a template file, choose Choose file, and select the template file you downloaded.
  6. Choose Next.
  7. In the Stack name section, enter a stack name.
  8. In the Parameters section, review the parameters for the template. If the value of Create fleet IAM role is Yes (the default), an IAM role is created for use by AppStream 2.0 fleets.
  9. Choose Next.
  10. On the Configure stack options page, choose Next.
  11. On the Review page, review and confirm the settings. Check the box acknowledging that the template will create IAM resources with custom names.
  12. Choose Submit to deploy the stack.
  13. Check the status of the stack in the CloudFormation console in the Status column. You should see a status of CREATE_COMPLETE in approximately one minute.

Step 2: Create an image builder

  1. Create a Windows image builder. An IAM role is not required.
  2. Connect to your image builder as an administrator.

Step 3: Update AWS Tools for PowerShell (optional)

At the time of publication, AppStream 2.0 base images include the legacy single, large-module version of AWS Tools for PowerShell. To speed up script execution, you can update to the modularized version. For more information see AWS Tools for PowerShell is now generally available with version 4.0 on the AWS Developer Tools Blog. If your image includes other PowerShell scripts that depend on the legacy version, skip this step.

Open PowerShell as an administrator and run the following commands:

# Uninstall legacy version of AWS Tools for PowerShell.
$legacy = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq "AWS Tools for Windows"}
$legacy.Uninstall()

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
# Install modularized version and Lambda module.
Install-Module -Name AWS.Tools.Installer -Force
Install-AWSToolsModule AWS.Tools.Lambda -CleanUp -Scope AllUsers -Force

Step 4: Configure the PowerShell script

  1. Create the folder C:\Scripts.
  2. Save the PowerShell script as C:\Scripts\Show-SessionInfo.ps1.
  3. If you updated AWS Tools for PowerShell in Step 3, remove the leading # from the line # Import-Module AWS.Tools.Lambda.
  4. Open a command prompt and run the following command to add the script to the application catalog:
    "C:\Program Files\Amazon\Photon\ConsoleImageBuilder\Image-Assistant.exe"^
     add-application --name session-info --display-name "Session Info"^
     --absolute-app-path C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe^
     --launch-parameters="-WindowStyle Hidden -F C:\Scripts\Show-SessionInfo.ps1"

    When run on an image builder, the script doesn’t invoke the Lambda function. Instead, it uses a simulated session expiration 11 minutes in the future.

    Screenshot of Session Info window showing a simulated session with 10 minutes remaining.

  5. Add other applications and create your image.

Step 5: Create a fleet

Create an AppStream 2.0 fleet with the IAM role named SessionExpirationFleetRole_<region>, for example, SessionExpirationFleetRole_us-east-1. The fleet must have access to the internet or a Lambda interface VPC endpoint. See Configuring interface VPC endpoints for Lambda for more information.

Step 6: Create a stack and test

  1. Create an AppStream 2.0 stack.
  2. Associate the stack with the fleet you created in Step 5.
  3. Connect to the stack.
  4. Choose the application catalog, then choose Session Info.
    Screenshot of the AppStream 2.0 toolbar and "Session Info" in the catalog.
  5. The Session Info window appears. The time and date shown are in the time zone you configured as an end user. For more information on configuring a time zone, see Configure Regional Settings.

Cleaning up

  1. Delete your image builder
  2. Stop and delete your fleet.
  3. Delete the CloudFormation stack.

If you choose to delete the CloudFormation stack but not the fleet, you must remove the IAM role from the fleet using the AWS CLI or API, or the fleet will fail to start in the future. To remove the IAM role via CLI, run the following command. Replace fleet-name with the name of your fleet.

aws appstream update-fleet --name fleet-name --attributes-to-delete IAM_ROLE_ARN

Conclusion

This post showed how to display the session expiration and time remaining in a simple graphical window. You can customize the code to display additional information relevant to your users.

Dylan Barlett is a Senior End User Computing Solutions Architect at AWS. He works with customers of all types and sizes to deploy and optimize AWS EUC services. In his spare time, he enjoys traveling and home improvement projects.