-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
This seems to be a Python specific problem. I'm able to reproduce this with Python running CDK version This seems to occur for not just 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 ...
"Handler": "index.handler",
"Runtime": "nodejs10.x",
"Events": {
"echo": {
"Properties": {},
"Type": "Api"
},
"s3": {
"Properties": {},
"Type": "aaa"
}
}
... |
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();
}
} |
Found this to be caused by a bug in the |
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
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
🐛 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
Verbose Log
Environment
Other information
The text was updated successfully, but these errors were encountered: