Skip to content

(aws-events-targets): ApiDestination target ignores path_parameter_values and query_string_parameters #21101

@soucema9

Description

@soucema9

Describe the bug

ApiDestination target construct ignores path_parameter_values and query_string_parameters if no header_parameters are provided.

Expected Behavior

If only path_parameter_values is provided it is rendered to CF template

aws_events_targets.ApiDestination(
    api_destination=my_api_destination,
    path_parameter_values=["$.detail.pathParameter"],
)
{
    "Id": "Target0",
    "Arn": ...,
    "RoleArn": ...,
    "HttpParameters": {
        "PathParameterValues": [ "$.detail.pathParameter" ]
    }
}

If only query_string_parameters is provided it is rendered to CF template

aws_events_targets.ApiDestination(
    api_destination=my_api_destination,
    query_string_parameters={"queryParameterName": "$.detail.queryParameterValue"},
)
{
    "Id": "Target0",
    "Arn": ...,
    "RoleArn": ...,
    "HttpParameters": {
        "QueryStringParameters": {
            "queryParameterName": "$.detail.queryParameterValue"
       }
    }
}

Current Behavior

Currently both path_parameter_values and query_string_parameters parameters are ignored and HttpParameters object is not added to CF template.

aws_events_targets.ApiDestination(
    api_destination=my_api_destination,
    path_parameter_values=["$.detail.pathParameter"],
    query_string_parameters={"queryParameterName": "$.detail.queryParameterValue"},
)
{
    "Id": "Target0",
    "Arn": ...,
    "RoleArn": ...,
    // missing HttpParameters
}

But if header_parameters is added everything seems to work. HttpParameters object is added to CF template and it even correctly represents path_parameter_values and query_string_parameters parameters.

aws_events_targets.ApiDestination(
    api_destination=my_api_destination,
    path_parameter_values=["$.detail.pathParameter"],
    query_string_parameters={"queryParameterName": "$.detail.queryParameterValue"},
    header_parameters={"cdk-workaround": "$.detail.headerValue"},
)
{
    "Id": "Target0",
    "Arn": ...,
    "RoleArn": ...,
    "HttpParameters": {
        "HeaderParameters": {
           "cdk-workaround": "$.detail.headerValue"
        },
        "PathParameterValues": [
            "$.detail.pathParameter"
        ],
        "QueryStringParameters": {
            "queryParameterName": "$.detail.queryParameterValue"
        }
    }
}

Reproduction Steps

from aws_cdk import aws_events, aws_events_targets, core

app = core.App()
stack = core.Stack(app, 'stack-name')

my_api_destination = aws_events.ApiDestination(
    stack, 'MyApiDestination',
    connection=aws_events.Connection(
        stack, 'MyConnection',
        authorization=aws_events.Authorization.api_key(
            api_key_name='Authorization', 
            api_key_value=core.SecretValue.unsafe_plain_text('xxx'))
    ),
    endpoint='https://example.com/*',
    http_method=aws_events.HttpMethod.POST,
)

rule = aws_events.Rule(
    stack, 'EventBridgeRule',
    event_pattern=aws_events.EventPattern(account=['123456789012']),
    targets=[
        aws_events_targets.ApiDestination(
            api_destination=my_api_destination,
            path_parameter_values=["$.detail.pathParameter"],
            query_string_parameters={"queryParameterName": "$.detail.queryParameterValue"},
            # header_parameters={"cdk-workaround": "$.detail.headerValue"},
        )
    ],
)

app.synth()

Possible Solution

I'm not a TypeScript developer. But it seems that problem is combination of !! and ?? operators in this condition.

public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
const httpParameters: events.CfnRule.HttpParametersProperty | undefined =
!!this.props.headerParameters ??
!!this.props.pathParameterValues ??
!!this.props.queryStringParameters
? {
headerParameters: this.props.headerParameters,
pathParameterValues: this.props.pathParameterValues,
queryStringParameters: this.props.queryStringParameters,
} : undefined;

Additional Information/Context

No response

CDK CLI Version

2.31.1 (build 42432c6)

Framework Version

No response

Node.js Version

v16.3.0

OS

macOS 12.4

Language

Python

Language Version

Python 3.8

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions