Skip to content

Commit 778a716

Browse files
authored
Merge pull request #351 from aws-samples/350-improvement-serverless-test-workshop-ui-configuration-improvement
350 improvement serverless test workshop UI configuration improvement
2 parents a764910 + 8a534fd commit 778a716

File tree

2 files changed

+9
-296
lines changed

2 files changed

+9
-296
lines changed

workshops/serverless-testing-workshop/demo-app/urs-ui.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
import uuid
1313
import time
1414
import requests
15+
import boto3
1516
import streamlit as st
1617
from streamlit_js_eval import streamlit_js_eval
1718

1819
# Initialize Contexts
1920
if 'api_endpoint_url' not in st.session_state:
20-
if os.path.isfile("config.json"):
21-
with open("config.json","r",encoding="utf-8") as f:
22-
app_config = json.load(f)
23-
st.session_state['api_endpoint_url'] = app_config["api_endpoint"].strip()
24-
else:
21+
try:
22+
cfn_client = boto3.client('cloudformation')
23+
response = cfn_client.describe_stacks(StackName=os.environ.get('BACKEND_STACK_NAME','urs-backend'))
24+
for output in response['Stacks'][0]['Outputs']:
25+
if output['OutputKey'] == 'ApiEndpoint':
26+
st.session_state['api_endpoint_url'] = output['OutputValue']
27+
except:
2528
st.session_state['api_endpoint_url'] = "https://{APIGATEWAYID}.execute-api.{REGION}.amazonaws.com/Prod/"
2629

2730
if 'unicorn_art' not in st.session_state:
@@ -215,7 +218,7 @@ def update_unicorn_reserve_list():
215218
key="api_endpoint_url",
216219
on_change=update_api_endpoint
217220
)
218-
221+
219222
# File picker for uploading to the unicorn inventory
220223
uploaded_file = st.file_uploader("Choose a CSV file for the Unicorn Inventory.", type=["csv"])
221224
if uploaded_file is not None:

workshops/serverless-testing-workshop/template.yaml

Lines changed: 0 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -242,293 +242,6 @@ Resources:
242242
name:
243243
- !Ref UnicornInventoryBucket
244244

245-
##################################################
246-
##################################################
247-
##### demo-app (begin)
248-
##################################################
249-
##################################################
250-
251-
#
252-
# Networking
253-
# VPC, 2 Public Subnets, S3/ECR/Cloudwatch Service Endpoints, Internet Gateway
254-
#
255-
256-
StreamlitVPC:
257-
Type: AWS::EC2::VPC
258-
Properties:
259-
CidrBlock: 192.168.0.0/24
260-
EnableDnsHostnames : true
261-
EnableDnsSupport : true
262-
263-
StreamlitSubnet1:
264-
Type: AWS::EC2::Subnet
265-
Properties:
266-
VpcId: !Ref StreamlitVPC
267-
CidrBlock: 192.168.0.0/25
268-
AvailabilityZone: !Sub "${AWS::Region}b"
269-
270-
StreamlitSubnet2:
271-
Type: AWS::EC2::Subnet
272-
Properties:
273-
VpcId: !Ref StreamlitVPC
274-
CidrBlock: 192.168.0.128/25
275-
AvailabilityZone: !Sub "${AWS::Region}c"
276-
277-
StreamlitInternetGateway:
278-
Type: AWS::EC2::InternetGateway
279-
280-
StreamlitGatewayAttachment:
281-
Type: AWS::EC2::VPCGatewayAttachment
282-
Properties:
283-
InternetGatewayId: !Ref StreamlitInternetGateway
284-
VpcId: !Ref StreamlitVPC
285-
286-
StreamlitSecurityGroup:
287-
Type: AWS::EC2::SecurityGroup
288-
Properties:
289-
GroupDescription: "Streamlit UI Security Group"
290-
GroupName: !Sub "${AWS::StackName}-ds-sg"
291-
SecurityGroupEgress:
292-
- IpProtocol: tcp
293-
FromPort: 0
294-
ToPort: 65535
295-
CidrIp: 0.0.0.0/0
296-
Description: Allow outbound access
297-
SecurityGroupIngress:
298-
- IpProtocol: tcp
299-
FromPort: !Ref iECRStreamlitPort
300-
ToPort: !Ref iECRStreamlitPort
301-
CidrIp: 0.0.0.0/0
302-
Description: Inbound only on Streamlit port
303-
VpcId: !Ref StreamlitVPC
304-
305-
EndpointSecurityGroup:
306-
Type: AWS::EC2::SecurityGroup
307-
Properties:
308-
GroupDescription: "Streamlit UI Endpoint Security Group"
309-
GroupName: !Sub "${AWS::StackName}-ep-sg"
310-
SecurityGroupEgress:
311-
- IpProtocol: tcp
312-
FromPort: 0
313-
ToPort: 65535
314-
CidrIp: 0.0.0.0/0
315-
Description: Allow outbound access
316-
SecurityGroupIngress:
317-
- IpProtocol: tcp
318-
FromPort: 0
319-
ToPort: 65535
320-
SourceSecurityGroupId: !Ref StreamlitSecurityGroup
321-
Description: Allow inbound from Streamlit sg only
322-
VpcId: !Ref StreamlitVPC
323-
324-
StreamlitRouteTable:
325-
Type: 'AWS::EC2::RouteTable'
326-
Properties:
327-
VpcId: !Ref StreamlitVPC
328-
329-
InternetGatewayRoute:
330-
Type: AWS::EC2::Route
331-
Properties:
332-
GatewayId: !Ref StreamlitInternetGateway
333-
RouteTableId: !Ref StreamlitRouteTable
334-
DestinationCidrBlock: 0.0.0.0/0
335-
336-
SubnetRouteTableAssociation1:
337-
Type: 'AWS::EC2::SubnetRouteTableAssociation'
338-
Properties:
339-
SubnetId: !Ref StreamlitSubnet1
340-
RouteTableId: !Ref StreamlitRouteTable
341-
342-
SubnetRouteTableAssociation2:
343-
Type: 'AWS::EC2::SubnetRouteTableAssociation'
344-
Properties:
345-
SubnetId: !Ref StreamlitSubnet2
346-
RouteTableId: !Ref StreamlitRouteTable
347-
348-
StreamlitVPCEndpointECRApi:
349-
Type: AWS::EC2::VPCEndpoint
350-
Properties:
351-
SecurityGroupIds:
352-
- !Ref EndpointSecurityGroup
353-
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.ecr.api'
354-
SubnetIds:
355-
- !Ref StreamlitSubnet1
356-
- !Ref StreamlitSubnet2
357-
VpcEndpointType: Interface
358-
VpcId: !Ref StreamlitVPC
359-
PrivateDnsEnabled: true
360-
361-
StreamlitVPCEndpointDocker:
362-
Type: AWS::EC2::VPCEndpoint
363-
Properties:
364-
SecurityGroupIds:
365-
- !Ref EndpointSecurityGroup
366-
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.ecr.dkr'
367-
SubnetIds:
368-
- !Ref StreamlitSubnet1
369-
- !Ref StreamlitSubnet2
370-
VpcEndpointType: Interface
371-
VpcId: !Ref StreamlitVPC
372-
PrivateDnsEnabled: true
373-
374-
StreamlitVPCEndpointLogs:
375-
Type: AWS::EC2::VPCEndpoint
376-
Properties:
377-
SecurityGroupIds:
378-
- !Ref EndpointSecurityGroup
379-
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.logs'
380-
SubnetIds:
381-
- !Ref StreamlitSubnet1
382-
- !Ref StreamlitSubnet2
383-
VpcEndpointType: Interface
384-
VpcId: !Ref StreamlitVPC
385-
PrivateDnsEnabled: true
386-
387-
StreamlitVPCEndpointS3:
388-
Type: AWS::EC2::VPCEndpoint
389-
Properties:
390-
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
391-
VpcEndpointType: Gateway
392-
VpcId: !Ref StreamlitVPC
393-
RouteTableIds:
394-
- !Ref StreamlitRouteTable
395-
396-
LoadBalancer:
397-
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
398-
Properties:
399-
Subnets:
400-
- !Ref StreamlitSubnet1
401-
- !Ref StreamlitSubnet2
402-
SecurityGroups:
403-
- !Ref StreamlitSecurityGroup
404-
405-
LoadBalancerListener:
406-
Type: AWS::ElasticLoadBalancingV2::Listener
407-
Properties:
408-
LoadBalancerArn: !Ref LoadBalancer
409-
Port: !Ref iECRStreamlitPort
410-
Protocol: HTTP
411-
DefaultActions:
412-
- Type: forward
413-
TargetGroupArn: !Ref TargetGroup
414-
415-
TargetGroup:
416-
Type: AWS::ElasticLoadBalancingV2::TargetGroup
417-
Properties:
418-
Name: !Sub "${AWS::StackName}-tg-http"
419-
VpcId: !Ref StreamlitVPC
420-
Port: !Ref iECRStreamlitPort
421-
Protocol: HTTP
422-
TargetType: ip
423-
HealthCheckEnabled: true
424-
HealthCheckIntervalSeconds: 60
425-
HealthCheckPath: "/_stcore/health"
426-
HealthCheckPort: !Ref iECRStreamlitPort
427-
HealthCheckProtocol: HTTP
428-
TargetGroupAttributes:
429-
- Key: stickiness.enabled
430-
Value: "true"
431-
- Key: stickiness.type
432-
Value: lb_cookie
433-
- Key: stickiness.lb_cookie.duration_seconds
434-
Value: "86500"
435-
436-
ECSTask:
437-
Type: AWS::ECS::TaskDefinition
438-
DependsOn: LoadBalancerListener
439-
Properties:
440-
RequiresCompatibilities:
441-
- FARGATE
442-
Cpu: '2048'
443-
Memory: '4096'
444-
NetworkMode: awsvpc
445-
RuntimePlatform:
446-
CpuArchitecture: "X86_64"
447-
OperatingSystemFamily: "LINUX"
448-
ExecutionRoleArn: !Ref ExecutionRole
449-
TaskRoleArn: !Ref TaskRole
450-
ContainerDefinitions:
451-
- Name: "streamlit"
452-
Image: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/docsearch-ecr"
453-
MemoryReservation: 2048
454-
Cpu: 2048
455-
Memory: 4096
456-
Essential: true
457-
PortMappings:
458-
- ContainerPort: !Ref iECRStreamlitPort
459-
LogConfiguration:
460-
LogDriver: awslogs
461-
Options:
462-
awslogs-create-group: "true"
463-
awslogs-group: !Sub "/ecs/${AWS::StackName}-ECSTask"
464-
awslogs-region: !Sub "${AWS::Region}"
465-
awslogs-stream-prefix: "ecs"
466-
467-
ECSCluster:
468-
Type: 'AWS::ECS::Cluster'
469-
Properties:
470-
ClusterName: !Sub "${AWS::StackName}-cluster"
471-
472-
ECSService:
473-
Type: 'AWS::ECS::Service'
474-
Properties:
475-
Cluster: !Ref ECSCluster
476-
TaskDefinition: !Ref ECSTask
477-
DesiredCount: 1
478-
LaunchType: FARGATE
479-
ServiceName: !Sub "${AWS::StackName}-svc"
480-
SchedulingStrategy: "REPLICA"
481-
LoadBalancers:
482-
- ContainerName: "streamlit"
483-
ContainerPort: !Ref iECRStreamlitPort
484-
TargetGroupArn: !Ref TargetGroup
485-
HealthCheckGracePeriodSeconds: 50
486-
NetworkConfiguration:
487-
AwsvpcConfiguration:
488-
AssignPublicIp: ENABLED
489-
SecurityGroups:
490-
- !Ref StreamlitSecurityGroup
491-
Subnets:
492-
- !Ref StreamlitSubnet1
493-
- !Ref StreamlitSubnet2
494-
495-
ExecutionRole:
496-
Type: AWS::IAM::Role
497-
Properties:
498-
RoleName: !Sub "${AWS::StackName}-execution-role"
499-
AssumeRolePolicyDocument:
500-
Statement:
501-
- Effect: Allow
502-
Principal:
503-
Service: ecs-tasks.amazonaws.com
504-
Action: 'sts:AssumeRole'
505-
Policies:
506-
- PolicyName: root
507-
PolicyDocument:
508-
Version: "2012-10-17"
509-
Statement:
510-
- Effect: Allow
511-
Action:
512-
- "ecr:GetAuthorizationToken"
513-
- "ecr:BatchCheckLayerAvailability"
514-
- "ecr:GetDownloadUrlForLayer"
515-
- "ecr:BatchGetImage"
516-
- "logs:CreateLogStream"
517-
- "logs:PutLogEvents"
518-
- "logs:CreateLogGroup"
519-
Resource: '*'
520-
521-
TaskRole:
522-
Type: AWS::IAM::Role
523-
Properties:
524-
RoleName: !Sub "${AWS::StackName}-task-role"
525-
AssumeRolePolicyDocument:
526-
Statement:
527-
- Effect: Allow
528-
Principal:
529-
Service: ecs-tasks.amazonaws.com
530-
Action: 'sts:AssumeRole'
531-
532245
Outputs:
533246
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
534247
# Find out more about other implicit resources you can reference within SAM
@@ -548,6 +261,3 @@ Outputs:
548261
GetFileValidatorARN:
549262
Description: "ARN of the Lambda function required in the 'OPTIONAL: Invoke a Lambda function in the cloud' section."
550263
Value: !GetAtt FileValidator.Arn
551-
oUiDnsName:
552-
Description: Host UI web link name
553-
Value: !Sub "http://${LoadBalancer.DNSName}:${iECRStreamlitPort}"

0 commit comments

Comments
 (0)