By using AWS re:Post, you agree to the Terms of Use
/"NameError: name 'self' is not defined" in CDK with python/

"NameError: name 'self' is not defined" in CDK with python


I'm just using the sample code from:

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",

HealthlakeStack(app, "HealthLake")

When I run cdk synth on this, I get back:

Traceback (most recent call last):
  File "", line 13, in <module>
    class HealthlakeStack():
  File "", 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 "", line 11, in <module>
    class HealthlakeStack():
  File "", line 15, in HealthlakeStack
    cfn_fHIRDatastore = healthlake.CfnFHIRDatastore("MyCfnFHIRDatastore",
  File "redacted/python/site-packages/jsii/", 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.

asked 14 days ago8 views
1 Answers
Accepted Answer

The problem was that I was trying to create the class in the 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/ file where the class definition belongs.

answered 13 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions