CDK patterns for serverless container with AWS Fargate
Project description
cdk-fargate-patterns
CDK patterns for serverless container with AWS Fargate
DualAlbFargateService
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.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
DualAlbFargateService(stack, "Service",
spot=True, # FARGATE_SPOT only cluster
tasks=[{
"task": order_task,
"desired_count": 2,
"internal": {"port": 443, "cert": cert},
"external": {"port": 80},
# customize the service autoscaling policy
"scaling_policy": {
"max_capacity": 20,
"request_per_target": 1000,
"target_cpu_utilization": 50
}
}, {"task": customer_task, "desired_count": 2, "internal": {"port": 8080}}, {"task": product_task, "desired_count": 2, "internal": {"port": 9090}}
],
route53_ops={
"zone_name": "svc.local",
"external_alb_record_name": "external",
"internal_alb_record_name": "internal"
}
)
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
DualAlbFargateService(stack, "Service",
tasks=[{
"task": customer_task,
"internal": {"port": 8080},
"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, External or Both
Specify the internal or external property to expose your service internally, externally or both.
The cert property implies HTTPS protocol.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
DualAlbFargateService(stack, "Service",
tasks=[{"task": task1, "internal": {"port": 8080}}, {"task": task2, "external": {"port": 8081}}, {
"task": task3,
"external": {"port": 443, "cert": my_acm_cert},
"internal": {"port": 8888}
}
]
)
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
DualAlbFargateService(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
DualAlbFargateService(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
// 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
To deploy with HTTPS-only with existing ACM certificate in your default VPC:
$ npx cdk --app lib/integ.default.js deploy -c use_default_vpc=1 -c ACM_CERT_ARN=<YOUR_ACM_CERT_ARN>
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
or
$ curl https://<YOUR_CUSTOM_DOMAIN_NAME>
{"service":"order", "version":"1.0"}
{"service":"product","version":"1.0"}
{"service":"customer","version":"1.0"}
WordPress
Use the WordPress construct to create a serverless WordPress service with AWS Fargate, Amazon EFS filesystem and Aurora serverless database. All credentials auto generated from the AWS Secret Manager and securely inject the credentials into the serverless container with the auto generated IAM task execution role.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
WordPress(stack, "WP",
aurora_serverless=True,
spot=True,
enable_execute_command=True
)
Laravel
The Laravl construct is provided as a high-level abstraction that allows you to create a modern Laravel environment running on AWS Fargate and Amazon Aurora Serverless. Here comes two variants as the reference:
laravel-bitnami - based on bitnami/laravel with artisan running as the built-in web server.
laravel-nginx-php-fpm - laravel with nginx and php-fpm maintained by Ernest Chiang.
Simply point code to your local Laravel codebase and it takes care everything else for you.
Samples
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
#
# laravel-bitnami
#
Laravel(stack, "LaravelBitnamiDemo",
aurora_serverless=True,
spot=True,
enable_execute_command=True,
code=path.join(__dirname, "../services/laravel-bitnami"),
container_port=3000,
loadbalancer={"port": 80}
)
#
# laravel-nginx-php-fpm
#
Laravel(stack, "LaravelNginxDemo",
aurora_serverless=True,
spot=True,
enable_execute_command=True,
code=path.join(__dirname, "../services/laravel-nginx-php-fpm"),
loadbalancer={"port": 80}
)
See integ.laravel.ts for the full code sample.
Local development and testing
The docker-compose.yml is provided with all sample services in the repository. To bring up all services locally, run:
$ cd services
$ docker compose up
Use cURL to test locally:
curl http://localhost
Response:
{"service":"order","version":"1.0"}
{"service":"product","version":"1.0"}
{"service":"customer","version":"1.0"}
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 cdk-fargate-patterns-0.2.12.tar.gz.
File metadata
- Download URL: cdk-fargate-patterns-0.2.12.tar.gz
- Upload date:
- Size: 238.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6875dda3a9f2acab920b8c1078810a0789a14df1f218d90edf2b00b3d9bc39a4
|
|
| MD5 |
b846cd399e9083c61886bbc1f250fe46
|
|
| BLAKE2b-256 |
3fe1e513474646e74dfe9e2e64082bc7d75dccdc4075fe91ce95ab76d7db5d0c
|
File details
Details for the file cdk_fargate_patterns-0.2.12-py3-none-any.whl.
File metadata
- Download URL: cdk_fargate_patterns-0.2.12-py3-none-any.whl
- Upload date:
- Size: 236.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30766450b6afce80a1f3743f4d49ca95112f0c8928d71eda15eddbfe76bf9273
|
|
| MD5 |
68ce8f898f596382ab82ad52023668ee
|
|
| BLAKE2b-256 |
59752dcf3d8062a85a0769b0b9a75c20318628c8306f1729f977d1933b45287c
|