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.
import { AlbFargateServices } from 'serverless-container-constructs';
new AlbFargateServices(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
},
});
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
new AlbFargateServices(stack, 'Service', {
tasks: [
{
listenerPort: 8080,
task: customerTask,
desiredCount: 2,
capacityProviderStrategy: [
{
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.
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.
new AlbFargateServices(stack, 'Service', {
tasks: [
// this task is internal only
{ listenerPort: 8080, task: task1, accessibility: LoadBalancerAccessibility.INTERNAL_ONLY},
// this task is external only
{ listenerPort: 8081, task: task2, accessibility: LoadBalancerAccessibility.EXTERNAL_ONLY},
// this task is both external and internal facing(default behavior)
{ listenerPort: 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:
new AlbFargateServices(stack, 'Service', {
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
onePerAz: 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:
new AlbFargateServices(stack, 'Service', {
vpcSubnets: {
subnets: [
ec2.Subnet.fromSubnetId(stack, 'sub-1a', 'subnet-0e9460dbcfc4cf6ee'),
ec2.Subnet.fromSubnetId(stack, 'sub-1b', 'subnet-0562f666bdf5c29af'),
ec2.Subnet.fromSubnetId(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.
Metadata
Release files for serverless-container-constructs 0.1.21
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| serverless-container-constructs-0.1.21.tar.gz | 57.6 kB | Without provenance | Details for serverless-container-constructs-0.1.21.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| serverless_container_constructs-0.1.21-py3-none-any.whl | Python 3 | none | any | Details for serverless_container_constructs-0.1.21-py3-none-any.whl |
Total release size: 114.0 kB
Release files / serverless-container-constructs-0.1.21.tar.gz
| Download URL | serverless-container-constructs-0.1.21.tar.gz |
|---|---|
| Size | 57.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9575e2c6f58e021f853a5abe61b5007e09593b2cc0dece2fa6bdd349d8e74d10
|
|
BLAKE2b-256 checksum How to use checksums |
3985cc1c4ddafdf6ee71e6d83d6f937040b9fb1d7c68b682ca54f8c0b7f741e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Release files / serverless_container_constructs-0.1.21-py3-none-any.whl
| Download URL | serverless_container_constructs-0.1.21-py3-none-any.whl |
|---|---|
| Size | 56.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
943b2fce1605c8e6edbd2b8ea10db76736e9f35b346b5b6ff9654f70bf94b4cd
|
|
BLAKE2b-256 checksum How to use checksums |
4d28a4e6fd005b6af9f571fe45f7da332d3f79ac142460612d6a7dd7ef83a803
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Security for serverless-container-constructs 0.1.21
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.