"NameError: name 'self' is not defined" in CDK with python
And I'm adding the import statements not included in that example.
#!/usr/bin/env python3 import aws_cdk as cdk import aws_cdk.aws_healthlake as healthlake from constructs import Construct from healthlake.healthlake_stack import HealthlakeStack app = cdk.App() class HealthlakeStack(): def __init__(self, scope: Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) cfn_fHIRDatastore = healthlake.CfnFHIRDatastore(self, "MyCfnFHIRDatastore", datastore_type_version="R4" ) HealthlakeStack(app, "HealthLake") app.synth()
When I run
cdk synth on this, I get back:
Traceback (most recent call last): File "app.py", line 13, in <module> class HealthlakeStack(): File "app.py", line 17, in HealthlakeStack cfn_fHIRDatastore = healthlake.CfnFHIRDatastore(self, "MyCfnFHIRDatastore", NameError: name 'self' is not defined
And if I remove
self, I get:
Traceback (most recent call last): File "app.py", line 11, in <module> class HealthlakeStack(): File "app.py", line 15, in HealthlakeStack cfn_fHIRDatastore = healthlake.CfnFHIRDatastore("MyCfnFHIRDatastore", File "redacted/python/site-packages/jsii/_runtime.py", line 86, in __call__ inst = super().__call__(*args, **kwargs) TypeError: __init__() missing 1 required positional argument: 'id'
And I tried a few other things. I'm guessing I'm just not nesting this correctly or something else basic to Python.
The problem was that I was trying to create the class in the app.py file, which is where an import statement of that same class was! I just had to revert to the code created by
cdk init app --language python and add my code in the healthlake/healthlake_stack.py file where the class definition belongs.
AWS CDK 2: Package subpath './aws-cloudfront/lib/experimental' is not defined by "exports" in xxx/node_modules/aws-cdk-lib/package.jsonasked 6 months ago
Code Style guide for AWS Cloudformation and CDKAccepted Answerasked 7 months ago
CDK DMS replication instanceAccepted Answerasked 2 years ago
"NameError: name 'self' is not defined" in CDK with pythonAccepted Answerasked 14 days ago
How to add python libraries to lambda using the CDK?Accepted Answerasked 2 years ago
Exception Running the Sample GameLift Code from AWSAccepted Answerasked a year ago
Packaging Java Lamba using CDK is failing in Code Buildasked 3 months ago
How to refer arn for an S3 bucket created using cfn resource in cdk python ?asked a month ago
NameError: name '_mysql' is not defined when trying to use mysql functions in aws lambda in python languageasked 4 months ago
Streaming Video, Face Detection, Sample Python Codeasked 2 years ago