Integration between Spring Boot and AWS Elastic Container Service (Fargate)
Build Image
docker build -t digimonapi:latest "."
Run Container using image created
docker run -d -p 80:80 -t digimonapi:latest
Use file template.yml to create that resources:
- Cluster
- TaskDefinition
- Service
- Security Group
- Load Balancer
- CloudWatch
Service:
Type: "AWS::ECS::Service"
Properties:
ServiceName: !Sub service-${FeatureName}-${MicroServiceName}
<...>
CapacityProviderStrategy:
- CapacityProvider: FARGATE_SPOT
Weight: 5
- CapacityProvider: FARGATE
Weight: 1
<...>
Config AutoScaling to scale up and down the service. Use template.yml to create that resources:
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn: Service
Properties:
MinCapacity: 2
MaxCapacity: 6
ResourceId: !Join
- "/"
- - service
- !Ref EcsClusterName
- !GetAtt Service.Name
RoleARN: !GetAtt AutoScalingRole.Arn
ScalableDimension: ecs:service:DesiredCount
ServiceNamespace: ecs
ScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: !Sub "${FeatureName}-auto-scaling-policy"
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref ScalableTarget
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
TargetValue: 30.0
It's configured to scale up and down the service when the CPU usage is greater than 30%.
Config Scheduled Scaling to scale up and down the service. Use template.yml to create that resources:
ScalableTarget:
<...>
ScheduledActions:
- ScalableTargetAction:
MinCapacity: 0
MaxCapacity: 0
Schedule: 'cron(0 0 22 ? * MON-FRI)'
ScheduledActionName: scale-down
- ScalableTargetAction:
MinCapacity: 2
MaxCapacity: 6
Schedule: 'cron(0 0 11 ? * MON-FRI)'
ScheduledActionName: scale-up
These are my articles about ECS Fargate using Spot instance and AutoScaling with ECS.
- Utilizando AWS Fargate Spot para economizar até 70% em aplicações ECS
- Estratégias utilizando Application Auto Scaling com ECS
- Add ECR permissions to codeBuild.
- Error - "You have reached your pull rate limit"
Developed by Jean Jacques Barros