aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

ManagedLayer : 'layerName' failed to satisfy constraint for nestedstack #1985

Open pierrebz opened 2 years ago

pierrebz commented 2 years ago

Hello,

Create Nestedstack by using chalice (api gateway/cdk/psycopg2) cause an issue regarding the creation of layer for packages/modules in vendor/ and chalicelib/

The issue: 1 validation error detected: Value 'xxxxxx-xxxxxx-development-backend/xxxxxxapp-managed-layer' at 'layerName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:[a-zA-Z0-9-]+:lambda:[a-zA-Z0-9-]+:\d{12}:layer:[a-zA-Z0-9-]+)|[a-zA-Z0-9-]+ (Service: AWSLambdaInternal; Status Code: 400; Error Code: ValidationException; Request ID: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx; Proxy: null)

Any way to setup the value of layerName ? Or fix the issue ?

Regards.

isaacsancheza commented 1 year ago

Hello,

Create Nestedstack by using chalice (api gateway/cdk/psycopg2) cause an issue regarding the creation of layer for packages/modules in vendor/ and chalicelib/

The issue: 1 validation error detected: Value 'xxxxxx-xxxxxx-development-backend/xxxxxxapp-managed-layer' at 'layerName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:[a-zA-Z0-9-]+:lambda:[a-zA-Z0-9-]+:\d{12}:layer:[a-zA-Z0-9-]+)|[a-zA-Z0-9-]+ (Service: AWSLambdaInternal; Status Code: 400; Error Code: ValidationException; Request ID: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx; Proxy: null)

Any way to setup the value of layerName ? Or fix the issue ?

Regards.

Hey,

I was facing the same issue. From my understanding, it occurs when using a nested stack then the layer name includes a slash ( / ) causing layer name not satisfy constraint.

My work around was to use managed_layer = get_resource('ManagedLayer') and then manually set layer name managed_layer.layer_name = f'{cdk.Names.unique_resource_name(self)}-managed-layer'

from typing import Any, cast

import aws_cdk as cdk
from aws_cdk import aws_lambda as lambda_
from constructs import Construct
from chalice.cdk import Chalice

class ChaliceNestedStack(cdk.NestedStack):
    def __init__(
            self, 
            scope: Construct, 
            construct_id: str, 
            /, 
            *, 
            source_dir: str = 'chalice',
            stage_config: dict[str, Any],
        ) -> None:
        super().__init__(scope, construct_id)

        self.app = Chalice(
            self, 
            'App', 
            source_dir=source_dir,
            stage_config=stage_config,
        )

        self.managed_layer = cast(lambda_.CfnLayerVersion, self.app.get_resource('ManagedLayer'))

        self.managed_layer.layer_name = f'{cdk.Names.unique_resource_name(self)}-managed-layer'