Skip to content

(cfn2ts): "metric" function does not work cross account/region resources #18951

@jonnekaunisto

Description

@jonnekaunisto

What is the problem?

When using the metric function on an Interface of a resource in another region or account, a metric is returned with no account or region information, which means that the metric does not properly work on alarms or dashboards.

Reproduction Steps

For example when doing something like this:

const queue = Queue.fromQueueArn(this, 'crossAccountQueue', queueArnFromAnotherAccount);
queue.metricNumberOfMessagesSent()

What did you expect to happen?

The metric should be for the queue on the other account.

The source on dashboard should look like this ideally:

 [ "AWS/SQS", "NumberOfMessagesSent", "QueueName", "SomeQueue", { "label": "SYD", "accountId": "281919751090", "region": "ap-southeast-2", "stat": "Sum" } ],

What actually happened?

If you add the following metric to your dashboard it will try to get the metric from the account that the dashboard is in instead of where the queue is.

The source on dashboard looked like this:

 [ "AWS/SQS", "NumberOfMessagesSent", "QueueName", "SomeQueue", { "label": "SYD",  "stat": "Sum" } ],

CDK CLI Version

1.143.0

Framework Version

1.139.0

Node.js Version

16

OS

Mac OS

Language

Typescript

Language Version

3.6.4

Other information

The way the metric function is implemented currently is by using code generation here: https://github.com/aws/aws-cdk/blob/3721358fa1501e42b3514b8a8f15f05c9615f149/tools/%40aws-cdk/cfn2ts/lib/augmentation-generator.ts

The following code is generated by that:

function_base_1.FunctionBase.prototype.metric = function (metricName, props) {
    return new cloudwatch.Metric({
        namespace: 'AWS/Lambda',
        metricName,
        dimensionsMap: { FunctionName: this.functionName },
        ...props
    }).attachTo(this);
};

I believe changing it to this would fix it:

function_base_1.FunctionBase.prototype.metric = function (metricName, props) {
    return new cloudwatch.Metric({
        namespace: 'AWS/Lambda',
        metricName,
        dimensionsMap: { FunctionName: this.functionName },
        account: this.account, //actually we'd parse it from the arn
        region: this.region,
        ...props
    }).attachTo(this);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-sqsRelated to Amazon Simple Queue Serviceeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions