CDK patterns for serverless container with AWS Fargate
Inspired by Vijay Menon from the AWS blog post introduced in 2019, DualAlbFargateService
allows you to create one or many fargate services with both internet-facing ALB and internal ALB associated with all services. With this pattern, fargate services will be allowed to intercommunicat via internal ALB while external inbound traffic will be spread across the same service tasks through internet-facing ALB.
The sample below will create 3 fargate services associated with both external and internal ALBs. The internal ALB will have an alias(internal.svc.local
) auto-configured from Route 53 so services can communite through the private ALB endpoint.
new DualAlbFargateService(stack, 'Service', {
spot: true, // FARGATE_SPOT only cluster
tasks: [
{
listenerPort: 80,
task: orderTask,
desiredCount: 2,
// customize the service autoscaling policy
scalingPolicy: {
maxCapacity: 20,
requestPerTarget: 1000,
targetCpuUtilization: 50,
},
},
{ listenerPort: 8080, task: customerTask, desiredCount: 2 },
{ listenerPort: 9090, task: productTask, desiredCount: 2 },
],
route53Ops: {
zoneName, // svc.local
externalAlbRecordName, // external.svc.local
internalAlbRecordName, // internal.svc.local
},
});
By enabling the spot
property, 100% fargate spot tasks will be provisioned to help you save up to 70%. Check more details about Fargate Spot. This is a handy catch-all flag to force all tasks to be FARGATE_SPOT
only.
To specify mixed strategy with partial FARGATE
and partial FARGATE_SPOT
, specify the capacityProviderStrategy
for individual tasks like
new DualAlbFargateService(stack, 'Service', {
tasks: [
{
listenerPort: 8080,
task: customerTask,
desiredCount: 2,
capacityProviderStretegy: [
{
capacityProvider: 'FARGATE',
base: 1,
weight: 1,
},
{
capacityProvider: 'FARGATE_SPOT',
base: 0,
weight: 3,
},
],
},
],
});
The custom capacity provider strategy will be applied if capacityProviderStretegy
is specified, otherwise, 100% spot will be used when spot: true
. The default policy is 100% Fargate on-demand.
This repository comes with a sample applicaiton with 3 services in Golang. On deployment, the Order
service will be exposed externally on external ALB port 80
and all requests to the Order
service will trigger sub-requests internally to another other two services(product
and customer
) through the internal ALB and eventually aggregate the response back to the client.
To deploy the sample application in you default VPC:
// compile the ts to js
$ yarn build
$ npx cdk --app lib/integ.default.js -c use_default_vpc=1 diff
$ npx cdk --app lib/integ.default.js -c use_default_vpc=1 deploy
On deployment complete, you will see the external ALB endpoint in the CDK output. cURL
the external HTTP endpoint and you should be able to see the aggregated response.
$ curl http://demo-Servi-EH1OINYDWDU9-1397122594.ap-northeast-1.elb.amazonaws.com
{"service":"order", "version":"1.0"}
{"service":"product","version":"1.0"}
{"service":"customer","version":"1.0"}