Skip to content

redshiftserverless workgroup error

0

AttributeError: 'CfnWorkgroup' object has no attribute 'attr_arn' indicates that you're using an incorrect attribute name.

For Amazon Redshift Serverless workgroups, the correct attribute to access the ARN is workgroupArn, not attr_arn. The workgroupArn is a string that represents the Amazon Resource Name (ARN) that links to the workgroup.

To fix your code, you should modify it to use:

workgroup_arn = workgroup.workgroupArn AttributeError: 'CfnWorkgroup' object has no attribute 'workgroupArn'

workgroup_arn = workgroup.attrWorkgroupArn AttributeError: 'CfnWorkgroup' object has no attribute 'attrWorkgroupArn'

    workgroup = redshift_sls.CfnWorkgroup(
        self,
        "RedshiftWorkgroup",
        workgroup_name=f"{self.resource_prefix}",
        namespace_name=namespace.namespace_name,
        base_capacity=2,
        enhanced_vpc_routing=True,
        publicly_accessible=False,
        security_group_ids=[redshift_sg.security_group_id],
        subnet_ids=subnet_ids,
    )

    workgroup.add_depends_on(namespace)

    workgroup_arn = workgroup.attrWorkgroupArn


    self.workgroup = workgroup
    self.redshift_workgroup_name = workgroup.workgroup_name
    self.redshift_workgroup_resource = workgroup

    self.workgroup_endpoint = (
        f"{workgroup.workgroup_name}.{self.region}.redshift-serverless.amazonaws.com"
    )
    self.workgroup_port = 5439

   
    # ---------------------------------------------------------

    )

    # attach inline permissions (exact as provided)
    # GetCredentials for the workgroup (dynamic)
    rssl_access_role.add_to_policy(
        iam.PolicyStatement(
            sid="GetCredentials",
            effect=iam.Effect.ALLOW,
            actions=["redshift-serverless:GetCredentials"],
            resources=[workgroup_arn],
        )
    )

    # GetWorkgroup for all
    rssl_access_role.add_to_policy(
        iam.PolicyStatement(
            sid="DescribeWorkgroups",
            effect=iam.Effect.ALLOW,
            actions=["redshift-serverless:GetWorkgroup"],
            resources=["*"],
        )
    )

    # ---------------------------------------------------------
    #  managed policy
  
    # ---------------------------------------------------------
   
    # import the existing SSO role by ARN
    user_role = iam.Role.from_role_arn(
        self,
        "ExistingUserRole",
        sso_user_role_arn,
        mutable=False
    )

    users_policy = iam.ManagedPolicy(
        self,
        "UsersPolicy",
        managed_policy_name=f"{self.resource_prefix}-rsslpolicy",
        statements=[
            iam.PolicyStatement(
                effect=iam.Effect.ALLOW,
                actions=["sts:AssumeRole"],
                resources=[rssl_access_role.role_arn],
            ),
            iam.PolicyStatement(
                effect=iam.Effect.ALLOW,
                actions=[
                    "redshift-serverless:ListNamespaces",
                    "redshift-serverless:ListWorkgroups",
                    "redshift-serverless:GetWorkgroup",
                    "redshift-serverless:GetCredentials"
                ],
                resources=[workgroup_arn],
            ),
            iam.PolicyStatement(
                sid="RedshiftKMSAccess",
                effect=iam.Effect.ALLOW,
                actions=[
                    "kms:Decrypt",
                    "kms:DescribeKey",
                    "kms:GenerateDataKey"
                ],
                resources=[manual_kms_arn],
            )
        ]
    )

   

suggest cdk to refer redshift serverless workgroup arn to its iam role

1 Answer
0

When working with Amazon Redshift Serverless workgroups in AWS CDK, you need to use the correct attribute to reference the workgroup ARN. For a CfnWorkgroup object, the correct attribute to access the ARN is attrWorkgroupArn.

Based on your code, you're trying to use workgroup.attrWorkgroupArn, but getting an AttributeError. This is because in CDK, CloudFormation resources expose their attributes with a specific naming pattern.

For Redshift Serverless workgroups, you should use:

workgroup_arn = workgroup.ref

Or alternatively:

workgroup_arn = workgroup.attr_workgroup_arn

The attr_workgroup_arn follows the CDK's Python naming convention for CloudFormation attributes (camelCase converted to snake_case).

When creating IAM policies that need to reference the workgroup ARN, you should use this attribute to ensure the correct ARN format is used. This is particularly important for permissions like redshift-serverless:GetCredentials and redshift-serverless:GetWorkgroup which require the workgroup ARN as a resource.

Remember that Amazon Redshift Serverless workgroups are collections of compute resources that allow you to isolate and control resources for different workloads or users. Properly referencing the workgroup ARN is essential for setting up the correct IAM permissions to interact with these resources.
Sources
RedshiftServerlessConfiguration - Amazon Bedrock
Workgroup - Amazon Redshift Serverless
Workgroups - Amazon Redshift
Workgroups and namespaces - Amazon Redshift

answered a year ago

EXPERT

reviewed 10 months 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.