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

0

I'm just using the sample code from: https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_healthlake/CfnFHIRDatastore.html#aws_cdk.aws_healthlake.CfnFHIRDatastore.add_deletion_override

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.

AWS
질문됨 2년 전1186회 조회
1개 답변
0
수락된 답변

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
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠