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.68k stars 3.93k forks source link

sam:CfnFunction is not generating 'ApiId' for events #27760

Open arshikam opened 1 year ago

arshikam commented 1 year ago

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

pahud commented 1 year ago

Yes I can confirm only path and method are synthesized. This seems to be a bug.

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-httpapi.html