aws / aws/aws-cdk

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

Open
#18,951 7 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-sqs effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### 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);
};
```

Contributor guide

Open the contributing guide

Research direction

Start with tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts and compare its generated metric implementation with the cross-account queue reproduction in the issue. Synthesize the dashboard metric for a resource imported from another account or region and inspect whether accountId and region are retained. Done means the generated metric carries the resource’s account and region information.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.