aws / aws/jsii

aws-cdk.ITaggable: Getting AttributeErrors trying to implement ITaggable in Python

Open
#3,956 1 comment 0 reactions 0 assignees View on GitHub
bug p1 package/tools
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

### Describe the bug

I created a construct with a custom resource to create SecureString SSM Parameters.

```
from aws_cdk.aws_iam import PolicyStatement
from aws_cdk.custom_resources import (
AwsCustomResource,
AwsCustomResourcePolicy,
AwsSdkCall,
PhysicalResourceId,
PhysicalResourceIdReference,
)
from aws_cdk import ITaggable, TagManager, TagType
from constructs import Construct

class SecureStringParameter(ITaggable):
def __init__(
self,
scope: Construct,
id_: str,
parameter_name: str,
parameter_value: str,
kms_key_id: str,
):

super().__init__(scope, id_)
self.tags = TagManager(TagType.MAP, "Custom::SSMSecureStringParameter")

self.custom_resource = AwsCustomResource(
self,
id_,
on_create=AwsSdkCall(
service="SSM",
action="putParameter",
parameters={
"Name": parameter_name,
"Type": "SecureString",
"Value": parameter_value,
"KeyId": kms_key_id,
"Tags": self.tags.rendered_tags,
},
physical_resource_id=PhysicalResourceId.of(parameter_name),
),
on_update=AwsSdkCall(
service="SSM",
action="putParameter",
parameters={
"Name": parameter_name,
"Type": "SecureString",
"Value": parameter_value,
"KeyId": kms_key_id,
"Overwrite": True,
},
physical_resource_id=PhysicalResourceId.of(parameter_name),
),
on_delete=AwsSdkCall(
service="SSM",
action="deleteParameter",
parameters={"Name": PhysicalResourceIdReference()},
),
policy=AwsCustomResourcePolicy.from_statements(
[
PolicyStatement(
actions=[
"ssm:putParameter",
"kms:Encrypt",
"ssm:deleteParameter",
],
resources=["*"],
)
]
),
resource_type="Custom::SSMSecureString",
)
```

I am getting AttributeErrors on `__init__` do to the `tags` attribute. First, I followed the example in the documentation [here](https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk/TagManager.html) and did not include the line

```
self.tags = TagManager(TagType.MAP, "Custom::SSMSecureStringParameter")
```

Then I got the following AttributeError when referencing `self.tags.rendered_tags`

```
AttributeError: 'NoneType' object has no attribute 'rendered_tags'
```

When I added the `self.tags = ...` line I got a different AttributeError

```
AttributeError: can't set attribute
```

Inspecting the Python source code I suspect this is because the `tags` property is Readonly.

### Expected Behavior

I should be able to access the `self.tags` to be able to add them to my AwsSdkCall so the SSM parameter is tagged the same as other resources in my stack.

### Current Behavior

AttributeError and synth/deploy fails

### Reproduction Steps

See construct in bug description above

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.62.2

### Framework Version

_No response_

### Node.js Version

19.5.0

### OS

Mac OS 13.1

### Language

Python

### Language Version

Python (3.9.6)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with the SecureStringParameter construct shown in the issue, then inspect the generated Python bindings for ITaggable, TagManager, and the tags property alongside their TypeScript definitions. Trace both AttributeErrors during initialization and tag rendering; done means the documented Python usage no longer fails and the SSM parameter receives the intended tags.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.