Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CfnFunction Not Initializing Event Source Properties for Api Event #3917

Closed
SherMM opened this issue Sep 3, 2019 · 3 comments · Fixed by aws/jsii#919
Closed

CfnFunction Not Initializing Event Source Properties for Api Event #3917

SherMM opened this issue Sep 3, 2019 · 3 comments · Fixed by aws/jsii#919
Assignees
Labels
bug This issue is a bug. language/python Related to Python bindings

Comments

@SherMM
Copy link

SherMM commented Sep 3, 2019

🐛 Bug Report

What is the problem?

I am trying to build a lambda-backed API gateway, but instead of using the Lambda and API GW constructs, I am using aws_sam.CfnFunction and aws_sam.CfnApi, due to my use case. I was able to build and deploy the lambda function, but when I started adding the API to it using CfnApi, I am unable to get things initialized as expected. When I attempt to initialize the events parameter of the aws_sam.CfnFunction object, the "Properties" key, nested under the "Events" key, is just an empty pair of brackets, and not the expected, initialized "Path", "Method", and "RestApiId" keys. I have attempted to initialize this multiple ways, but none have worked so far.

Reproduction Steps

echo_api = aws_sam.CfnApi(
    self,
    "EchoApi",
    stage_name=<stage-name>,
    definition_body=<swagger-definition>
)

echo_lambda = aws_sam.CfnFunction(
    self,
    "EchoLambda",
    code_uri=aws_sam.CfnFunction.S3LocationProperty(
        bucket=<bucket-string>,
        key=<key-string>,
    runtime=_lambda.Runtime.PYTHON_3_6.to_string(),
    handler="lambda/echo.handler",
    description="An example lambda that echos a message",
    events={
        "Echo": aws_sam.CfnFunction.EventSourceProperty(
            properties=aws_sam.CfnFunction.ApiEventProperty(
                method="get",
                path="/echo",
                rest_api_id=echo_api.logical_id
            ),
            type="Api"
        )
    }
)

Verbose Log

"Handler": "lambda/echo.handler",
"Runtime": "python3.6",
"Description": "An example lambda that echos a message",
"Events": {
    "Echo": {
    "Properties": {},
    "Type": "Api"
    }
}

Environment

  • CDK CLI Version: 1.0.0
  • Module Version: 1.4
  • OS: Linux RHEL
  • Language: Python

Other information

@SherMM SherMM added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 3, 2019
@SomayaB SomayaB added service/aws-serverless @aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-lambda Related to AWS Lambda language/python Related to Python bindings @aws-cdk/aws-sam Related to AWS Serverless Application Model and removed service/aws-serverless labels Sep 3, 2019
@SomayaB SomayaB added the needs-reproduction This issue needs reproduction. label Sep 10, 2019
@nija-at nija-at removed @aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-lambda Related to AWS Lambda labels Sep 24, 2019
@nija-at
Copy link
Contributor

nija-at commented Sep 24, 2019

This seems to be a Python specific problem. I'm able to reproduce this with Python running CDK version 1.9.0 but not Typescript.

This seems to occur for not just ApiEventProperty but also S3EventProperty (see Py snippet below) and probably for others.

This is my working typescript code -

#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import sam = require('@aws-cdk/aws-sam');

const app = new cdk.App();
const stack = new cdk.Stack(app, 'mystack');

new sam.CfnFunction(stack, 'myfunction', {
  codeUri: {
    bucket: 'b', key: 'k'
  },
  runtime: 'nodejs10.x',
  handler: 'index.handler',
  events: {
    'echo': {
      type: 'api',
      properties: {
        method: 'get',
        path: '/echo'
      }
    }
  }
});

app.synth();

And this is the Python CDK app where I could replicate this issue -

#!/usr/bin/env python3

from aws_cdk import (
    core,
    aws_sam as sam
)

app = core.App()
stack = core.Stack(app, "hello-cdk")
api = sam.CfnApi(
    stack,
    "myapi",
    name="myapi",
    stage_name="mystagename",
)

fn = sam.CfnFunction(
    stack,
    "myfn",
    code_uri=sam.CfnFunction.S3LocationProperty(
        bucket="b",
        key="k"
    ),
    runtime="nodejs10.x",
    handler="index.handler",
    events={
        "echo": sam.CfnFunction.EventSourceProperty(
            properties=sam.CfnFunction.ApiEventProperty(
                method="get",
                path="/echo",
                #rest_api_id=api.logical_id
            ),
            type="Api"
        ),
        "s3": sam.CfnFunction.EventSourceProperty(
            properties=sam.CfnFunction.S3EventProperty(
                bucket="b",
                events=["1"]
            ),
            type="aaa"
        )
    }
)

app.synth()

The output CF template had empty Properties bags against the events key

...
        "Handler": "index.handler",
        "Runtime": "nodejs10.x",
        "Events": {
          "echo": {
            "Properties": {},
            "Type": "Api"
          },
          "s3": {
            "Properties": {},
            "Type": "aaa"
          }
        }
...

@nija-at nija-at removed needs-reproduction This issue needs reproduction. needs-triage This issue or PR still needs to be triaged. labels Sep 24, 2019
@RomainMuller RomainMuller self-assigned this Sep 24, 2019
@RomainMuller
Copy link
Contributor

The same problem happens in Java:

package com.myorg;

import java.util.Arrays;

import com.google.common.collect.ImmutableMap;

import software.amazon.awscdk.core.App;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.services.sam.CfnFunction;
import software.amazon.awscdk.services.sam.CfnFunction.ApiEventProperty;
import software.amazon.awscdk.services.sam.CfnFunction.EventSourceProperty;
import software.amazon.awscdk.services.sam.CfnFunction.S3EventProperty;
import software.amazon.awscdk.services.sam.CfnFunction.S3LocationProperty;
import software.amazon.awscdk.services.sam.CfnFunctionProps;

public class HelloApp {
    public static void main(final String argv[]) {
        final App app = new App();
        final Stack stack = new Stack(app, "mystack");

        final CfnFunctionProps functionProps =
                CfnFunctionProps.builder()
                                .codeUri(S3LocationProperty.builder().bucket("b").key("k").build())
                                .runtime("nodejs10.x")
                                .handler("index.handler")
                                .events(ImmutableMap.of("echo",
                                                        EventSourceProperty.builder()
                                                                           .type("Api")
                                                                           .properties(ApiEventProperty.builder()
                                                                                                       .method("get")
                                                                                                       .path("/echo")
                                                                                                       .build())
                                                                           .build(),
                                                        "s3",
                                                        EventSourceProperty.builder()
                                                                           .type("aaa")
                                                                           .properties(S3EventProperty.builder()
                                                                                                      .bucket("b")
                                                                                                      .events(Arrays.asList("1"))
                                                                                                      .build())
                                                                           .build()))
                                .build();
        new CfnFunction(stack, "myfunction", functionProps);

        app.synth();
    }
}

@RomainMuller
Copy link
Contributor

Found this to be caused by a bug in the jsii kernel (aws/jsii#822).

@nija-at nija-at removed the @aws-cdk/aws-sam Related to AWS Serverless Application Model label Sep 25, 2019
@nija-at nija-at removed their assignment Sep 25, 2019
RomainMuller added a commit to aws/jsii that referenced this issue Oct 30, 2019
When deserializing structures in a Union context, the Kernel used to try
options in an arbitrary order (actually - the declaration order which
often is alphanumerically sorted). In addition, it would ignore any
additional property encountered silently.

In cases where several of the union's possibilities had overlapping
required properties, the first attempted option would succeed, even if a
subsequent option would have consumed more properties. A pathologic case
of this is when the first candidate is a type with only optional
properties; as such a type would *always* successfully deserialize
anything, possibly ignoring all properties.

In order to address this, the `structs` can now be passed into the
Kernel by wrapping the object in a decorator box:
```js
{
  "$jsii.struct": {
    "fqn": "fully.qualified.struct.TypeName",
    "data": {
      /* the actual data included in the struct instance */
    }
  }
}
```

This enables "native" languages to correctly communicate the intended
type to the Kernel, so it can make an appropriate deserialization
decision.

The encoding of structs from the Kernel to "native" languages has not
changed: those are still passed by-reference in order to maximize the
compatibility; and to avoid undeterministic behavior in case where union
of structs are returned.

Fixes #822
Fixes aws/aws-cdk#3917
RomainMuller added a commit to aws/jsii that referenced this issue Oct 30, 2019
When deserializing structures in a Union context, the Kernel used to try
options in an arbitrary order (actually - the declaration order which
often is alphanumerically sorted). In addition, it would ignore any
additional property encountered silently.

In cases where several of the union's possibilities had overlapping
required properties, the first attempted option would succeed, even if a
subsequent option would have consumed more properties. A pathologic case
of this is when the first candidate is a type with only optional
properties; as such a type would *always* successfully deserialize
anything, possibly ignoring all properties.

In order to address this, the `structs` can now be passed into the
Kernel by wrapping the object in a decorator box:
```js
{
  "$jsii.struct": {
    "fqn": "fully.qualified.struct.TypeName",
    "data": {
      /* the actual data included in the struct instance */
    }
  }
}
```

This enables "native" languages to correctly communicate the intended
type to the Kernel, so it can make an appropriate deserialization
decision.

The encoding of structs from the Kernel to "native" languages has not
changed: those are still passed by-reference in order to maximize the
compatibility; and to avoid undeterministic behavior in case where union
of structs are returned.

Fixes #822
Fixes aws/aws-cdk#3917
Fixes aws/aws-cdk#2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. language/python Related to Python bindings
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants