Skip to content
v2.273.0

Experimental

@aws-cdk/aws-applicationsignals-alpha is an experimental (alpha) package: its API may change in any release, without a major version bump.

CloudWatchAgentIntegration ​

A construct that adds CloudWatch Agent as a container to an ECS task definition.

Example ​

ts
import { Construct } from 'constructs';
import * as appsignals from '@aws-cdk/aws-applicationsignals-alpha';
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';

class MyStack extends cdk.Stack {
  public constructor(scope?: Construct, id?: string, props: cdk.StackProps = {}) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'TestVpc', {});
    const cluster = new ecs.Cluster(this, 'TestCluster', { vpc });

    // Define Task Definition for CloudWatch agent (Daemon)
    const cwAgentTaskDefinition = new ecs.Ec2TaskDefinition(this, 'CloudWatchAgentTaskDefinition', {
      networkMode: ecs.NetworkMode.HOST,
    });

    new appsignals.CloudWatchAgentIntegration(this, 'CloudWatchAgentIntegration', {
      taskDefinition: cwAgentTaskDefinition,
      containerName: 'ecs-cwagent',
      enableLogging: false,
      cpu: 128,
      memoryLimitMiB: 64,
      portMappings: [
        {
          containerPort: 4316,
          hostPort: 4316,
        },
        {
          containerPort: 2000,
          hostPort: 2000,
        },
      ],
    });

    // Create the CloudWatch Agent daemon service
    new ecs.Ec2Service(this, 'CloudWatchAgentDaemon', {
      cluster,
      taskDefinition: cwAgentTaskDefinition,
      daemon: true,  // Runs one container per EC2 instance
    });

    // Define Task Definition for user application
    const sampleAppTaskDefinition = new ecs.Ec2TaskDefinition(this, 'SampleAppTaskDefinition', {
      networkMode: ecs.NetworkMode.HOST,
    });

    sampleAppTaskDefinition.addContainer('app', {
      image: ecs.ContainerImage.fromRegistry('test/sample-app'),
      cpu: 0,
      memoryLimitMiB: 512,
    });

    // No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
    new appsignals.ApplicationSignalsIntegration(this, 'ApplicationSignalsIntegration', {
      taskDefinition: sampleAppTaskDefinition,
      instrumentation: {
        sdkVersion: appsignals.PythonInstrumentationVersion.V0_8_0
      },
      serviceName: 'sample-app'
    });

    new ecs.Ec2Service(this, 'MySampleApp', {
      cluster,
      taskDefinition: sampleAppTaskDefinition,
      desiredCount: 1,
    });
  }
}
python
from constructs import Construct
import aws_cdk.aws_applicationsignals_alpha as appsignals
import aws_cdk as cdk
import aws_cdk.aws_ec2 as ec2
import aws_cdk.aws_ecs as ecs

class MyStack(cdk.Stack):
    def __init__(self, scope=None, id=None, *, description=None, env=None, stackName=None, tags=None, notificationArns=None, synthesizer=None, terminationProtection=None, analyticsReporting=None, crossRegionReferences=None, permissionsBoundary=None, suppressTemplateIndentation=None, propertyInjectors=None):
        super().__init__(scope, id, description=description, env=env, stackName=stackName, tags=tags, notificationArns=notificationArns, synthesizer=synthesizer, terminationProtection=terminationProtection, analyticsReporting=analyticsReporting, crossRegionReferences=crossRegionReferences, permissionsBoundary=permissionsBoundary, suppressTemplateIndentation=suppressTemplateIndentation, propertyInjectors=propertyInjectors)

        vpc = ec2.Vpc(self, "TestVpc")
        cluster = ecs.Cluster(self, "TestCluster", vpc=vpc)

        # Define Task Definition for CloudWatch agent (Daemon)
        cw_agent_task_definition = ecs.Ec2TaskDefinition(self, "CloudWatchAgentTaskDefinition",
            network_mode=ecs.NetworkMode.HOST
        )

        appsignals.CloudWatchAgentIntegration(self, "CloudWatchAgentIntegration",
            task_definition=cw_agent_task_definition,
            container_name="ecs-cwagent",
            enable_logging=False,
            cpu=128,
            memory_limit_mi_b=64,
            port_mappings=[ecs.PortMapping(
                container_port=4316,
                host_port=4316
            ), ecs.PortMapping(
                container_port=2000,
                host_port=2000
            )
            ]
        )

        # Create the CloudWatch Agent daemon service
        ecs.Ec2Service(self, "CloudWatchAgentDaemon",
            cluster=cluster,
            task_definition=cw_agent_task_definition,
            daemon=True
        )

        # Define Task Definition for user application
        sample_app_task_definition = ecs.Ec2TaskDefinition(self, "SampleAppTaskDefinition",
            network_mode=ecs.NetworkMode.HOST
        )

        sample_app_task_definition.add_container("app",
            image=ecs.ContainerImage.from_registry("test/sample-app"),
            cpu=0,
            memory_limit_mi_b=512
        )

        # No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
        appsignals.ApplicationSignalsIntegration(self, "ApplicationSignalsIntegration",
            task_definition=sample_app_task_definition,
            instrumentation=appsignals.InstrumentationProps(
                sdk_version=appsignals.PythonInstrumentationVersion.V0_8_0
            ),
            service_name="sample-app"
        )

        ecs.Ec2Service(self, "MySampleApp",
            cluster=cluster,
            task_definition=sample_app_task_definition,
            desired_count=1
        )
java
import software.constructs.Construct;
import software.amazon.awscdk.services.applicationsignals.alpha.*;
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.ec2.*;
import software.amazon.awscdk.services.ecs.*;

public class MyStack extends Stack {
    public MyStack() {
        this(null);
    }

    public MyStack(Construct scope) {
        this(scope, null);
    }

    public MyStack(Construct scope, String id) {
        this(scope, id, StackProps.builder().build());
    }

    public MyStack(Construct scope, String id, StackProps props) {
        super(scope, id, props);

        Vpc vpc = Vpc.Builder.create(this, "TestVpc").build();
        Cluster cluster = Cluster.Builder.create(this, "TestCluster").vpc(vpc).build();

        // Define Task Definition for CloudWatch agent (Daemon)
        Ec2TaskDefinition cwAgentTaskDefinition = Ec2TaskDefinition.Builder.create(this, "CloudWatchAgentTaskDefinition")
                .networkMode(NetworkMode.HOST)
                .build();

        CloudWatchAgentIntegration.Builder.create(this, "CloudWatchAgentIntegration")
                .taskDefinition(cwAgentTaskDefinition)
                .containerName("ecs-cwagent")
                .enableLogging(false)
                .cpu(128)
                .memoryLimitMiB(64)
                .portMappings(List.of(PortMapping.builder()
                        .containerPort(4316)
                        .hostPort(4316)
                        .build(), PortMapping.builder()
                        .containerPort(2000)
                        .hostPort(2000)
                        .build()))
                .build();

        // Create the CloudWatch Agent daemon service
        // Create the CloudWatch Agent daemon service
        Ec2Service.Builder.create(this, "CloudWatchAgentDaemon")
                .cluster(cluster)
                .taskDefinition(cwAgentTaskDefinition)
                .daemon(true)
                .build();

        // Define Task Definition for user application
        Ec2TaskDefinition sampleAppTaskDefinition = Ec2TaskDefinition.Builder.create(this, "SampleAppTaskDefinition")
                .networkMode(NetworkMode.HOST)
                .build();

        sampleAppTaskDefinition.addContainer("app", ContainerDefinitionOptions.builder()
                .image(ContainerImage.fromRegistry("test/sample-app"))
                .cpu(0)
                .memoryLimitMiB(512)
                .build());

        // No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
        // No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
        ApplicationSignalsIntegration.Builder.create(this, "ApplicationSignalsIntegration")
                .taskDefinition(sampleAppTaskDefinition)
                .instrumentation(InstrumentationProps.builder()
                        .sdkVersion(PythonInstrumentationVersion.V0_8_0)
                        .build())
                .serviceName("sample-app")
                .build();

        Ec2Service.Builder.create(this, "MySampleApp")
                .cluster(cluster)
                .taskDefinition(sampleAppTaskDefinition)
                .desiredCount(1)
                .build();
    }
}
csharp
// Install: dotnet add package Amazon.CDK.AWS.ApplicationSignals.Alpha
using Constructs;
using Amazon.CDK.AWS.ApplicationSignals.Alpha;
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.ECS;

class MyStack : Stack
{
    public MyStack(Construct? scope=null, string? id=null, StackProps props=new StackProps { }) : base(scope, id, props)
    {

        var vpc = new Vpc(this, "TestVpc", new VpcProps { });
        var cluster = new Cluster(this, "TestCluster", new ClusterProps { Vpc = vpc });

        // Define Task Definition for CloudWatch agent (Daemon)
        var cwAgentTaskDefinition = new Ec2TaskDefinition(this, "CloudWatchAgentTaskDefinition", new Ec2TaskDefinitionProps {
            NetworkMode = NetworkMode.HOST
        });

        new CloudWatchAgentIntegration(this, "CloudWatchAgentIntegration", new CloudWatchAgentIntegrationProps {
            TaskDefinition = cwAgentTaskDefinition,
            ContainerName = "ecs-cwagent",
            EnableLogging = false,
            Cpu = 128,
            MemoryLimitMiB = 64,
            PortMappings = new [] { new PortMapping {
                ContainerPort = 4316,
                HostPort = 4316
            }, new PortMapping {
                ContainerPort = 2000,
                HostPort = 2000
            } }
        });

        // Create the CloudWatch Agent daemon service
        // Create the CloudWatch Agent daemon service
        new Ec2Service(this, "CloudWatchAgentDaemon", new Ec2ServiceProps {
            Cluster = cluster,
            TaskDefinition = cwAgentTaskDefinition,
            Daemon = true
        });

        // Define Task Definition for user application
        var sampleAppTaskDefinition = new Ec2TaskDefinition(this, "SampleAppTaskDefinition", new Ec2TaskDefinitionProps {
            NetworkMode = NetworkMode.HOST
        });

        sampleAppTaskDefinition.AddContainer("app", new ContainerDefinitionOptions {
            Image = ContainerImage.FromRegistry("test/sample-app"),
            Cpu = 0,
            MemoryLimitMiB = 512
        });

        // No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
        // No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
        new ApplicationSignalsIntegration(this, "ApplicationSignalsIntegration", new ApplicationSignalsIntegrationProps {
            TaskDefinition = sampleAppTaskDefinition,
            Instrumentation = new InstrumentationProps {
                SdkVersion = PythonInstrumentationVersion.V0_8_0
            },
            ServiceName = "sample-app"
        });

        new Ec2Service(this, "MySampleApp", new Ec2ServiceProps {
            Cluster = cluster,
            TaskDefinition = sampleAppTaskDefinition,
            DesiredCount = 1
        });
    }
}
go
// Install: go get github.com/aws/aws-cdk-go/awscdkapplicationsignalsalpha/v2
import "github.com/aws/constructs-go/constructs"
import "github.com/aws/aws-cdk-go/awscdkapplicationsignalsalpha"
import cdk "github.com/aws/aws-cdk-go/awscdk"
import ec2 "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

type myStack struct {
	Stack
}

func newMyStack(scope Construct, id *string, props StackProps) *myStack {
	if props == nil {
		props = &StackProps{
		}
	}
	this := &myStack{}
	cdk.NewStack_Override(this, scope, id, props)

	vpc := ec2.NewVpc(this, jsii.String("TestVpc"), &VpcProps{
	})
	cluster := ecs.NewCluster(this, jsii.String("TestCluster"), &ClusterProps{
		Vpc: Vpc,
	})

	// Define Task Definition for CloudWatch agent (Daemon)
	cwAgentTaskDefinition := ecs.NewEc2TaskDefinition(this, jsii.String("CloudWatchAgentTaskDefinition"), &Ec2TaskDefinitionProps{
		NetworkMode: ecs.NetworkMode_HOST,
	})

	appsignals.NewCloudWatchAgentIntegration(this, jsii.String("CloudWatchAgentIntegration"), &CloudWatchAgentIntegrationProps{
		TaskDefinition: cwAgentTaskDefinition,
		ContainerName: jsii.String("ecs-cwagent"),
		EnableLogging: jsii.Boolean(false),
		Cpu: jsii.Number(128),
		MemoryLimitMiB: jsii.Number(64),
		PortMappings: []PortMapping{
			&PortMapping{
				ContainerPort: jsii.Number(4316),
				HostPort: jsii.Number(4316),
			},
			&PortMapping{
				ContainerPort: jsii.Number(2000),
				HostPort: jsii.Number(2000),
			},
		},
	})

	// Create the CloudWatch Agent daemon service
	// Create the CloudWatch Agent daemon service
	ecs.NewEc2Service(this, jsii.String("CloudWatchAgentDaemon"), &Ec2ServiceProps{
		Cluster: Cluster,
		TaskDefinition: cwAgentTaskDefinition,
		Daemon: jsii.Boolean(true),
	})

	// Define Task Definition for user application
	sampleAppTaskDefinition := ecs.NewEc2TaskDefinition(this, jsii.String("SampleAppTaskDefinition"), &Ec2TaskDefinitionProps{
		NetworkMode: ecs.NetworkMode_HOST,
	})

	sampleAppTaskDefinition.AddContainer(jsii.String("app"), &ContainerDefinitionOptions{
		Image: ecs.ContainerImage_FromRegistry(jsii.String("test/sample-app")),
		Cpu: jsii.Number(0),
		MemoryLimitMiB: jsii.Number(512),
	})

	// No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
	// No CloudWatch Agent side car is needed as application container communicates to CloudWatch Agent daemon through host network
	appsignals.NewApplicationSignalsIntegration(this, jsii.String("ApplicationSignalsIntegration"), &ApplicationSignalsIntegrationProps{
		TaskDefinition: sampleAppTaskDefinition,
		Instrumentation: &InstrumentationProps{
			SdkVersion: appsignals.PythonInstrumentationVersion_V0_8_0(),
		},
		ServiceName: jsii.String("sample-app"),
	})

	ecs.NewEc2Service(this, jsii.String("MySampleApp"), &Ec2ServiceProps{
		Cluster: Cluster,
		TaskDefinition: sampleAppTaskDefinition,
		DesiredCount: jsii.Number(1),
	})
	return this
}

Initializer ​

ts
new CloudWatchAgentIntegration(scope: Construct, id: string, props: CloudWatchAgentIntegrationProps)
python
CloudWatchAgentIntegration(scope: Construct, id: str, props: CloudWatchAgentIntegrationProps)
java
new CloudWatchAgentIntegration(Construct scope, String id, CloudWatchAgentIntegrationProps props)
csharp
new CloudWatchAgentIntegration(Construct scope, string id, CloudWatchAgentIntegrationProps props)
go
NewCloudWatchAgentIntegration(Scope Construct, Id *string, Props CloudWatchAgentIntegrationProps)

Construct Props ​

agentConfigstringagent_configstragentConfigStringAgentConfigstringAgentConfig*string= Uses default configuration for A…#
Custom agent configuration in JSON format.
default

Default: Uses default configuration for Application Signals

containerNamestringcontainer_namestrcontainerNameStringContainerNamestringContainerName*stringrequired#
Name of the CloudWatch Agent container.
cpunumbercpuint | floatcpuNumberCpudoubleCpu*float64= No minimum CPU units reserved.#
The minimum number of CPU units to reserve for the container.
enableLoggingbooleanenable_loggingboolenableLoggingBooleanEnableLoggingboolEnableLogging*bool= false#
Whether to enable logging for the CloudWatch Agent.
essentialbooleanessentialboolessentialBooleanEssentialboolEssential*bool= true#
Start as an essential container.
memoryLimitMiBnumbermemory_limit_mi_bint | floatmemoryLimitMiBNumberMemoryLimitMiBdoubleMemoryLimitMiB*float64= No memory limit.#
The amount (in MiB) of memory to present to the container.
memoryReservationMiBnumbermemory_reservation_mi_bint | floatmemoryReservationMiBNumberMemoryReservationMiBdoubleMemoryReservationMiB*float64= No memory reserved.#
The soft limit (in MiB) of memory to reserve for the container.
operatingSystemFamilyOperatingSystemFamilyoperating_system_familyOperatingSystemFamilyoperatingSystemFamilyOperatingSystemFamilyOperatingSystemFamilyOperatingSystemFamilyOperatingSystemFamilyOperatingSystemFamily= Linux#
Operating system family for the CloudWatch Agent.
portMappingsPortMapping[]port_mappingstyping.List[PortMapping]portMappingsList<PortMapping>PortMappingsPortMapping[]PortMappings*[]PortMapping= No ports are mapped.#
The port mappings to add to the container definition.
taskDefinitionTaskDefinitiontask_definitionTaskDefinitiontaskDefinitionTaskDefinitionTaskDefinitionTaskDefinitionTaskDefinitionTaskDefinitionrequired#
The task definition to integrate CloudWatch agent into.

[disable-awslint:ref-via-interface]

Properties ​

agentContainerContainerDefinitionagent_containerContainerDefinitionagentContainerContainerDefinitionAgentContainerContainerDefinitionAgentContainerContainerDefinitionrequired#
The CloudWatch Agent container definition.
nodeNodenodeNodenodeNodeNodeNodeNodeNoderequired#
The tree node.

Methods ​

isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Construct.

toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#
Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.

1 parameter · returns

Parameters

  • mixinsmixinsmixinsmixinsMixins — The mixins to apply.

Returns: This construct for chaining