Package: cdk-fargate-fastautoscaler
aws-fargate-fast-autoscalerAWS Fargate Fast Autosaler- A Serverless Implementation that Triggers your AWS Fargate autoscaling in seconds withcdk-fargate-fastautoscaler.cdk-fargate-fastautoscalercdk-fargate-fastautoscaleris aaws/jsiiconstruct library for AWS CDK.By building your AWS CDK stacks withcdk-fargate-fastautoscaler, you can create your customized Fargate workload with the fast autoscaling capabilities.How it worksBehind the scene, our workload in PHP, NodeJS, Java or Python is running with a nginx reverse proxy within a single AWS Fargate Task exposing a/nginx_statusendpoint for realtime connections info generation. All traffic coming through ALB to Fargate tasks will establish active connecitons with the nginx reverse proxy before it can hit our backend server.We are running an AWS Step Function state machine to periodically invoke the AWS Lambda function and collect active connection numbers from each Fargate Taskevery 3 secondsand determine our scaling policy in the state machine followed by immediateecs service updateto increase the desired number of Fargate tasks.AWS CDK SampleThe following CDK sample creates a PHP service in AWS Fargate with the nginx as the reverse proxy.import*ascdkfrom'@aws-cdk/core'import*asec2from'@aws-cdk/aws-ec2';import{AwsLogDriver,ContainerImage}from'@aws-cdk/aws-ecs';import{FargateFastAutoscaler}from'cdk-fargate-fastautoscaler';import*aspathfrom'path';constapp=newcdk.App()constenv={region:process.env.CDK_DEFAULT_REGION,account:process.env.CDK_DEFAULT_ACCOUNT,};conststack=newcdk.Stack(app,'FargateFastAutoscalerDemo',{env})constvpc=ec2.Vpc.fromLookup(stack,'Vpc',{isDefault:true})newFargateFastAutoscaler(stack,'FargateFastAutoscaler',{vpc,//createthebackendPHPservicebackendContainer:{image:ContainerImage.fromAsset(path.join(__dirname,'../../sample/backend/php')),},//PHPservicerunningoncontainerport2015backendContainerPortMapping:[{containerPort:2015}],})On deployment complete, you'll see the URL in the Outputs:fargate-fast-autoscaling.URL=http://farga-exter-1GW64WGQYNE4O-1567742142.us-west-2.elb.amazonaws.comOpen this URL and you will see the Caddy web server welcome page with phpinfo.And if you append/nginx_statusin the URL and reload the page, you'll see this page:Start your state machineGo to Step Function console and clickstart executionon the state machines. Leave the execution name and input column as is and clickstart executionagain. Your state machine will be running. Behind the scene the step function will invoke a Lambda function to collectActive Connectionsnumber from nginx reverse proxy on each fargate task and determine a new desired number of fargate tasks to scale. Typically it would just takeless than 10 secondsbefore it starts to scale.SNS Service IntegrationTheSNSScaleOuttask in the state machine leverages direct Amazon SNS service integration to publish a notification to your SNS topic. You will receive SNS notification when it startsServiceScaleOuttask.Specify thesnsTopicproperty to define your custom SNS topic. If not defined, the construct will create a default SNS topic.Disable Scale InBy default,disableScaleInis set to true to prevent your workload from scale-in. If you prefer to enable scale in, setdisableScaleIntofalse.License SummaryThis sample code is made available under the MIT-0 license. See the LICENSE file.