Ec2Service

class aws_cdk.aws_ecs.Ec2Service(scope, id, *, task_definition, assign_public_ip=None, daemon=None, placement_constraints=None, placement_strategies=None, propagate_task_tags_from=None, security_group=None, vpc_subnets=None, cluster, cloud_map_options=None, desired_count=None, enable_ecs_managed_tags=None, health_check_grace_period=None, max_healthy_percent=None, min_healthy_percent=None, propagate_tags=None, service_name=None)

Bases: aws_cdk.aws_ecs.BaseService

This creates a service using the EC2 launch type on an ECS cluster.

resource: :resource:: AWS::ECS::Service

__init__(scope, id, *, task_definition, assign_public_ip=None, daemon=None, placement_constraints=None, placement_strategies=None, propagate_task_tags_from=None, security_group=None, vpc_subnets=None, cluster, cloud_map_options=None, desired_count=None, enable_ecs_managed_tags=None, health_check_grace_period=None, max_healthy_percent=None, min_healthy_percent=None, propagate_tags=None, service_name=None)

Constructs a new instance of the Ec2Service class.

Parameters
  • scope (Construct) –

  • id (str) –

  • props

  • task_definition (TaskDefinition) – The task definition to use for tasks in the service. [disable-awslint:ref-via-interface]

  • assign_public_ip (Optional[bool]) – Specifies whether the task’s elastic network interface receives a public IP address. If true, each task will receive a public IP address. This property is only used for tasks that use the awsvpc network mode. Default: - Use subnet default.

  • daemon (Optional[bool]) – Specifies whether the service will use the daemon scheduling strategy. If true, the service scheduler deploys exactly one task on each container instance in your cluster. When you are using this strategy, do not specify a desired number of tasks orany task placement strategies. Default: false

  • placement_constraints (Optional[List[PlacementConstraint]]) – The placement constraints to use for tasks in the service. For more information, see Amazon ECS Task Placement Constraints. Default: - No constraints.

  • placement_strategies (Optional[List[PlacementStrategy]]) – The placement strategies to use for tasks in the service. For more information, see Amazon ECS Task Placement Strategies. Default: - No strategies.

  • propagate_task_tags_from (Optional[PropagatedTagSource]) – Specifies whether to propagate the tags from the task definition or the service to the tasks in the service. Tags can only be propagated to the tasks within the service during service creation. Default: PropagatedTagSource.NONE

  • security_group (Optional[ISecurityGroup]) – The security groups to associate with the service. If you do not specify a security group, the default security group for the VPC is used. This property is only used for tasks that use the awsvpc network mode. Default: - A new security group is created.

  • vpc_subnets (Optional[SubnetSelection]) – The subnets to associate with the service. This property is only used for tasks that use the awsvpc network mode. Default: - Private subnets.

  • cluster (ICluster) – The name of the cluster that hosts the service.

  • cloud_map_options (Optional[CloudMapOptions]) – The options for configuring an Amazon ECS service to use service discovery. Default: - AWS Cloud Map service discovery is not enabled.

  • desired_count (Union[int, float, None]) – The desired number of instantiations of the task definition to keep running on the service. Default: 1

  • enable_ecs_managed_tags (Optional[bool]) – Specifies whether to enable Amazon ECS managed tags for the tasks within the service. For more information, see Tagging Your Amazon ECS Resources Default: false

  • health_check_grace_period (Optional[Duration]) – The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy Elastic Load Balancing target health checks after a task has first started. Default: - defaults to 60 seconds if at least one load balancer is in-use and it is not already set

  • max_healthy_percent (Union[int, float, None]) – The maximum number of tasks, specified as a percentage of the Amazon ECS service’s DesiredCount value, that can run in a service during a deployment. Default: - 100 if daemon, otherwise 200

  • min_healthy_percent (Union[int, float, None]) – The minimum number of tasks, specified as a percentage of the Amazon ECS service’s DesiredCount value, that must continue to run and remain healthy during a deployment. Default: - 0 if daemon, otherwise 50

  • propagate_tags (Optional[PropagatedTagSource]) – Specifies whether to propagate the tags from the task definition or the service to the tasks in the service. Valid values are: PropagatedTagSource.SERVICE, PropagatedTagSource.TASK_DEFINITION or PropagatedTagSource.NONE Default: PropagatedTagSource.NONE

  • service_name (Optional[str]) – The name of the service. Default: - CloudFormation-generated name.

Return type

None

Methods

add_placement_constraints(*constraints)

Adds one or more placement strategies to use for tasks in the service.

For more information, see Amazon ECS Task Placement Constraints.

Parameters

constraints (PlacementConstraint) –

Return type

None

add_placement_strategies(*strategies)

Adds one or more placement strategies to use for tasks in the service.

For more information, see Amazon ECS Task Placement Strategies.

Parameters

strategies (PlacementStrategy) –

Return type

None

attach_to_application_target_group(target_group)

This method is called to attach this service to an Application Load Balancer.

Don’t call this function directly. Instead, call listener.addTargets() to add this service to a load balancer.

Parameters

target_group (IApplicationTargetGroup) –

Return type

LoadBalancerTargetProps

attach_to_classic_lb(load_balancer)

Registers the service as a target of a Classic Load Balancer (CLB).

Don’t call this. Call loadBalancer.addTarget() instead.

Parameters

load_balancer (LoadBalancer) –

Return type

None

attach_to_network_target_group(target_group)

This method is called to attach this service to a Network Load Balancer.

Don’t call this function directly. Instead, call listener.addTargets() to add this service to a load balancer.

Parameters

target_group (INetworkTargetGroup) –

Return type

LoadBalancerTargetProps

auto_scale_task_count(*, max_capacity, min_capacity=None)

An attribute representing the minimum and maximum task count for an AutoScalingGroup.

Parameters
  • props

  • max_capacity (Union[int, float]) – Maximum capacity to scale to.

  • min_capacity (Union[int, float, None]) – Minimum capacity to scale to. Default: 1

Return type

ScalableTaskCount

enable_cloud_map(*, cloud_map_namespace=None, dns_record_type=None, dns_ttl=None, failure_threshold=None, name=None)

Enable CloudMap service discovery for the service.

Parameters
  • options

  • cloud_map_namespace (Optional[INamespace]) – The service discovery namespace for the Cloud Map service to attach to the ECS service. Default: - the defaultCloudMapNamespace associated to the cluster

  • dns_record_type (Optional[DnsRecordType]) – The DNS record type that you want AWS Cloud Map to create. The supported record types are A or SRV. Default: DnsRecordType.A

  • dns_ttl (Optional[Duration]) – The amount of time that you want DNS resolvers to cache the settings for this record. Default: 60

  • failure_threshold (Union[int, float, None]) – The number of 30-second intervals that you want Cloud Map to wait after receiving an UpdateInstanceCustomHealthStatus request before it changes the health status of a service instance. NOTE: This is used for HealthCheckCustomConfig

  • name (Optional[str]) – The name of the Cloud Map service to attach to the ECS service. Default: CloudFormation-generated name

return :rtype: Service :return: The created CloudMap service

load_balancer_target(*, container_name, container_port=None, protocol=None)

Return a load balancing target for a specific container and port.

Use this function to create a load balancer target if you want to load balance to another container than the first essential container or the first mapped port on the container.

Use the return value of this function where you would normally use a load balancer target, instead of the Service object itself.

Parameters
  • options

  • container_name (str) – The name of the container.

  • container_port (Union[int, float, None]) – The port number of the container. Only applicable when using application/network load balancers. Default: - Container port of the first added port mapping.

  • protocol (Optional[Protocol]) – The protocol used for the port mapping. Only applicable when using application load balancers. Default: Protocol.TCP

Example::

# Example may have issues. See https://github.com/aws/jsii/issues/826 listener.add_targets(service.load_balancer_target(

container_name=”MyContainer”, container_port=1234

))

Return type

IEcsLoadBalancerTarget

metric(metric_name, *, color=None, dimensions=None, label=None, period=None, statistic=None, unit=None)

This method returns the specified CloudWatch metric name for this service.

Parameters
  • metric_name (str) –

  • props

  • color (Optional[str]) – Color for this metric when added to a Graph in a Dashboard.

  • dimensions (Optional[Mapping[str, Any]]) – Dimensions of the metric. Default: - No dimensions.

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard.

  • period (Optional[Duration]) – The period over which the specified statistic is applied. Default: Duration.minutes(5)

  • statistic (Optional[str]) – What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Default: Average

  • unit (Optional[Unit]) – Unit for the metric that is associated with the alarm.

Return type

Metric

metric_cpu_utilization(*, color=None, dimensions=None, label=None, period=None, statistic=None, unit=None)

This method returns the CloudWatch metric for this clusters CPU utilization.

Parameters
  • props

  • color (Optional[str]) – Color for this metric when added to a Graph in a Dashboard.

  • dimensions (Optional[Mapping[str, Any]]) – Dimensions of the metric. Default: - No dimensions.

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard.

  • period (Optional[Duration]) – The period over which the specified statistic is applied. Default: Duration.minutes(5)

  • statistic (Optional[str]) – What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Default: Average

  • unit (Optional[Unit]) – Unit for the metric that is associated with the alarm.

default :default: average over 5 minutes

Return type

Metric

metric_memory_utilization(*, color=None, dimensions=None, label=None, period=None, statistic=None, unit=None)

This method returns the CloudWatch metric for this clusters memory utilization.

Parameters
  • props

  • color (Optional[str]) – Color for this metric when added to a Graph in a Dashboard.

  • dimensions (Optional[Mapping[str, Any]]) – Dimensions of the metric. Default: - No dimensions.

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard.

  • period (Optional[Duration]) – The period over which the specified statistic is applied. Default: Duration.minutes(5)

  • statistic (Optional[str]) – What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Default: Average

  • unit (Optional[Unit]) – Unit for the metric that is associated with the alarm.

default :default: average over 5 minutes

Return type

Metric

register_load_balancer_targets(*targets)

Use this function to create all load balancer targets to be registered in this service, add them to target groups, and attach target groups to listeners accordingly.

Alternatively, you can use listener.addTargets() to create targets and add them to target groups.

Parameters

targets (EcsTarget) –

Example::

# Example may have issues. See https://github.com/aws/jsii/issues/826 service.register_load_balancer_targets(

container_target={

“container_name”: “web”, “container_port”: 80

}, target_group_id=”ECS”, listener=ecs.ListenerConfig.application_listener(listener,

protocol=elbv2.ApplicationProtocol.HTTPS

)

)

Return type

None

to_string()

Returns a string representation of this construct.

Return type

str

Attributes

cloud_map_service

The CloudMap service created for this service, if any.

Return type

Optional[IService]

cluster

The cluster that hosts the service.

Return type

ICluster

connections

The security groups which manage the allowed network traffic for the service.

Return type

Connections

node

Construct tree node which offers APIs for interacting with the construct tree.

Return type

ConstructNode

service_arn

The Amazon Resource Name (ARN) of the service.

Return type

str

service_name

The name of the service.

attribute: :attribute:: true

Return type

str

stack

The stack in which this resource is defined.

Return type

Stack

task_definition

The task definition to use for tasks in the service.

Return type

TaskDefinition

Static Methods

classmethod from_ec2_service_arn(scope, id, ec2_service_arn)

Imports from the specified service ARN.

Parameters
  • scope (Construct) –

  • id (str) –

  • ec2_service_arn (str) –

Return type

IEc2Service

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters

x (Any) –

Return type

bool