Package: low-cost-ecs
Low-Cost ECSA CDK construct that provides an easy andlow-costECS on EC2 server setup without a load balancer.WhyECS may often seem expensive when used for personal development purposes, due to the cost of the load balancer.
The application load balancer is a great service that is easy to set up managed ACM certificates, easy scaling, and has dynamic port mappings..., but it is over-featured for running 1 ECS task.However, to run an ECS server without a load balancer, you need to associate an Elastic IP to the host instance and install your certificate to your service every time you start up the server.
This construct aims to automate these works and make it easy to deploy resources to run a low-cost ECS server.Try it out!The easiest way to try the construct is to clone this repository and deploy the sample Nginx server.
Edit settings inexamples/minimum.tsand deploy the cdk construct.Public hosted zoneis required.Clone and install packagesgit clone https://github.com/rajyan/low-cost-ecs.git
yarn installEdit email and domain in example.tshttps://github.com/rajyan/low-cost-ecs/blob/3d1bbf7ef4b59d0f4e9d3cd9cb90584977b71c0a/examples/minimum.ts#L1-L15Deploy!cdk deployAccess the configuredhostedZoneDomainand see that the sample Nginx server has been deployed.InstallationTo use this construct in your cdk stack as a library,npm install low-cost-ecsimport{Stack,StackProps}from'aws-cdk-lib';import{Construct}from'constructs';import{LowCostECS}from'low-cost-ecs';classSampleStackextendsStack{constructor(scope:Construct,id:string,props?:StackProps){super(scope,id,props);constvpc={/**YourVPC*/};constsecurityGroup={/**Yoursecuritygroup*/};constserverTaskDefinition={/**Yourtaskdefinition*/};newLowCostECS(this,'LowCostECS',{hostedZoneDomain:"example.com",email:"test@example.com",vpc:vpc,securityGroup:securityGroup,serverTaskDefinition:serverTaskDefinition});}}The required fields arehostedZoneDomainandemail.
You can configure your server task definition and other props. ReadLowCostECSPropsdocumentationfor details.OverviewResources generated in this stackRoute53 A recordForwarding to host instance Elastic IPCertificate State MachineInstall and renew certificates to EFS usingcertbot-dns-route53Scheduled automated renewal every 60 daysEmail notification on certbot task failureECS on EC2 host instanceECS-optimized Amazon Linux 2 AMI instance auto-scaling groupAutomatically associated with Elastic IP on instance initializationECS ServiceTLS/SSL certificate installation before default container startupCertificate EFS mounted on default container as/etc/letsencryptOthersVPC with only public subnets (no NAT Gateways to decrease cost)Security groups with minimum inboundsIAM roles with minimum privilegesCostAll resources except Route53 HostedZone should be included inAWS Free Tierif you are in the 12 Months Free period.
After your 12 Months Free period, settinghostInstanceSpotPriceto use spot instances is recommended.EC2t2.micro 750 instance hours (12 Months Free Tier)30GB EBS volume (12 Months Free Tier)ECSNo additional charge because using ECS on EC2EFSUsage is very small, it should be freeCloud WatchUsage is very small, and it should be included in the free tierEnablingcontainerInsightswill charge for custom metricsDebuggingSSM Session ManagerSSM manager is pre-installed in the host instance (by ECS-optimized Amazon Linux 2 AMI) andAmazonSSMManagedInstanceCoreis added to the host instance role to access and debug in your host instance.aws ssm start-session --target $INSTANCE_IDECS ExecService ECS Exec is enabled, so execute commands can be used to debug your server task container.aws ecs execute-command \
--cluster $CLUSTER_ID \
--task $TASK_ID \
--container nginx \
--command bash \
--interactiveLimitationsBecause the ECS service occupies a host port, only one task can be executed at a time.
The old task must be terminated before the new task launches, and this causes downtime on release.Also, if you make changes that require recreating the service, you may need to manually terminate the task of the old service.