CDK patterns for modern application with serverless containers on AWS
Project description
serverless-container-constructs
CDK patterns for modern application with serverless containers on AWS
AlbFargateServices
Inspired by Vijay Menon from the AWS blog post introduced in 2019, AlbFargateServices 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.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from serverless_container_constructs import AlbFargateServices
AlbFargateServices(stack, "Service",
spot=True, # FARGATE_SPOT only cluster
tasks=[{
"listener_port": 80,
"task": order_task,
"desired_count": 2,
# customize the service autoscaling policy
"scaling_policy": {
"max_capacity": 20,
"request_per_target": 1000,
"target_cpu_utilization": 50
}
}, {"listener_port": 8080, "task": customer_task, "desired_count": 2}, {"listener_port": 9090, "task": product_task, "desired_count": 2}
],
route53_ops={
"zone_name": zone_name, # svc.local
"external_alb_record_name": external_alb_record_name, # external.svc.local
"internal_alb_record_name": internal_alb_record_name
}
)
Fargate Spot Support
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
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
AlbFargateServices(stack, "Service",
tasks=[{
"listener_port": 8080,
"task": customer_task,
"desired_count": 2,
"capacity_provider_strategy": [{
"capacity_provider": "FARGATE",
"base": 1,
"weight": 1
}, {
"capacity_provider": "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.
ECS Exec
Simply turn on the enableExecuteCommand property to enable the ECS Exec support for all services.
Internal or External Only
By default, all task(s) defined in the AlbFargateServices will be registered to both external and internal ALBs.
Set accessibility to make it internal only, external only, or both.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
AlbFargateServices(stack, "Service",
tasks=[{"listener_port": 8080, "task": task1, "accessibility": LoadBalancerAccessibility.INTERNAL_ONLY}, {"listener_port": 8081, "task": task2, "accessibility": LoadBalancerAccessibility.EXTERNAL_ONLY}, {"listener_port": 8082, "task": task3}
]
)
Please note if all tasks are defined as INTERNAL_ONLY, no external ALB will be created. Similarly, no internal ALB
will be created if all defined as EXTERNAL_ONLY.
VPC Subnets
By default, all tasks will be deployed in the private subnets. You will need the NAT gateway for the default route associated with the private subnets to ensure the task can successfully pull the container images.
However, you are allowed to specify vpcSubnets to customize the subnet selection.
To deploy all tasks in public subnets, one per AZ:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
AlbFargateServices(stack, "Service",
vpc_subnets={
"subnet_type": ec2.SubnetType.PUBLIC,
"one_per_az": True
}, ...
)
This will implicitly enable the auto assign public IP for each fargate task so the task can successfully pull the container images from external registry. However, the ingress traffic will still be balanced via the external ALB.
To deploy all tasks in specific subnets:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
AlbFargateServices(stack, "Service",
vpc_subnets={
"subnets": [
ec2.Subnet.from_subnet_id(stack, "sub-1a", "subnet-0e9460dbcfc4cf6ee"),
ec2.Subnet.from_subnet_id(stack, "sub-1b", "subnet-0562f666bdf5c29af"),
ec2.Subnet.from_subnet_id(stack, "sub-1c", "subnet-00ab15c0022872f06")
]
}, ...
)
Sample Application
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.
Deploy
To deploy the sample application in you default VPC:
// install first
yarn install
npx cdk diff -c use_default_vpc=1
npx cdk deploy -c use_default_vpc=1
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"}
cdk-nag with AwsSolutions check
This construct follows the best practices from the AWS Solutoins with cdk-nag. Enable the AWS_SOLUTIONS_CHECK context variable to check aginst the cdk-nag rules.
npx cdk diff -c AWS_SOLUTIONS_CHECK=1
or
npx cdk synth -c AWS_SOLUTIONS_CHECK=1
Security
See CONTRIBUTING for more information.
License
This library is licensed under the MIT-0 License. See the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file serverless-container-constructs-0.1.5.tar.gz.
File metadata
- Download URL: serverless-container-constructs-0.1.5.tar.gz
- Upload date:
- Size: 55.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
663b2d2ad08bb75204c687c21529a13f215c3a3d91991999e91bfb2934d91b8b
|
|
| MD5 |
480248e3cd158fdf8bf20a4b580bb757
|
|
| BLAKE2b-256 |
bbdb446c667f208172b970f41fd4ba0ce3d27da73250d95bb1a1a160e4cebc5a
|
File details
Details for the file serverless_container_constructs-0.1.5-py3-none-any.whl.
File metadata
- Download URL: serverless_container_constructs-0.1.5-py3-none-any.whl
- Upload date:
- Size: 53.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
351c607a4bf9055b5ef44b858734a8e9795bcbe4a576f881e77038f6e2951e1c
|
|
| MD5 |
eb7c0f36f9a9a702760adbf22f0a5419
|
|
| BLAKE2b-256 |
2778bf2b78a7744281ab283ffb0e0f2ff40e0b96ca133e1fb2f7d4261271cbc1
|