We are seeing what I would like to describe as a bug in AppStream, allthoug the expected behaviour is just based on intuition, and not really documented.
Problem Description
When running the start script in AppStream, the environment variable APPSTREAM_SESSION_CONTEXT
contains the session-context
specified for the previous session (same user, same fleet), not the current session.
If I for example run (Session #1)
aws appstream create-streaming-url --stack-name my-stack --fleet-name my-fleet --user-id 10 --application-id notepad --session-context CONTEXT-A
And have a start script including this line:
Write-Output "CONTEXT=$($env:APPSTREAM_SESSION_CONTEXT)"
It will print
CONTEXT=
Now, if I expire that session and start a new (Session #2) using
aws appstream create-streaming-url --stack-name my-stack --fleet-name my-fleet --user-id 10 --application-id notepad --session-context CONTEXT-B
It will print (with an A and not the expected B)
CONTEXT=CONTEXT-A
This is only the case in the shell of the start script, not in a shell later opened by the user. In a shell opened by the user, the "correct" (the specified) context is set. That is, in the Session #1$env:APPSTREAM_SESSION_CONTEXT=CONTEXT-A
and in the Session #2 $env:APPSTREAM_SESSION_CONTEXT=CONTEXT-B
.
My setup
I connect to the AppStream instance using a Browser.
Start script
Setup:
With start script I mean the executable specified in SessionStart of the config.json file:
{
"SessionStart": {
"executables": [
{
"context": "user",
"filename": "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
"arguments": "-NonInteractive -File C:\\Scripts\\start_script.ps1",
"s3LogEnabled": true
}
],
"waitingTime": 60
}
}
Start script itself (Powershell)
$StartDate = (Get-Date).ToUniversalTime()
$AppStreamImgArn = $env:AppStream_Image_Arn
$SessionID = (Get-Item Env:AppStream_Session_ID).Value
$AS2UserName = (Get-Item Env:AppStream_UserName).Value
$AS2StackName = (Get-Item Env:AppStream_Stack_Name).Value
$AS2FleetName = (Get-Item Env:AppStream_Resource_Name).Value
Write-Output "User $AS2UserName starting session $SessionID in $AS2StackName/$AS2FleetName at $((Get-Date).ToUniversalTime())"
Write-Output "ImageArn: $AppStreamImgArn"
Write-Output "CONTEXT=$($env:APPSTREAM_SESSION_CONTEXT)"
Stack
MyStack:
Type: AWS::AppStream::Stack
Properties:
Name: my-stack
StorageConnectors:
- ConnectorType: 'HOMEFOLDERS'
ResourceIdentifier: 'MyIdentifier'
ApplicationSettings:
Enabled: true
SettingsGroup: AStack
EmbedHostDomains:
- !Ref EmbedHostDomain
RedirectURL: !Ref RedirectURL
Fleet
MyFleet:
Type: AWS::AppStream::Fleet
Properties:
Name: my-fleet
DisplayName: My Fleet
InstanceType: stream.graphics-design.large
FleetType: ON_DEMAND
StreamView: APP
ComputeCapacity:
DesiredInstances: 1
VpcConfig:
SubnetIds: !ImportValue
'Fn::Sub': '${NetworkStackName}-PrivateSubnet1'
SecurityGroupIds:
- !ImportValue
'Fn::Sub': '${NetworkStackName}-NoIngressSecurityGroup'
MaxUserDurationInSeconds: 345600
DisconnectTimeoutInSeconds: 7200
IdleDisconnectTimeoutInSeconds: 0 # Disabled
EnableDefaultInternetAccess: False
IamRoleArn: !GetAtt AppStreamFleetRole.Arn
ImageArn: arn:aws:appstream:eu-west-1::image/AppStream-Graphics-Design-WinServer2019-03-24-2024
Thanks for a quick answer with good links!