{"id":9515,"library":"aws-cdk-aws-ecs","title":"AWS CDK AWS ECS Constructs (v1)","description":"The `aws-cdk-aws-ecs` package provides AWS Cloud Development Kit (CDK) constructs for defining Amazon Elastic Container Service (ECS) resources in Python. It is part of AWS CDK v1. While functional, AWS CDK v2 is the recommended and actively developed version, which consolidates all constructs into `aws-cdk-lib`. This package is largely superseded by `aws-cdk-lib`'s ECS constructs.","status":"deprecated","version":"1.204.0","language":"en","source_language":"en","source_url":"https://github.com/aws/aws-cdk.git","tags":["aws-cdk","ecs","fargate","cloud","infrastructure-as-code","v1"],"install":[{"cmd":"pip install aws-cdk-aws-ecs aws-cdk.core","lang":"bash","label":"Install AWS CDK v1 ECS and Core"}],"dependencies":[{"reason":"Core AWS CDK v1 functionality, including App, Stack, and base constructs.","package":"aws-cdk.core"},{"reason":"Required for network constructs like VPCs, which ECS services often depend on.","package":"aws-cdk.aws-ec2","optional":true},{"reason":"Required for defining Elastic Container Registry (ECR) repositories or pulling images.","package":"aws-cdk.aws-ecr","optional":true},{"reason":"Required for defining IAM roles and policies for ECS tasks and services.","package":"aws-cdk.aws-iam","optional":true}],"imports":[{"note":"This package is for AWS CDK v1. The `aws_cdk_lib` namespace is for v2.","wrong":"from aws_cdk_lib import aws_ecs","symbol":"aws_ecs","correct":"from aws_cdk import aws_ecs"},{"note":"AWS CDK v1 core constructs like App and Stack are in `aws_cdk.core`.","wrong":"from aws_cdk_lib import core","symbol":"core","correct":"from aws_cdk import core"},{"note":"Specific service constructs are imported directly under `aws_cdk` in v1.","wrong":"from aws_cdk_lib import aws_ec2","symbol":"aws_ec2","correct":"from aws_cdk import aws_ec2"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    core,\n    aws_ecs as ecs,\n    aws_ec2 as ec2,\n    aws_ecr as ecr,\n    aws_iam as iam,\n)\n\nclass MyEcsFargateStack(core.Stack):\n    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:\n        super().__init__(scope, id, **kwargs)\n\n        # Look up an existing VPC or create a new one\n        vpc = ec2.Vpc(self, \"MyVpc\", max_azs=2)\n\n        cluster = ecs.Cluster(self, \"MyFargateCluster\", vpc=vpc)\n\n        # Create a Task Definition\n        task_definition = ecs.FargateTaskDefinition(\n            self, \"MyTaskDef\",\n            memory_limit_mib=512,\n            cpu=256\n        )\n\n        # Add a container to the task definition\n        # Using a public image for simplicity, replace with your ECR image if needed\n        image_name = os.environ.get('ECR_IMAGE_NAME', 'amazon/amazon-ecs-sample')\n        task_definition.add_container(\n            \"MyContainer\",\n            image=ecs.ContainerImage.from_registry(image_name),\n            port_mappings=[ecs.PortMapping(container_port=80, host_port=80)]\n        )\n\n        # Create a Fargate Service\n        ecs.FargateService(\n            self, \"MyFargateService\",\n            cluster=cluster,\n            task_definition=task_definition,\n            desired_count=1\n        )\n\napp = core.App()\nMyEcsFargateStack(app, \"MyEcsFargateStack\",\n    env=core.Environment(\n        account=os.environ.get(\"CDK_DEFAULT_ACCOUNT\", os.environ.get(\"AWS_ACCOUNT_ID\")),\n        region=os.environ.get(\"CDK_DEFAULT_REGION\", os.environ.get(\"AWS_REGION\"))\n    )\n)\napp.synth()","lang":"python","description":"This quickstart defines an AWS CDK v1 stack that creates an ECS Fargate cluster and deploys a simple Fargate service with a sample container image. It demonstrates importing `aws_ecs`, `aws_ec2`, and `core` constructs as expected in v1. Ensure `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` (or `AWS_ACCOUNT_ID`/`AWS_REGION`) environment variables are set."},"warnings":[{"fix":"For new projects, use `aws-cdk-lib` and its ECS constructs (e.g., `from aws_cdk import aws_ecs as ecs`). For existing v1 projects, ensure all CDK packages are `aws-cdk.*` and avoid `aws-cdk-lib`. Consider migrating to v2 for long-term support and features.","message":"AWS CDK v1 (which this package is part of) is largely superseded by AWS CDK v2. V2 consolidates all core constructs into a single package, 'aws-cdk-lib', and has a different module structure. Mixing v1 and v2 packages or attempting to use v1 constructs with a v2 application will lead to errors.","severity":"breaking","affected_versions":"All v1 versions."},{"fix":"Always include `from aws_cdk import core` and other required base service modules (e.g., `from aws_cdk import aws_ec2`, `from aws_cdk import aws_iam`) alongside `from aws_cdk import aws_ecs`.","message":"CDK v1 constructs often require explicit imports for base modules like 'core', 'aws_ec2', 'aws_iam', etc., in addition to the specific 'aws_ecs' module. Forgetting these can lead to 'NameError' or 'AttributeError'.","severity":"gotcha","affected_versions":"All v1 versions."},{"fix":"Manually inspect AWS console for orphaned resources after `cdk destroy` failures. For S3 buckets, ensure `auto_delete_objects=True` and `removal_policy=core.RemovalPolicy.DESTROY` are set if you want them to be deleted with the stack (use with caution in production).","message":"CDK v1 resources, especially during development, might not always clean up fully upon `cdk destroy` if there are dependencies like S3 buckets not explicitly removed or if the stack fails mid-deletion. This can incur unexpected costs.","severity":"gotcha","affected_versions":"All v1 versions."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Standardize on either CDK v1 (using `aws-cdk.*` packages) or CDK v2 (using `aws-cdk-lib`). Ensure your `cdk.json` and `package.json` (if applicable) align with your chosen version.","cause":"Attempting to use a CDK v1 package (like `aws-cdk-aws-ecs`) in an environment or project configured for CDK v2, or vice versa.","error":"jsii.errors.JavaScriptError: Cannot find module 'aws-cdk-lib'"},{"fix":"Ensure you have `from aws_cdk import aws_ecs as ecs` at the top of your file. If you intended to use CDK v2, you would `pip install aws-cdk-lib` and import `from aws_cdk import aws_ecs as ecs` from the v2 library.","cause":"Missing or incorrect import statement for the 'aws_ecs' module.","error":"NameError: name 'ecs' is not defined"},{"fix":"Pass `env=core.Environment(account=\"...\", region=\"...\")` to your `core.Stack` constructor, not `core.App()`. The `App` is the container, the `Stack` defines the resources within a specific environment.","cause":"In AWS CDK v1, the 'env' parameter should typically be passed to the `Stack` constructor, not directly to `core.App()`. `core.App()` itself does not accept an 'env' argument.","error":"TypeError: __init__() got an unexpected keyword argument 'env' (when passing 'env' directly to 'core.App')"}]}