Skip to content

Commit

Permalink
unified stack, custom step, container works again
Browse files Browse the repository at this point in the history
  • Loading branch information
okigan committed Nov 23, 2023
1 parent 4d13fe1 commit 05bc7ab
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv/
.aws-sam/
.aws-sam/
__pycache__/
7 changes: 4 additions & 3 deletions samconfig.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
version = 0.1
[dev.deploy.parameters]
stack_name = "anyscale5-dev"
stack_name = "anyscale8-dev"
resolve_s3 = true
s3_prefix = "sam-app"
region = "us-east-1"
capabilities = "CAPABILITY_IAM"
rollback_on_failure = false
parameter_overrides = [
"paramDomainName=lambda-or-alb5.cloud.okulist.net",
"paramHostedZoneId=Z00976901X2BWQNUQ03E3"
"paramDomainName=lambda-or-anyscale8.cloud.okulist.net",
"paramHostedZoneId=Z00976901X2BWQNUQ03E3",
"paramWithFargate=yes",
]
55 changes: 41 additions & 14 deletions scale/template-fargate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ AWSTemplateFormatVersion: '2010-09-09'
Description: AWS Fargate infrastructure with VPC, subnets, and autoscaling for FastAPI application

Parameters:
ContainerImageUri:
Type: String
Description: The URI of the container image for the Fargate task
paramDomainName:
Type: String
Description: "The domain name for the ACM certificate (e.g., your-custom-domain-name.example.com)"

paramHostedZoneId:
Type: String
Description: "The Route 53 hosted zone ID for the domain name (e.g., Z1ABCDEF123456)"

ContainerImageUri:
Type: String
Description: The URI of the container image for the Fargate task

Resources:
# Network resources
Expand Down Expand Up @@ -188,11 +195,18 @@ Resources:
ExecutionRoleArn: !GetAtt ECSExecutionRole.Arn
TaskRoleArn: !GetAtt ECSTaskRole.Arn
ContainerDefinitions:
- Name: 'fastapi-container'
- Name: 'fastapic1'
Image: !Ref ContainerImageUri
PortMappings:
- ContainerPort: 80

- ContainerPort: 8080
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: /ecs/MyLogGroup
awslogs-region: us-east-1 # adjust the region if needed
awslogs-stream-prefix: MyContainer


# Security group for the Fargate tasks and ALB
FargateSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Expand All @@ -205,7 +219,7 @@ Resources:
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 44
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0

Expand All @@ -226,10 +240,10 @@ Resources:
SecurityGroups:
- !Ref FargateSecurityGroup
LoadBalancers:
- ContainerName: 'fastapi-container'
ContainerPort: 80
- ContainerName: 'fastapic1'
ContainerPort: 8080
# LoadBalancerName: fastapi-alb
TargetGroupArn: !GetAtt ALBTargetGroup.TargetGroupArn
TargetGroupArn: !GetAtt ALBTargetGroup2.TargetGroupArn
DependsOn:
- ALB
- ALBListener
Expand Down Expand Up @@ -266,17 +280,30 @@ Resources:
Properties:
DefaultActions:
- Type: 'forward'
TargetGroupArn: !GetAtt ALBTargetGroup.TargetGroupArn
TargetGroupArn: !GetAtt ALBTargetGroup2.TargetGroupArn
LoadBalancerArn: !Ref ALB
Port: '80'
Protocol: 'HTTP'

ALBTargetGroup:
ALBTargetGroup2:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
Port: 80
Port: 8080
Protocol: 'HTTP'
VpcId: !Ref MyVPC
Matcher:
HttpCode: '200-499'
TargetType: ip # Specify the target type as 'ip'
TargetType: ip

MyRoute53RecordSet:
Type: AWS::Route53::RecordSet
Properties:
Name: !Ref paramDomainName
HostedZoneId: !Ref paramHostedZoneId
Type: A
SetIdentifier: rd_02
Weight: 200
AliasTarget:
DNSName: !GetAtt ALB.DNSName
HostedZoneId: !GetAtt ALB.CanonicalHostedZoneID
EvaluateTargetHealth: false
10 changes: 6 additions & 4 deletions scale/xxxx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

# Get ImageUri from Lambda function with the following name
REGION="us-east-1"
LAMBDA_FUNCTION_NAME="anyscale-dev-MyLambdaFunction-yrsZdUsDY93I"
LAMBDA_FUNCTION_NAME="anyscale6-dev-MyLambdaFunction-GtxUxkwEQDd9"

response=$(aws lambda get-function --region ${REGION} --function-name $LAMBDA_FUNCTION_NAME)
IMAGE_URI=$(echo $response | jq -r '.Code.ImageUri')
echo "Image URI: $IMAGE_URI"

STACK_NAME="sss2"
STACK_NAME="sss9"
TEMPLATE_FILE="template-fargate.yml"

aws cloudformation update-stack \
aws cloudformation create-stack \
--template-body file://$TEMPLATE_FILE \
--stack-name $STACK_NAME \
--region $REGION \
--capabilities CAPABILITY_IAM \
--parameters ParameterKey=ContainerImageUri,ParameterValue=$IMAGE_URI \
--parameters "ParameterKey=ContainerImageUri,ParameterValue=$IMAGE_URI" \
"ParameterKey=paramDomainName,ParameterValue=lambda-or-alb6.cloud.okulist.net" \
"ParameterKey=paramHostedZoneId,ParameterValue=Z00976901X2BWQNUQ03E3" \
--no-cli-pager \
--disable-rollback

Expand Down
5 changes: 2 additions & 3 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ RUN python -m pip install -r requirements.txt
COPY app.py .
COPY entrypoint.sh .

# Inform Docker that the container is listening on port 80
EXPOSE 80
EXPOSE 8080

# Set the CMD to your handler location
ENTRYPOINT ["/var/task/entrypoint.sh"]
# CMD [ "app.handler" ]
17 changes: 13 additions & 4 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import logging
import os

from fastapi import FastAPI
from mangum import Mangum

logger = logging.getLogger()
logger.setLevel(logging.INFO)

logger.info('Loading function')
logger.info('Loading function........')


app = FastAPI()

handler = Mangum(app)


@app.get("/asf")
def read_root():
return {"Hello": "World"}

handler = Mangum(app)

logger.info('Loading function complete')

if __name__ == "__main__":
logger.info('processing __main__ condition')
# if 'LAMBDA_TASK_ROOT' not in os.environ:
logger.info('LAMBDA_TASK_ROOT in os.environ')
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)

logger.info('Loading function complete')
13 changes: 7 additions & 6 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/sh

# set

# Check if we are running in the AWS Lambda environment according to whether FUNCTION_TASK_ROOT is set.
if [ -n "$FUNCTION_TASK_ROOT" ]; then
# Start the Lambda function handler
exec /var/lang/bin/python -m awslambdaric $1
if [ -n "$AWS_LAMBDA_FUNCTION_MEMORY_SIZE" ]; then
echo "Running as a Lambda function"
/var/runtime/bootstrap app.handler
else
# Start the FastAPI application using Uvicorn for Fargate
cd /var/task
exec uvicorn app:app --host 0.0.0.0 --port 80
echo "Running as a Fargate container"
exec uvicorn app:app --host "0.0.0.0" --port 8080 --log-level debug
fi
Loading

0 comments on commit 05bc7ab

Please sign in to comment.