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.
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
- Download the CloudFormation template.
- Sign in to the AWS Management Console and visit the CloudFormation console at https://console.thinkwithwp.com/cloudformation/.
- 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.
- On the CloudFormation console landing page, choose Create stack.
- In the Specify template section, choose Upload a template file, choose Choose file, and select the template file you downloaded.
- Choose Next.
- In the Stack name section, enter a stack name.
- 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. - Choose Next.
- On the Configure stack options page, choose Next.
- On the Review page, review and confirm the settings. Check the box acknowledging that the template will create IAM resources with custom names.
- Choose Submit to deploy the stack.
- 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
- Create a Windows image builder. An IAM role is not required.
- 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
- Create the folder
C:\Scripts
. - Save the PowerShell script as
C:\Scripts\Show-SessionInfo.ps1
. - If you updated AWS Tools for PowerShell in Step 3, remove the leading
#
from the line# Import-Module AWS.Tools.Lambda
. - 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.
- 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
- Create an AppStream 2.0 stack.
- Associate the stack with the fleet you created in Step 5.
- Connect to the stack.
- Choose the application catalog, then choose Session Info.
- 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
- Delete your image builder
- Stop and delete your fleet.
- 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. |