BaseService

class aws_cdk.aws_ecs.BaseService(scope, id, props, additional_props, task_definition)

Bases: Resource

The base class for Ec2Service and FargateService services.

ExampleMetadata:

infused

Example:

import aws_cdk.aws_ecs as ecs


service = ecs.BaseService.from_service_arn_with_cluster(self, "EcsService", "arn:aws:ecs:us-east-1:123456789012:service/myClusterName/myServiceName")
pipeline = codepipeline.Pipeline(self, "MyPipeline")
build_output = codepipeline.Artifact()
# add source and build stages to the pipeline as usual...
deploy_stage = pipeline.add_stage(
    stage_name="Deploy",
    actions=[
        codepipeline_actions.EcsDeployAction(
            action_name="DeployAction",
            service=service,
            input=build_output
        )
    ]
)

Constructs a new instance of the BaseService class.

Parameters:

Methods

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:

policy (RemovalPolicy) –

Return type:

None

associate_cloud_map_service(*, service, container=None, container_port=None)

Associates this service with a CloudMap service.

Parameters:
  • service (IService) – The cloudmap service to register with.

  • container (Optional[ContainerDefinition]) – The container to point to for a SRV record. Default: - the task definition’s default container

  • container_port (Union[int, float, None]) – The port to point to for a SRV record. Default: - the default port of the task definition’s default container

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:
  • 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, container=None, container_port=None, dns_record_type=None, dns_ttl=None, failure_threshold=None, name=None)

Enable CloudMap service discovery for the service.

Parameters:
  • 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

  • container (Optional[ContainerDefinition]) – The container to point to for a SRV record. Default: - the task definition’s default container

  • container_port (Union[int, float, None]) – The port to point to for a SRV record. Default: - the default port of the task definition’s default container

  • 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 if TaskDefinition.networkMode = AWS_VPC, otherwise DnsRecordType.SRV

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

  • 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 type:

Service

Returns:

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:
  • 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

Return type:

IEcsLoadBalancerTarget

Example:

# listener: elbv2.ApplicationListener
# service: ecs.BaseService

listener.add_targets("ECS",
    port=80,
    targets=[service.load_balancer_target(
        container_name="MyContainer",
        container_port=1234
    )]
)
metric(metric_name, *, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

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

Parameters:
  • metric_name (str) –

  • account (Optional[str]) – Account which this metric comes from. Default: - Deployment account.

  • color (Optional[str]) – The hex color code, prefixed with ‘#’ (e.g. ‘#00ff00’), to use when this metric is rendered on a graph. The Color class has a set of standard colors that can be used here. Default: - Automatic color

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

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

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard. You can use dynamic labels to show summary information about the entire displayed time series in the legend. For example, if you use:: [max: ${MAX}] MyMetric As the metric label, the maximum value in the visible range will be shown next to the time series name in the graph’s legend. Default: - No label

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

  • region (Optional[str]) – Region which this metric comes from. Default: - Deployment region.

  • 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 used to filter the metric stream. Only refer to datums emitted to the metric stream with the given unit and ignore all others. Only useful when datums are being emitted to the same metric stream under different units. The default is to use all matric datums in the stream, regardless of unit, which is recommended in nearly all cases. CloudWatch does not honor this property for graphs. Default: - All metric datums in the given metric stream

Return type:

Metric

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

This method returns the CloudWatch metric for this service’s CPU utilization.

Parameters:
  • account (Optional[str]) – Account which this metric comes from. Default: - Deployment account.

  • color (Optional[str]) – The hex color code, prefixed with ‘#’ (e.g. ‘#00ff00’), to use when this metric is rendered on a graph. The Color class has a set of standard colors that can be used here. Default: - Automatic color

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

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

  • label (Optional[str]) –

    Label for this metric when added to a Graph in a Dashboard. You can use dynamic labels to show summary information about the entire displayed time series in the legend. For example, if you use:: [max: ${MAX}] MyMetric As the metric label, the maximum value in the visible range will be shown next to the time series name in the graph’s legend. Default: - No label

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

  • region (Optional[str]) – Region which this metric comes from. Default: - Deployment region.

  • 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 used to filter the metric stream. Only refer to datums emitted to the metric stream with the given unit and ignore all others. Only useful when datums are being emitted to the same metric stream under different units. The default is to use all matric datums in the stream, regardless of unit, which is recommended in nearly all cases. CloudWatch does not honor this property for graphs. Default: - All metric datums in the given metric stream

Default:

average over 5 minutes

Return type:

Metric

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

This method returns the CloudWatch metric for this service’s memory utilization.

Parameters:
  • account (Optional[str]) – Account which this metric comes from. Default: - Deployment account.

  • color (Optional[str]) – The hex color code, prefixed with ‘#’ (e.g. ‘#00ff00’), to use when this metric is rendered on a graph. The Color class has a set of standard colors that can be used here. Default: - Automatic color

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

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

  • label (Optional[str]) –

    Label for this metric when added to a Graph in a Dashboard. You can use dynamic labels to show summary information about the entire displayed time series in the legend. For example, if you use:: [max: ${MAX}] MyMetric As the metric label, the maximum value in the visible range will be shown next to the time series name in the graph’s legend. Default: - No label

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

  • region (Optional[str]) – Region which this metric comes from. Default: - Deployment region.

  • 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 used to filter the metric stream. Only refer to datums emitted to the metric stream with the given unit and ignore all others. Only useful when datums are being emitted to the same metric stream under different units. The default is to use all matric datums in the stream, regardless of unit, which is recommended in nearly all cases. CloudWatch does not honor this property for graphs. Default: - All metric datums in the given metric stream

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) –

Return type:

None

Example:

# listener: elbv2.ApplicationListener
# service: ecs.BaseService

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
    )
)
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.

cluster

The cluster that hosts the service.

connections

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

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

node

The construct tree node associated with this construct.

service_arn

The Amazon Resource Name (ARN) of the service.

service_name

The name of the service.

Attribute:

true

stack

The stack in which this resource is defined.

task_definition

The task definition to use for tasks in the service.

Static Methods

classmethod from_service_arn_with_cluster(scope, id, service_arn)

Import an existing ECS/Fargate Service using the service cluster format.

The format is the “new” format “arn:aws:ecs:region:aws_account_id:service/cluster-name/service-name”.

Parameters:
  • scope (Construct) –

  • id (str) –

  • service_arn (str) –

See:

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#ecs-resource-ids

Return type:

IBaseService

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool