aws / aws/aws-cdk

(aws-lambda): imported capacity providers can't use attach to functions

Open
#36,760 4 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-lambda bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

In the underlying APIs, all that is needed to attach a capacity provider to a Lambda function is the ARN of that capacity provider, like:
```yaml
MyFunction:
...
CapacityProviderConfig:
LambdaManagedInstancesCapacityProviderConfig:
CapacityProviderArn: arn:aws:lambda:us-east-1:capacity-provider:myCP
```
In the CDK when you're creating both a function and capacity provider, it looks like this in Typescript:
```typescript
const myFunc = new lambda.Function(...);
const myCP = new lambda.CapacityProvider(...);
myCP.add(myFunc);
```

However, when the capacity provider is imported via its ARN or name, this does not work, even though it should. It should work because all we need is the ARN, but it doesn't in the following case:

```typescript
const myFunc = new lambda.Function(...);
const myCP = CapacityProvider.fromCapacityProviderArn(...);
myCP.add(myFunc);
```

This is because the `fromCapacityProviderArn` function returns a value that is only guaranteed to be of the `ICapacityProvider` interface (in practice an `Import` type, which extends `CapacityProviderBase`. However, the `addFunction` method is only on the `CapacityProvider` construct, so object isn't guaranteed to have it.

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Library Version

_No response_

### Expected Behavior

```typescript
const myFunc = new lambda.Function(...);
const myCP = CapacityProvider.fromCapacityProviderArn('arn:aws:lambda:us-east-1:capacity-provider:myCP');
myCP.add(myFunc);
```

Should work and end up with CloudFormation like
```yaml
MyFunction:
...
CapacityProviderConfig:
LambdaManagedInstancesCapacityProviderConfig:
CapacityProviderArn: arn:aws:lambda:us-east-1:capacity-provider:myCP
```

### Current Behavior

```typescript
const myFunc = new lambda.Function(...);
const myCP = CapacityProvider.fromCapacityProviderArn(...);
myCP.add(myFunc);
```
gives typecheck error.

### Reproduction Steps

```typescript
const myFunc = new lambda.Function(...);
const myCP = CapacityProvider.fromCapacityProviderArn(...);
myCP.add(myFunc);
```
should give typecheck error.

### Possible Solution

Move the `addFunction` method to be declared in the interface and be implemented in `CapacityProviderBase`. This would be backwards compatible since the construct extends `CapacityProviderBase`, and we're only extending the interface.

### Additional Information/Context

_No response_

### AWS CDK Library version (aws-cdk-lib)

2.235.1

### AWS CDK CLI version

2.1033.0

### Node.js Version

v18.20.2

### OS

MacOS

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with CapacityProvider.fromCapacityProviderArn, the ICapacityProvider interface, and CapacityProviderBase, then trace how add is exposed for imported providers. Done means the TypeScript example type-checks for an imported ARN and produces the shown Lambda CapacityProviderConfig with that ARN.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.