Package: aws-cdk.aws-ecs
Amazon ECS Construct Library---AWS CDK v1 has reached End-of-Support on 2023-06-01.
This package is no longer being updated, and users should migrate to AWS CDK v2.For more information on how to migrate, see theMigrating to AWS CDK v2guide.This package contains constructs for working withAmazon Elastic Container
Service(Amazon ECS).Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service.For further information on Amazon ECS,
see theAmazon ECS documentationThe following example creates an Amazon ECS cluster, adds capacity to it, and
runs a service on it:# vpc: ec2.Vpc# Create an ECS clustercluster=ecs.Cluster(self,"Cluster",vpc=vpc)# Add capacity to itcluster.add_capacity("DefaultAutoScalingGroupCapacity",instance_type=ec2.InstanceType("t2.xlarge"),desired_capacity=3)task_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("DefaultContainer",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=512)# Instantiate an Amazon ECS Serviceecs_service=ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition)For a set of constructs defining common ECS architectural patterns, see the@aws-cdk/aws-ecs-patternspackage.Launch Types: AWS Fargate vs Amazon EC2There are two sets of constructs in this library; one to run tasks on Amazon EC2 and
one to run tasks on AWS Fargate.Use theEc2TaskDefinitionandEc2Serviceconstructs to run tasks on Amazon EC2 instances running in your account.Use theFargateTaskDefinitionandFargateServiceconstructs to run tasks on
instances that are managed for you by AWS.Use theExternalTaskDefinitionandExternalServiceconstructs to run AWS ECS Anywhere tasks on self-managed infrastructure.Here are the main differences:Amazon EC2: instances are under your control. Complete control of task to host
allocation. Required to specify at least a memory reservation or limit for
every container. Can use Host, Bridge and AwsVpc networking modes. Can attach
Classic Load Balancer. Can share volumes between container and host.AWS Fargate: tasks run on AWS-managed instances, AWS manages task to host
allocation for you. Requires specification of memory and cpu sizes at the
taskdefinition level. Only supports AwsVpc networking modes and
Application/Network Load Balancers. Only the AWS log driver is supported.
Many host features are not supported such as adding kernel capabilities
and mounting host devices/volumes inside the container.AWS ECSAnywhere: tasks are run and managed by AWS ECS Anywhere on infrastructure owned by the customer. Only Bridge networking mode is supported. Does not support autoscaling, load balancing, cloudmap or attachment of volumes.For more information on Amazon EC2 vs AWS Fargate, networking and ECS Anywhere see the AWS Documentation:AWS Fargate,Task Networking,ECS AnywhereClustersAClusterdefines the infrastructure to run your
tasks on. You can run many tasks on a single cluster.The following code creates a cluster that can run AWS Fargate tasks:# vpc: ec2.Vpccluster=ecs.Cluster(self,"Cluster",vpc=vpc)The following code imports an existing cluster using the ARN which can be used to
import an Amazon ECS service either EC2 or Fargate.cluster_arn="arn:aws:ecs:us-east-1:012345678910:cluster/clusterName"cluster=ecs.Cluster.from_cluster_arn(self,"Cluster",cluster_arn)To use tasks with Amazon EC2 launch-type, you have to add capacity to
the cluster in order for tasks to be scheduled on your instances.  Typically,
you add an AutoScalingGroup with instances running the latest
Amazon ECS-optimized AMI to the cluster. There is a method to build and add such an
AutoScalingGroup automatically, or you can supply a customized AutoScalingGroup
that you construct yourself. It's possible to add multiple AutoScalingGroups
with various instance types.The following example creates an Amazon ECS cluster and adds capacity to it:# vpc: ec2.Vpccluster=ecs.Cluster(self,"Cluster",vpc=vpc)# Either add default capacitycluster.add_capacity("DefaultAutoScalingGroupCapacity",instance_type=ec2.InstanceType("t2.xlarge"),desired_capacity=3)# Or add customized capacity. Be sure to start the Amazon ECS-optimized AMI.auto_scaling_group=autoscaling.AutoScalingGroup(self,"ASG",vpc=vpc,instance_type=ec2.InstanceType("t2.xlarge"),machine_image=ecs.EcsOptimizedImage.amazon_linux(),# Or use Amazon ECS-Optimized Amazon Linux 2 AMI# machineImage: EcsOptimizedImage.amazonLinux2(),desired_capacity=3)capacity_provider=ecs.AsgCapacityProvider(self,"AsgCapacityProvider",auto_scaling_group=auto_scaling_group)cluster.add_asg_capacity_provider(capacity_provider)If you omit the propertyvpc, the construct will create a new VPC with two AZs.By default, all machine images will auto-update to the latest version
on each deployment, causing a replacement of the instances in your AutoScalingGroup
if the AMI has been updated since the last deployment.If task draining is enabled, ECS will transparently reschedule tasks on to the new
instances before terminating your old instances. If you have disabled task draining,
the tasks will be terminated along with the instance. To prevent that, you
can pick a non-updating AMI by passingcacheInContext: true, but be sure
to periodically update to the latest AMI manually by using theCDK CLI
context management commands:# vpc: ec2.Vpcauto_scaling_group=autoscaling.AutoScalingGroup(self,"ASG",machine_image=ecs.EcsOptimizedImage.amazon_linux(cached_in_context=True),vpc=vpc,instance_type=ec2.InstanceType("t2.micro"))BottlerocketBottlerocketis a Linux-based open source operating system that is
purpose-built by AWS for running containers. You can launch Amazon ECS container instances with the Bottlerocket AMI.The following example will create a capacity with self-managed Amazon EC2 capacity of 2c5.largeLinux instances running withBottlerocketAMI.The following example adds Bottlerocket capacity to the cluster:# cluster: ecs.Clustercluster.add_capacity("bottlerocket-asg",min_capacity=2,instance_type=ec2.InstanceType("c5.large"),machine_image=ecs.BottleRocketImage())ARM64 (Graviton) InstancesTo launch instances with ARM64 hardware, you can use the Amazon ECS-optimized
Amazon Linux 2 (arm64) AMI. Based on Amazon Linux 2, this AMI is recommended
for use when launching your EC2 instances that are powered by Arm-based AWS
Graviton Processors.# cluster: ecs.Clustercluster.add_capacity("graviton-cluster",min_capacity=2,instance_type=ec2.InstanceType("c6g.large"),machine_image=ecs.EcsOptimizedImage.amazon_linux2(ecs.AmiHardwareType.ARM))Bottlerocket is also supported:# cluster: ecs.Clustercluster.add_capacity("graviton-cluster",min_capacity=2,instance_type=ec2.InstanceType("c6g.large"),machine_image_type=ecs.MachineImageType.BOTTLEROCKET)Spot InstancesTo add spot instances into the cluster, you must specify thespotPricein theecs.AddCapacityOptionsand optionally enable thespotInstanceDrainingproperty.# cluster: ecs.Cluster# Add an AutoScalingGroup with spot instances to the existing clustercluster.add_capacity("AsgSpot",max_capacity=2,min_capacity=2,desired_capacity=2,instance_type=ec2.InstanceType("c5.xlarge"),spot_price="0.0735",# Enable the Automated Spot Draining support for Amazon ECSspot_instance_draining=True)SNS Topic EncryptionWhen theecs.AddCapacityOptionsthat you provide has a non-zerotaskDrainTime(the default) then an SNS topic and Lambda are created to ensure that the
cluster's instances have been properly drained of tasks before terminating. The SNS Topic is sent the instance-terminating lifecycle event from the AutoScalingGroup,
and the Lambda acts on that event. If you wish to engageserver-side encryptionfor this SNS Topic
then you may do so by providing a KMS key for thetopicEncryptionKeyproperty ofecs.AddCapacityOptions.# Given# cluster: ecs.Cluster# key: kms.Key# Then, use that key to encrypt the lifecycle-event SNS Topic.cluster.add_capacity("ASGEncryptedSNS",instance_type=ec2.InstanceType("t2.xlarge"),desired_capacity=3,topic_encryption_key=key)Task definitionsA task definition describes what a single copy of ataskshould look like.
A task definition has one or more containers; typically, it has one
main container (thedefault containeris the first one that's added
to the task definition, and it is markedessential) and optionally
some supporting containers which are used to support the main container,
doings things like upload logs or metrics to monitoring services.To run a task or service with Amazon EC2 launch type, use theEc2TaskDefinition. For AWS Fargate tasks/services, use theFargateTaskDefinition. For AWS ECS Anywhere use theExternalTaskDefinition. These classes
provide simplified APIs that only contain properties relevant for each specific launch type.For aFargateTaskDefinition, specify the task size (memoryLimitMiBandcpu):fargate_task_definition=ecs.FargateTaskDefinition(self,"TaskDef",memory_limit_mi_b=512,cpu=256)On Fargate Platform Version 1.4.0 or later, you may specify up to 200GiB ofephemeral storage:fargate_task_definition=ecs.FargateTaskDefinition(self,"TaskDef",memory_limit_mi_b=512,cpu=256,ephemeral_storage_gi_b=100)To add containers to a task definition, calladdContainer():fargate_task_definition=ecs.FargateTaskDefinition(self,"TaskDef",memory_limit_mi_b=512,cpu=256)container=fargate_task_definition.add_container("WebContainer",# Use an image from DockerHubimage=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"))For aEc2TaskDefinition:ec2_task_definition=ecs.Ec2TaskDefinition(self,"TaskDef",network_mode=ecs.NetworkMode.BRIDGE)container=ec2_task_definition.add_container("WebContainer",# Use an image from DockerHubimage=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=1024)For anExternalTaskDefinition:external_task_definition=ecs.ExternalTaskDefinition(self,"TaskDef")container=external_task_definition.add_container("WebContainer",# Use an image from DockerHubimage=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=1024)You can specify container properties when you add them to the task definition, or with various methods, e.g.:To add a port mapping when adding a container to the task definition, specify theportMappingsoption:# task_definition: ecs.TaskDefinitiontask_definition.add_container("WebContainer",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=1024,port_mappings=[ecs.PortMapping(container_port=3000)])To add port mappings directly to a container definition, calladdPortMappings():# container: ecs.ContainerDefinitioncontainer.add_port_mappings(container_port=3000)To add data volumes to a task definition, calladdVolume():fargate_task_definition=ecs.FargateTaskDefinition(self,"TaskDef",memory_limit_mi_b=512,cpu=256)volume={# Use an Elastic FileSystem"name":"mydatavolume","efs_volume_configuration":{"file_system_id":"EFS"}}container=fargate_task_definition.add_volume(volume)Note: ECS Anywhere doesn't support volume attachments in the task definition.To use a TaskDefinition that can be used with either Amazon EC2 or
AWS Fargate launch types, use theTaskDefinitionconstruct.When creating a task definition you have to specify what kind of
tasks you intend to run: Amazon EC2, AWS Fargate, or both.
The following example uses both:task_definition=ecs.TaskDefinition(self,"TaskDef",memory_mi_b="512",cpu="256",network_mode=ecs.NetworkMode.AWS_VPC,compatibility=ecs.Compatibility.EC2_AND_FARGATE)ImagesImages supply the software that runs inside the container. Images can be
obtained from either DockerHub or from ECR repositories, built directly from a local Dockerfile, or use an existing tarball.ecs.ContainerImage.fromRegistry(imageName): use a public image.ecs.ContainerImage.fromRegistry(imageName, { credentials: mySecret }): use a private image that requires credentials.ecs.ContainerImage.fromEcrRepository(repo, tagOrDigest): use the given ECR repository as the image
to start. If no tag or digest is provided, "latest" is assumed.ecs.ContainerImage.fromAsset('./image'): build and upload an
image directly from aDockerfilein your source directory.ecs.ContainerImage.fromDockerImageAsset(asset): uses an existing@aws-cdk/aws-ecr-assets.DockerImageAssetas a container image.ecs.ContainerImage.fromTarball(file): use an existing tarball.new ecs.TagParameterContainerImage(repository): use the given ECR repository as the image
but a CloudFormation parameter as the tag.Environment variablesTo pass environment variables to the container, you can use theenvironment,environmentFiles, andsecretsprops.# secret: secretsmanager.Secret# db_secret: secretsmanager.Secret# parameter: ssm.StringParameter# task_definition: ecs.TaskDefinition# s3_bucket: s3.Bucketnew_container=task_definition.add_container("container",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=1024,environment={# clear text, not for sensitive data"STAGE":"prod"},environment_files=[# list of environment files hosted either on local disk or S3ecs.EnvironmentFile.from_asset("./demo-env-file.env"),ecs.EnvironmentFile.from_bucket(s3_bucket,"assets/demo-env-file.env")],secrets={# Retrieved from AWS Secrets Manager or AWS Systems Manager Parameter Store at container start-up."SECRET":ecs.Secret.from_secrets_manager(secret),"DB_PASSWORD":ecs.Secret.from_secrets_manager(db_secret,"password"),# Reference a specific JSON field, (requires platform version 1.4.0 or later for Fargate tasks)"API_KEY":ecs.Secret.from_secrets_manager_version(secret,ecs.SecretVersionInfo(version_id="12345"),"apiKey"),# Reference a specific version of the secret by its version id or version stage (requires platform version 1.4.0 or later for Fargate tasks)"PARAMETER":ecs.Secret.from_ssm_parameter(parameter)})new_container.add_environment("QUEUE_NAME","MyQueue")The task execution role is automatically granted read permissions on the secrets/parameters. Support for environment
files is restricted to the EC2 launch type for files hosted on S3. Further details provided in the AWS documentation
aboutspecifying environment variables.System controlsTo set system controls (kernel parameters) on the container, use thesystemControlsprop:# task_definition: ecs.TaskDefinitiontask_definition.add_container("container",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_limit_mi_b=1024,system_controls=[ecs.SystemControl(namespace="net",value="ipv4.tcp_tw_recycle")])Using Windows containers on FargateAWS Fargate supports Amazon ECS Windows containers. For more details, please see thisblog post# Create a Task Definition for the Windows container to starttask_definition=ecs.FargateTaskDefinition(self,"TaskDef",runtime_platform=ecs.RuntimePlatform(operating_system_family=ecs.OperatingSystemFamily.WINDOWS_SERVER_2019_CORE,cpu_architecture=ecs.CpuArchitecture.X86_64),cpu=1024,memory_limit_mi_b=2048)task_definition.add_container("windowsservercore",logging=ecs.LogDriver.aws_logs(stream_prefix="win-iis-on-fargate"),port_mappings=[ecs.PortMapping(container_port=80)],image=ecs.ContainerImage.from_registry("mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019"))Using Graviton2 with FargateAWS Graviton2 supports AWS Fargate. For more details, please see thisblog post# Create a Task Definition for running container on Graviton Runtime.task_definition=ecs.FargateTaskDefinition(self,"TaskDef",runtime_platform=ecs.RuntimePlatform(operating_system_family=ecs.OperatingSystemFamily.LINUX,cpu_architecture=ecs.CpuArchitecture.ARM64),cpu=1024,memory_limit_mi_b=2048)task_definition.add_container("webarm64",logging=ecs.LogDriver.aws_logs(stream_prefix="graviton2-on-fargate"),port_mappings=[ecs.PortMapping(container_port=80)],image=ecs.ContainerImage.from_registry("public.ecr.aws/nginx/nginx:latest-arm64v8"))ServiceAServiceinstantiates aTaskDefinitionon aClustera given number of
times, optionally associating them with a load balancer.
If a task fails,
Amazon ECS automatically restarts the task.# cluster: ecs.Cluster# task_definition: ecs.TaskDefinitionservice=ecs.FargateService(self,"Service",cluster=cluster,task_definition=task_definition,desired_count=5)ECS Anywhere service definition looks like:# cluster: ecs.Cluster# task_definition: ecs.TaskDefinitionservice=ecs.ExternalService(self,"Service",cluster=cluster,task_definition=task_definition,desired_count=5)Servicesby default will create a security group if not provided.
If you'd like to specify which security groups to use you can override thesecurityGroupsproperty.Deployment circuit breaker and rollbackAmazon ECSdeployment circuit breakerautomatically rolls back unhealthy service deployments without the need for manual intervention. UsecircuitBreakerto enable
deployment circuit breaker and optionally enablerollbackfor automatic rollback. SeeUsing the deployment circuit breakerfor more details.# cluster: ecs.Cluster# task_definition: ecs.TaskDefinitionservice=ecs.FargateService(self,"Service",cluster=cluster,task_definition=task_definition,circuit_breaker=ecs.DeploymentCircuitBreaker(rollback=True))Note: ECS Anywhere doesn't support deployment circuit breakers and rollback.Include an application/network load balancerServicesare load balancing targets and can be added to a target group, which will be attached to an application/network load balancers:# vpc: ec2.Vpc# cluster: ecs.Cluster# task_definition: ecs.TaskDefinitionservice=ecs.FargateService(self,"Service",cluster=cluster,task_definition=task_definition)lb=elbv2.ApplicationLoadBalancer(self,"LB",vpc=vpc,internet_facing=True)listener=lb.add_listener("Listener",port=80)target_group1=listener.add_targets("ECS1",port=80,targets=[service])target_group2=listener.add_targets("ECS2",port=80,targets=[service.load_balancer_target(container_name="MyContainer",container_port=8080)])Note: ECS Anywhere doesn't support application/network load balancers.Note that in the example above, the defaultserviceonly allows you to register the first essential container or the first mapped port on the container as a target and add it to a new target group. To have more control over which container and port to register as targets, you can useservice.loadBalancerTarget()to return a load balancing target for a specific container and port.Alternatively, you can also create all load balancer targets to be registered in this service, add them to target groups, and attach target groups to listeners accordingly.# cluster: ecs.Cluster# task_definition: ecs.TaskDefinition# vpc: ec2.Vpcservice=ecs.FargateService(self,"Service",cluster=cluster,task_definition=task_definition)lb=elbv2.ApplicationLoadBalancer(self,"LB",vpc=vpc,internet_facing=True)listener=lb.add_listener("Listener",port=80)service.register_load_balancer_targets(container_name="web",container_port=80,new_target_group_id="ECS",listener=ecs.ListenerConfig.application_listener(listener,protocol=elbv2.ApplicationProtocol.HTTPS))Using a Load Balancer from a different StackIf you want to put your Load Balancer and the Service it is load balancing to in
different stacks, you may not be able to use the convenience methodsloadBalancer.addListener()andlistener.addTargets().The reason is that these methods will create resources in the same Stack as the
object they're called on, which may lead to cyclic references between stacks.
Instead, you will have to create anApplicationListenerin the service stack,
or an emptyTargetGroupin the load balancer stack that you attach your
service to.See theecs/cross-stack-load-balancer examplefor the alternatives.Include a classic load balancerServicescan also be directly attached to a classic load balancer as targets:# cluster: ecs.Cluster# task_definition: ecs.TaskDefinition# vpc: ec2.Vpcservice=ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition)lb=elb.LoadBalancer(self,"LB",vpc=vpc)lb.add_listener(external_port=80)lb.add_target(service)Similarly, if you want to have more control over load balancer targeting:# cluster: ecs.Cluster# task_definition: ecs.TaskDefinition# vpc: ec2.Vpcservice=ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition)lb=elb.LoadBalancer(self,"LB",vpc=vpc)lb.add_listener(external_port=80)lb.add_target(service.load_balancer_target(container_name="MyContainer",container_port=80))There are two higher-level constructs available which include a load balancer for you that can be found in the aws-ecs-patterns module:LoadBalancedFargateServiceLoadBalancedEc2ServiceTask Auto-ScalingYou can configure the task count of a service to match demand. Task auto-scaling is
configured by callingautoScaleTaskCount():# target: elbv2.ApplicationTargetGroup# service: ecs.BaseServicescaling=service.auto_scale_task_count(max_capacity=10)scaling.scale_on_cpu_utilization("CpuScaling",target_utilization_percent=50)scaling.scale_on_request_count("RequestScaling",requests_per_target=10000,target_group=target)Task auto-scaling is powered byApplication Auto-Scaling.
See that section for details.Integration with CloudWatch EventsTo start an Amazon ECS task on an Amazon EC2-backed Cluster, instantiate an@aws-cdk/aws-events-targets.EcsTaskinstead of anEc2Service:# cluster: ecs.Cluster# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_asset(path.resolve(__dirname,"..","eventhandler-image")),memory_limit_mi_b=256,logging=ecs.AwsLogDriver(stream_prefix="EventDemo",mode=ecs.AwsLogDriverMode.NON_BLOCKING))# An Rule that describes the event trigger (in this case a scheduled run)rule=events.Rule(self,"Rule",schedule=events.Schedule.expression("rate(1 min)"))# Pass an environment variable to the container 'TheContainer' in the taskrule.add_target(targets.EcsTask(cluster=cluster,task_definition=task_definition,task_count=1,container_overrides=[targets.ContainerOverride(container_name="TheContainer",environment=[targets.TaskEnvironmentVariable(name="I_WAS_TRIGGERED",value="From CloudWatch Events")])]))Log DriversCurrently Supported Log Drivers:awslogsfluentdgelfjournaldjson-filesplunksyslogawsfirelensGenericawslogs Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.aws_logs(stream_prefix="EventDemo"))fluentd Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.fluentd())gelf Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.gelf(address="my-gelf-address"))journald Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.journald())json-file Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.json_file())splunk Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.splunk(token=SecretValue.secrets_manager("my-splunk-token"),url="my-splunk-url"))syslog Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.syslog())firelens Log Driver# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.firelens(options={"Name":"firehose","region":"us-west-2","delivery_stream":"my-stream"}))To pass secrets to the log configuration, use thesecretOptionsproperty of the log configuration. The task execution role is automatically granted read permissions on the secrets/parameters.# secret: secretsmanager.Secret# parameter: ssm.StringParametertask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.LogDrivers.firelens(options={},secret_options={# Retrieved from AWS Secrets Manager or AWS Systems Manager Parameter Store"apikey":ecs.Secret.from_secrets_manager(secret),"host":ecs.Secret.from_ssm_parameter(parameter)}))Generic Log DriverA generic log driver object exists to provide a lower level abstraction of the log driver configuration.# Create a Task Definition for the container to starttask_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("TheContainer",image=ecs.ContainerImage.from_registry("example-image"),memory_limit_mi_b=256,logging=ecs.GenericLogDriver(log_driver="fluentd",options={"tag":"example-tag"}))CloudMap Service DiscoveryTo register your ECS service with a CloudMap Service Registry, you may add thecloudMapOptionsproperty to your service:# task_definition: ecs.TaskDefinition# cluster: ecs.Clusterservice=ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition,cloud_map_options=ecs.CloudMapOptions(# Create A records - useful for AWSVPC network mode.dns_record_type=cloudmap.DnsRecordType.A))Withbridgeorhostnetwork modes, onlySRVDNS record types are supported.
By default,SRVDNS record types will target the default container and default
port. However, you may target a different container and port on the same ECS task:# task_definition: ecs.TaskDefinition# cluster: ecs.Cluster# Add a container to the task definitionspecific_container=task_definition.add_container("Container",image=ecs.ContainerImage.from_registry("/aws/aws-example-app"),memory_limit_mi_b=2048)# Add a port mappingspecific_container.add_port_mappings(container_port=7600,protocol=ecs.Protocol.TCP)ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition,cloud_map_options=ecs.CloudMapOptions(# Create SRV records - useful for bridge networkingdns_record_type=cloudmap.DnsRecordType.SRV,# Targets port TCP port 7600 `specificContainer`container=specific_container,container_port=7600))Associate With a Specific CloudMap ServiceYou may associate an ECS service with a specific CloudMap service. To do
this, use the service'sassociateCloudMapServicemethod:# cloud_map_service: cloudmap.Service# ecs_service: ecs.FargateServiceecs_service.associate_cloud_map_service(service=cloud_map_service)Capacity ProvidersThere are two major families of Capacity Providers:AWS
Fargate(including Fargate Spot) and EC2Auto Scaling
GroupCapacity Providers. Both are supported.Fargate Capacity ProvidersTo enable Fargate capacity providers, you can either setenableFargateCapacityProviderstotruewhen creating your cluster, or by
invoking theenableFargateCapacityProviders()method after creating your
cluster. This will add bothFARGATEandFARGATE_SPOTas available capacity
providers on your cluster.# vpc: ec2.Vpccluster=ecs.Cluster(self,"FargateCPCluster",vpc=vpc,enable_fargate_capacity_providers=True)task_definition=ecs.FargateTaskDefinition(self,"TaskDef")task_definition.add_container("web",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"))ecs.FargateService(self,"FargateService",cluster=cluster,task_definition=task_definition,capacity_provider_strategies=[ecs.CapacityProviderStrategy(capacity_provider="FARGATE_SPOT",weight=2),ecs.CapacityProviderStrategy(capacity_provider="FARGATE",weight=1)])Auto Scaling Group Capacity ProvidersTo add an Auto Scaling Group Capacity Provider, first create an EC2 Auto Scaling
Group. Then, create anAsgCapacityProviderand pass the Auto Scaling Group to
it in the constructor. Then add the Capacity Provider to the cluster. Finally,
you can refer to the Provider by its name in your service's or task's Capacity
Provider strategy.By default, an Auto Scaling Group Capacity Provider will manage the Auto Scaling
Group's size for you. It will also enable managed termination protection, in
order to prevent EC2 Auto Scaling from terminating EC2 instances that have tasks
running on them. If you want to disable this behavior, set bothenableManagedScalingto andenableManagedTerminationProtectiontofalse.# vpc: ec2.Vpccluster=ecs.Cluster(self,"Cluster",vpc=vpc)auto_scaling_group=autoscaling.AutoScalingGroup(self,"ASG",vpc=vpc,instance_type=ec2.InstanceType("t2.micro"),machine_image=ecs.EcsOptimizedImage.amazon_linux2(),min_capacity=0,max_capacity=100)capacity_provider=ecs.AsgCapacityProvider(self,"AsgCapacityProvider",auto_scaling_group=auto_scaling_group)cluster.add_asg_capacity_provider(capacity_provider)task_definition=ecs.Ec2TaskDefinition(self,"TaskDef")task_definition.add_container("web",image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),memory_reservation_mi_b=256)ecs.Ec2Service(self,"EC2Service",cluster=cluster,task_definition=task_definition,capacity_provider_strategies=[ecs.CapacityProviderStrategy(capacity_provider=capacity_provider.capacity_provider_name,weight=1)])Elastic Inference AcceleratorsCurrently, this feature is only supported for services with EC2 launch types.To add elastic inference accelerators to your EC2 instance, first addinferenceAcceleratorsfield to the Ec2TaskDefinition and set thedeviceNameanddeviceTypeproperties.inference_accelerators=[{"device_name":"device1","device_type":"eia2.medium"}]task_definition=ecs.Ec2TaskDefinition(self,"Ec2TaskDef",inference_accelerators=inference_accelerators)To enable using the inference accelerators in the containers, addinferenceAcceleratorResourcesfield and set it to a list of device names used for the inference accelerators. Each value in the
list should match aDeviceNamefor anInferenceAcceleratorspecified in the task definition.# task_definition: ecs.TaskDefinitioninference_accelerator_resources=["device1"]task_definition.add_container("cont",image=ecs.ContainerImage.from_registry("test"),memory_limit_mi_b=1024,inference_accelerator_resources=inference_accelerator_resources)ECS Exec commandPlease note, ECS Exec leverages AWS Systems Manager (SSM). So as a prerequisite for the exec command
to work, you need to have the SSM plugin for the AWS CLI installed locally. For more information, seeInstall Session Manager plugin for AWS CLI.To enable the ECS Exec feature for your containers, set the boolean flagenableExecuteCommandtotruein
yourEc2ServiceorFargateService.# cluster: ecs.Cluster# task_definition: ecs.TaskDefinitionservice=ecs.Ec2Service(self,"Service",cluster=cluster,task_definition=task_definition,enable_execute_command=True)Enabling loggingYou can enable sending logs of your execute session commands to a CloudWatch log group or S3 bucket by configuring
theexecuteCommandConfigurationproperty for your cluster. The default configuration will send the
logs to the CloudWatch Logs using theawslogslog driver that is configured in your task definition. Please note,
when using your ownlogConfigurationthe log group or S3 Bucket specified must already be created.To encrypt data using your own KMS Customer Key (CMK), you must create a CMK and provide the key in thekmsKeyfield
of theexecuteCommandConfiguration. To use this key for encrypting CloudWatch log data or S3 bucket, make sure to associate the key
to these resources on creation.# vpc: ec2.Vpckms_key=kms.Key(self,"KmsKey")# Pass the KMS key in the `encryptionKey` field to associate the key to the log grouplog_group=logs.LogGroup(self,"LogGroup",encryption_key=kms_key)# Pass the KMS key in the `encryptionKey` field to associate the key to the S3 bucketexec_bucket=s3.Bucket(self,"EcsExecBucket",encryption_key=kms_key)cluster=ecs.Cluster(self,"Cluster",vpc=vpc,execute_command_configuration=ecs.ExecuteCommandConfiguration(kms_key=kms_key,log_configuration=ecs.ExecuteCommandLogConfiguration(cloud_watch_log_group=log_group,cloud_watch_encryption_enabled=True,s3_bucket=exec_bucket,s3_encryption_enabled=True,s3_key_prefix="exec-command-output"),logging=ecs.ExecuteCommandLogging.OVERRIDE))