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

Python jsii: trying to serialize a function (after forgetting to call it) yields a very obscure error message #4064

Closed
swapnil-jak opened this issue Sep 13, 2019 · 2 comments · Fixed by aws/jsii#824
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. language/python Related to Python bindings

Comments

@swapnil-jak
Copy link

🐛 Bug Report

What is the problem?

I'm using the AWS CDK with Python. I'm trying to add an Ingress Rule to a VPC SecurityGroup using the add_ingress_rule() method within the aws_ec2.SecurityGroup construct. The method signature is as described in the API documentation here

However, when I run cdk deploy the following error is thrown : AttributeError: 'method' object has no attribute '__jsii__type__'

It seems CDK doesn't like the connection = ec2.Port.tcp(443) parameter.

Reproduction Steps

Simple Code as below -

`
#!/usr/bin/env python3

from aws_cdk import (
	core,
	aws_ec2 as ec2
)

my_env = {
	'region': 'eu-west-1'
}

class NetworkStack(core.Stack):

	def __init__(self, scope:core.Construct, id:str, **kwargs):
		super().__init__(scope, id, **kwargs)

		## VPC
		vpc = ec2.Vpc(
			self,
			id="vpc",
			cidr="192.169.0.0/16",
			max_azs = 2,
			nat_gateways = 1,
			subnet_configuration=[
				ec2.SubnetConfiguration(name = 'Web', subnet_type = ec2.SubnetType.PUBLIC, cidr_mask=20),
				ec2.SubnetConfiguration(name = 'App', subnet_type = ec2.SubnetType.PRIVATE, cidr_mask=20),
				ec2.SubnetConfiguration(name = 'Data', subnet_type = ec2.SubnetType.PRIVATE, cidr_mask=20)
			]
		)

		sg_elb = ec2.SecurityGroup(
			self,
			id = "sg_elb",
			vpc = vpc,
			security_group_name = "sg_elb"
		)
		# PROBLEM !!	
		sg_elb.add_ingress_rule(
			peer = ec2.Peer.any_ipv4,
			connection = ec2.Port.tcp(443)
		)

app = core.App()
NetworkStack(app, "cdk-scratch", env=my_env)

app.synth()

`

Verbose Log

$ cdk deploy cdk-scratch --profile blaze-projects Traceback (most recent call last): File "app.py", line 54, in <module> NetworkStack(app, "cdk-scratch", env=my_env) File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_runtime.py", line 66, in __call__ inst = super().__call__(*args, **kwargs) File "app.py", line 50, in __init__ connection = ec2.Port.tcp(443) File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\aws_cdk\aws_ec2\__init__.py", line 19275, in add_ingress_rule return jsii.invoke(self, "addIngressRule", [peer, connection, description, remote_rule]) File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_kernel\__init__.py", line 104, in wrapped return _recursize_dereference(kernel, fn(kernel, *args, **kwargs)) File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_kernel\__init__.py", line 267, in invoke args=_make_reference_for_native(self, args), File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_kernel\__init__.py", line 119, in _make_reference_for_native return [_make_reference_for_native(kernel, i) for i in d] File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_kernel\__init__.py", line 119, in <listcomp> return [_make_reference_for_native(kernel, i) for i in d] File "D:\blazeclan-git-repo\aws-cdk-stuff\cdk-scratch\.env\lib\site-packages\jsii\_kernel\__init__.py", line 130, in _make_reference_for_native d.__jsii__type__ = "Object" AttributeError: 'method' object has no attribute '__jsii__type__' Subprocess exited with error 1 (.env)

Environment

  • CDK CLI Version: 1.8.0 (build 5244f97)
  • Module Version: aws-cdk.aws-ec2 Version 1.8.0
  • OS: Windows 10
  • Language: Python 3.7.2

Other information

@swapnil-jak swapnil-jak added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 13, 2019
@SomayaB SomayaB added language/python Related to Python bindings @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud package/vpc labels Sep 13, 2019
@SomayaB SomayaB added the needs-reproduction This issue needs reproduction. label Sep 13, 2019
@rix0rrr rix0rrr removed needs-reproduction This issue needs reproduction. needs-triage This issue or PR still needs to be triaged. labels Sep 16, 2019
@jsmith97
Copy link

jsmith97 commented Sep 24, 2019

Having exactly the same issue as detailed above, I fixed it with the following syntax..

 bastion_sg.add_ingress_rule(
            peer=ec2.Peer.any_ipv4(),
            connection=ec2.Port.tcp(22),
            description='allow ssh from anywhere to bastion server'
        )

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 25, 2019

Oh, I see. The difference is in peer=ec2.Peer.any_ipv4 vs peer=ec2.Peer.any_ipv4().

Thanks for posting @jsmith97, I completely missed that first time scanning over the code.

Seems that JSII could supply a better error message here.

@rix0rrr rix0rrr changed the title Method add_ingress_rule() for Construct aws_ec2.SecurityGroup returns AttributeError. Python jsii: trying to serialize a function (after forgetting to call it) yields a very obscure error message Sep 25, 2019
rix0rrr pushed a commit to aws/jsii that referenced this issue Sep 26, 2019
A common error seems to be to try to pass a function as a parameter
into a JSII call (have seen 3 occurrences of this in the last week).

Previously, this call would fail when trying to assing the
`__jsii_type__` property to the function object.

Specifically catch this case to give a helpful error message.

Fixes aws/aws-cdk#4064.
rix0rrr added a commit to aws/jsii that referenced this issue Sep 27, 2019
)

A common error seems to be to try to pass a function as a parameter
into a JSII call (have seen 3 occurrences of this in the last week).

Previously, this call would fail when trying to assing the
`__jsii_type__` property to the function object.

Specifically catch this case to give a helpful error message.

Fixes aws/aws-cdk#4064.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud 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.

5 participants