Skip to content

Python Container Service

This generator adds a containerised HTTP service to an existing Python project. You get a small FastAPI server, run by the uvicorn web server, with a GET /health route and a sample GET /echo route, a Dockerfile, and targets to run the server locally, build its image and scan the image. You also get CDK or Terraform code that runs the image on Amazon Elastic Container Service (Amazon ECS), the AWS service that runs and scales containers.

The service runs on AWS Fargate, which runs each container without servers for you to manage. Each running copy of your container is an ECS task. The tasks run in your own VPC (virtual private cloud, an isolated network in your AWS account). A security group (a set of firewall rules attached to the tasks) controls which network traffic may reach them and leave them.

The service is private. It has no load balancer, no API and no public IP address. Its security group has no inbound rule, so nothing can open a connection to the service until you allow a caller. The service can still make outbound calls, for example to a database or to other AWS services.

You can add a container service to a Python project in two ways:

Run this generator@aws/nx-plugin:py#container

pnpm nx g @aws/nx-plugin:py#container
Build your command5

Required

Generator Options5 options
projectRequiredstring

The project to add the container service to

infraenumDefault: ecs

The infrastructure that runs your container. ecs runs it as an Amazon ECS service on AWS Fargate in your VPC, with no load balancer and no inbound access. none generates no infrastructure.

ecsnone
iacenumDefault: inherit

The preferred IaC provider. By default this is inherited from your initial selection.

inheritcdkterraform
namestring

The name of your container service (default: container)

preferInstallDependenciesbooleanDefault: true

Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.

With infra set to none the generator vends no infrastructure, and the service runs only locally.

The generator adds the following files to your project. The source folder is named after the name option in snake_case (for example my_container for --name=my-container), or container if you leave it out:

  • Directoryyour-project/
    • Directoryyour_module/
      • Directorycontainer/ (or the name you chose)
        • __init__.py Python package initialization
        • main.py FastAPI app with GET /health and the sample route GET /echo, served by uvicorn on PORT
        • Dockerfile Container image definition
        • .dockerignore Files left out of the image build context
    • pyproject.toml Updated with FastAPI and uvicorn dependencies
    • project.json Updated with the bundle-arm and bundle targets and the <name>-serve, <name>-docker and <name>-trivy targets for your service

Target names start with the name option, or with container if you leave it out. For example, the targets are container-serve, container-docker and container-trivy without a name, and orders-serve, orders-docker and orders-trivy for --name=orders. The bundle-arm and bundle targets are shared by everything the project bundles.

infra = ecs

Since this generator vends infrastructure as code based on your chosen iac, it will create a project in packages/common which includes the relevant CDK constructs or Terraform modules.

The common infrastructure as code project is structured as follows:

  • Directorypackages/common/constructs
    • Directorysrc
      • Directoryapp/ Constructs for infrastructure specific to a project/generator
        • …
      • Directorycore/ Generic constructs which are reused by constructs in app
        • …
      • index.ts Entry point exporting constructs from app
    • project.json Project build targets and configuration

For deploying your service, the following files are generated. <name> is your service name in kebab-case: the name option, or the last part of your project name followed by -container when you leave the option out. Project orders with no name option gives orders-container, and --name=api gives api:

  • Directorypackages/common/constructs/src
    • Directoryapp
      • Directoryservices
        • <name>.ts CDK construct for your service, with its image
    • Directorycore
      • Directorycontainer
        • ecs-service.ts Generic construct for an ECS service on Fargate

The construct and the module create an ECS cluster with Container Insights (CloudWatch metrics for the cluster’s containers), a task definition, the service, a task role and an execution role, a log group encrypted with an AWS Key Management Service (AWS KMS) key, CPU-based autoscaling and the service’s security group.

infra = none

If you set infra to none, no CDK constructs or Terraform modules are generated, and the service runs only locally.

Loading the diagram…

The <name>-serve target runs the server on your machine, without a container:

Terminal window
pnpm nx run your-project:container-serve

The server listens on the port in the PORT environment variable. The <name>-serve target sets it to a port the generator assigns, starting at 8081 and skipping ports other projects in your workspace already use. Inside the container the server listens on port 8080.

The server listens on the address in the HOST environment variable, which defaults to 127.0.0.1, so the <name>-serve target accepts connections only from your own machine. The image sets HOST=0.0.0.0, so inside a task the server listens on the task’s own network interface. The task’s security group still decides who may connect.

Check that the server is up with its health route, which returns status 200 and {"status":"ok"}:

Terminal window
curl http://localhost:<port>/health

The server also has a sample route that you replace with your own routes. GET /echo?message=<x> returns {"message":"<x>"}. The message parameter is required and may be at most 1024 characters long. A request without it, or with a longer value, gets status 422.

The <name>-docker target builds a linux/arm64 image of your service. It runs the bundle-arm target first, which writes your code and its linux/arm64 dependencies to dist/<project root>/bundle-arm. The aggregate bundle target depends on bundle-arm. The <name>-docker target then copies that folder, the Dockerfile and the .dockerignore into dist/<project root>/docker/<name>, and builds the image with that folder as the build context (the files the image build can read). It uses the container engine your workspace is set up with, docker or Finch. The target runs as part of the aggregate docker target, which builds every image in the project.

Terminal window
pnpm nx run your-project:container-docker

On ECS, a health check command runs inside the container and calls GET /health on the container port. It needs no inbound access. ECS replaces a task whose health check keeps failing. ECS ignores a HEALTHCHECK instruction in the Dockerfile. For more on how images are built and published, see Docker Bundling.

The Docker image built for this project can be scanned for vulnerabilities using Trivy, running from the ECR-hosted Trivy image.

A trivy target is added to your project which scans the built image and exits non-zero if any HIGH or CRITICAL severity vulnerability is found. The generated Dockerfile uses a base image with no known fixable vulnerabilities of these severities at time of generation, and upgrades bundled tooling (such as npm) to keep it that way.

The scan uses the same container engine as your image build (docker or finch), so no additional tooling is required. The scan is not cached, since the image it reads lives in the container engine rather than on disk — so it always scans the real image, and fails loudly rather than reporting a cached pass for an image that is no longer there. Each run therefore takes tens of seconds per image and refreshes Trivy’s vulnerability database, so it needs network access. The vended trivy root script scans every image in the workspace:

Terminal window
pnpm trivy

There may be instances where you want to suppress a specific vulnerability, for example when no fix is yet available and you have assessed the risk as acceptable.

Add the vulnerability ID (one per line) to the .trivyignore file in the root of your project (i.e. next to your project.json):

.trivyignore
# node-tar arbitrary file write - not exploitable in our usage
CVE-2024-XXXXX

For more details on filtering findings, refer to the Trivy filtering documentation.

infra = ecs

The construct and the module never create a VPC. You create or import one in your stack or root module and pass it in. The tasks run in private subnets with egress, which reach AWS services through a NAT gateway (a managed gateway that lets private subnets make outbound connections) or through VPC endpoints (private connections from your VPC to AWS services).

The CDK construct is created in common/constructs. Its class name is <name> in PascalCase, so project orders with no name option gives the class OrdersContainer in packages/common/constructs/src/app/services/orders-container.ts, and --name=api gives Api in api.ts. MyService below is a placeholder for your own class, which your @<scope>/common-constructs package exports under that name. The package exports no construct called Service, so if your editor offers one it is Service from aws-cdk-lib/aws-servicediscovery.

Example usage with a new VPC:

packages/infra/src/stacks/application-stack.ts
import { MyService } from '@my-scope/common-constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
import { SubnetType, Vpc } from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
new MyService(this, 'MyService', {
vpc,
vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
});
}
}

To use an existing VPC, look it up with Vpc.fromLookup(this, 'Vpc', { vpcId: '<vpc-id>' }) and pass it as vpc.

When your tasks start, ECS pulls the image and sends logs on their behalf. Your server code may also call AWS services. In private subnets with no NAT gateway, each of these calls needs a VPC endpoint. Without the right endpoints, tasks fail to start or time out when they call AWS.

Create these endpoints in the VPC, replacing <region> with your AWS Region:

EndpointTypeWhat uses it
com.amazonaws.<region>.ecr.apiInterfaceECS, to authenticate with Amazon ECR and look up the image
com.amazonaws.<region>.ecr.dkrInterfaceECS, to pull the image from Amazon ECR
com.amazonaws.<region>.s3GatewayECS, to download image layers, which Amazon ECR stores in Amazon S3
com.amazonaws.<region>.logsInterfaceThe container’s log output, sent to Amazon CloudWatch Logs
com.amazonaws.<region>.secretsmanagerInterfaceECS, to read the secrets you pass to the container when a task starts
com.amazonaws.<region>.appconfigdataInterfaceYour code, to read runtime configuration from AWS AppConfig
com.amazonaws.<region>.stsInterfaceYour code, when it requests temporary credentials from AWS STS (for example to assume another role)

Interface endpoints need a security group that allows HTTPS (port 443) from the tasks. For details on the Amazon ECR endpoints, see Amazon ECR interface VPC endpoints.

The generated infrastructure applies these settings by default:

  • The service’s security group has no inbound rule. No caller inside or outside the VPC can connect to the tasks until you add a rule.
  • Tasks run in private subnets and never get a public IP address.
  • The generator creates no load balancer and no API in front of the service.
  • Tasks may send outbound traffic only on port 443. Connections to databases add the database port.
  • The task role (the role your server code runs as) may only read runtime configuration from AWS AppConfig until you or a connection grant more.
  • The execution role (the role ECS uses to start your task) may only pull the service’s image, write to its log group and read the secrets you list.
  • Secrets reach the container only as references to AWS Secrets Manager or AWS Systems Manager Parameter Store, never as plain environment values.
  • The container has a read-only root file system with a writable /tmp folder, and runs as uid 1000, a non-root user. ECS Exec (shell access into a running task) is off.
  • A deployment that fails its health checks stops and rolls back to the previous version.
  • The log group is encrypted with an AWS KMS key, and logs are kept for one year.

This section is about callers inside the VPC. An HTTP client outside the VPC needs an API in front of the service, as the note at the top of this guide explains.

Nothing can call the service as generated. If another workload in the VPC needs to call it, for example a Lambda function or another ECS service, you add an inbound rule to the service’s security group yourself. The generator adds no inbound rule. Allow one named caller by its security group, on the container port, and never allow an address range.

The construct implements IConnectable, so allow the caller’s security group through its connections:

packages/infra/src/stacks/application-stack.ts
import { Port } from 'aws-cdk-lib/aws-ec2';
const svc = new MyService(this, 'MyService', { vpc });
svc.connections.allowFrom(callerSecurityGroup, Port.tcp(8080));

If the caller’s security group is defined in your CDK app and limits outbound traffic, allowFrom also adds the matching outbound rule to it.

The caller connects to a task’s private IP address. The generator sets up no service discovery, and a task’s address changes when ECS replaces the task. To give callers a stable name, set up service discovery yourself, for example with Amazon ECS Service Connect.

The generated service sets its name and image for you. The examples below use the generic EcsService construct and ecs-service module directly, for settings the generated service does not pass.

In CDK, your common-constructs package exports EcsService next to your service’s construct, so you import it the same way: import { EcsService } from '@my-scope/common-constructs'. The generated construct extends EcsService and passes every other prop through, for example cluster, environment, secrets and grantTaskRole. Its optional image prop replaces the image built from your project.

In Terraform, your service’s module lets you set only some of the ecs-service module’s variables: vpc_id, subnet_ids, cluster_arn, environment, secrets, task_role_policy_statements and tags. To set any other variable, edit the module "ecs_service" block in your service’s module.

To run an image that is already built, for example from a public registry, pass its URI. Nothing is built locally. For an image in a private Amazon ECR repository, CDK grants the execution role pull access on that repository. Terraform grants pull access only when you also pass the repository’s ARN as image_repository_arn.

new EcsService(this, 'Prebuilt', {
serviceName: 'Prebuilt',
vpc,
image: { imageUri: 'public.ecr.aws/my-org/my-image@sha256:<digest>' },
});

Each service creates its own ECS cluster unless you pass one in. To run several services in one cluster, pass the same cluster to each.

const cluster = new Cluster(this, 'Cluster', { vpc });
new MyService(this, 'MyService', { vpc, cluster });
new MyOtherService(this, 'MyOtherService', { vpc, cluster });

Plain settings go in environment. Never put secret values there, because anyone who can read the task definition can read them. Pass secrets as references instead. The execution role may read only the secrets you list.

new EcsService(this, 'MyService', {
// ...
environment: { LOG_LEVEL: 'info' },
secrets: { API_KEY: Secret.fromSecretsManager(apiKeySecret) },
});

Each task gets 256 CPU units (a quarter of a vCPU) and 512 MiB of memory by default. The service starts one task, and autoscaling keeps between one and two tasks running, adding tasks when average CPU use goes above 70%.

SettingCDK propTerraform variableDefault
CPU units per taskcpucpu256
Memory per task (MiB)memoryLimitMiBmemory512
Tasks at startdesiredCountdesired_count1
Fewest tasksminCapacitymin_capacity1
Most tasksmaxCapacitymax_capacity2
Target average CPU (%)targetCpuUtilizationPercenttarget_cpu_utilization70

The task role of your service’s construct or module starts with read access to runtime configuration only. Add the permissions your code needs:

new EcsService(this, 'MyService', {
// ...
grantTaskRole: (taskRole) => myTable.grantReadData(taskRole),
});

The task definition runs the container as uid 1000, a non-root user. The vended Dockerfile matches it: it creates a user app with uid and gid 1000 and sets USER 1000:1000. To run as another user, set the CDK user prop or the Terraform container_user variable. Terraform rejects 0 and root.

SettingCDK propTerraform variableDefault
User the container runs asusercontainer_user'1000' (CDK), "1000:1000" (Terraform)

In Terraform, container_user is a variable of the ecs-service module. Your service’s module does not pass it, so set it in its module "ecs_service" block.

A container service bills while it is idle, unlike a Lambda function. These parts bill by the hour whether or not anyone calls the service:

  • Fargate tasks, per vCPU and per GB of memory. desiredCount and minCapacity keep at least one task running.
  • Interface VPC endpoints, per hour in each Availability Zone, or a NAT gateway, per hour and per GB of data it processes.
  • Log storage in CloudWatch Logs, and image storage in Amazon ECR.

To estimate the cost for your Region and traffic, use the AWS Pricing Calculator.

To remove the service, destroy the stack or the Terraform configuration that holds it:

Run your infrastructure project’s destroy target, which runs cdk destroy:

Terminal window
pnpm nx run infra:destroy

Then check in the AWS console or with the AWS CLI that the ECS service and cluster are gone. With both CDK and Terraform, destroy deletes the service’s log groups and schedules its AWS KMS key for deletion. KMS waits 7 days before it deletes the key. Images stay in Amazon ECR, in the CDK assets repository or the shared asset-ecr repository. Delete them if you no longer need them. The VPC is yours, so destroying the service leaves it in place.

Use the connection generator to connect your service to other projects in your workspace:

Run this generator@aws/nx-plugin:connection

pnpm nx g @aws/nx-plugin:connection
Build your command5

Required

Required

Your service can use a relational database or a DynamoDB table. Each connection has its own guide:

A connection needs a service with infra set to ecs.