aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.48k stars 3.83k forks source link

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

Open jonnekaunisto opened 2 years ago

jonnekaunisto commented 2 years ago

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);
};
tim-finnigan commented 1 year ago

Hi @jonnekaunisto thanks for reporting this. Do you have any updates on your end as far as what you've tried or observed regarding this issue? We just received another issue that appears to be related: https://github.com/aws/aws-cdk/issues/24061. If these are overlapping issues then we likely want to de-duplicate them and close one for tracking purposes.

github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

jonnekaunisto commented 1 year ago

Yeah it's pretty much the same issue

tim-finnigan commented 1 year ago

Thanks for confirming, I've closed the newer issue as a duplicate. I think this one will need to get prioritized for a deep dive to address the root problem.

peterwoodworth commented 1 year ago

We would need to add options for which account and region you're importing the resource from. Then we'd be able to configure the metric appropriately automatically. But as it is now, we support the account and region options as options for this method already, so either way you'll have to specify the account and region somewhere. So i'm not super sure what exactly is the ask here

github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

EHadoux commented 1 year ago

I think the issue is that one would like to be able to do, e.g.:

const queue = Queue.fromQueueArn(this, 'Queue', QUEUE_ARN);
queue.metricApproximateNumberOfMessagesVisible();

but has to do instead:

const queue = Queue.fromQueueArn(this, 'Queue', QUEUE_ARN);
new Metric({
  metricName: 'ApproximateNumberOfMessagesVisible',
  namespace: 'AWS/SQS',
  account: ACCOUNT_NUMBER,
  dimensionsMap: { QueueName: queue.queueName },
});

even though the queue should contain/know everything already as both the region and the account are coming from QUEUE_ARN.