aws / aws/aws-cdk

sam:CfnFunction is not generating 'ApiId' for events

Open
#27,760 1 comment 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-events 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

For sam.CfnFunction in the events section, 'api_id' is not getting recognised.

This is the below code:

```
from aws_cdk import (
# Duration,
Stack,
# aws_sqs as sqs,
aws_sam as sam
)
import aws_cdk as core
from constructs import Construct

class HttpapiStack(Stack):

def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

my_api_gateway = sam.CfnHttpApi(
self,
f"HttpApiGateway",
description="test,
stage_name="$default",
cors_configuration=sam.CfnHttpApi.CorsConfigurationObjectProperty(
allow_headers=["*"],
allow_methods=["GET", "PUT", "POST", "OPTIONS"],
allow_origins=["*"],
max_age=864000,
),
)
s3_signer_lambda = sam.CfnFunction(
self,
f"S3SignerLambda",
description="test",
timeout=900,
role="arn:aws:iam::1234567890:role/lambdaexecutionrole",
inline_code="return",
handler="index.handler",
runtime="python3.9",
events={
"S3Sign": sam.CfnFunction.EventSourceProperty(
properties=sam.CfnFunction.HttpApiEventProperty(
path="/s3-sign",
method="POST",
api_id= my_api_gateway.ref
),
type="HttpApi"
)
},
)
```
As per the code, in the events section of the CfnFunction, the value for 'api_id' is referenced from the 'CfnHttpApi' defined above so this should resolve as per the cfn return value.

### Expected Behavior

The event section should be resolving as below:

```
S3SignerLambda:
Type: AWS::Serverless::Function
Properties:
Description: test
Events:
S3Sign:
Properties:
Method: POST
Path: /s3-sign
ApiId:
Ref: HttpApiGateway
Type: HttpApi
```

### Current Behavior

However, it is resolving as below where it is observed that it is completely omitting the 'ApiId' property.

```
S3SignerLambda:
Type: AWS::Serverless::Function
Properties:
Description: test
Events:
S3Sign:
Properties:
Method: POST
Path: /s3-sign
Type: HttpApi
```

### Reproduction Steps

Added the code to replicate the same above.

### Possible Solution

As a workaround, using raw overrides as below is resolving the concern but this should not be an ideal case.

```
s3_signer_lambda.add_property_override("Events.S3Sign.Properties.ApiId", my_api_gateway.ref);

```

### Additional Information/Context

_No response_

### CDK CLI Version

2.96.2 and 2.103.0

### Framework Version

_No response_

### Node.js Version

v18.17.1

### OS

macOS Ventura 13.5.2

### Language

Python

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Python reproduction and synthesize the stack using the affected CDK versions, then compare the generated AWS::Serverless::Function event with the expected ApiId Ref. Trace the CfnFunction HttpApiEventProperty handling and verify that synthesis preserves api_id without requiring the raw property override.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
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.