使用serverless.yaml创建Glue表时提示表名不能含大写错误(表名已小写)
问题
我尝试使用Serverless框架创建AWS Glue数据库和表,测试用的YAML代码如下:
org: testorg service: TestAthenaServerless provider: name: aws runtime: nodejs20.x functions: hello: handler: handler.hello events: - httpApi: path: / method: get resources: Resources: ReportsBucket: Type: AWS::S3::Bucket Properties: BucketName: test-bucket TestDatabase: Type: AWS::Glue::Database Properties: DatabaseInput: Name: school_db Description: testing athena serverless CatalogId: Ref AWS::AccountId StudentTable: Type: AWS::Glue::Table Properties: DatabaseName: Ref TestDatabase CatalogId: Ref AWS::AccountId TableInput: Name: student_table Description: Serverless test table TableType: EXTERNAL_TABLE StorageDescriptor: Columns: - {Name: student_name, Type: string} - {Name: student_id, Type: string} - {Name: roll_no, Type: string} - {Name: gender, Type: string} Location: s3://test-bucket/students Parameters: {}
如上所示,表名student_table为全小写,但部署时失败并报错:
✖ An error occurred: StudentTable - Table name [ student_table ] must not contain uppercase characters. ServerlessError2: An error occurred: StudentTable - Table name [ student_table ] must not contain uppercase characters.
我尝试将表名改为单字studenttable、用连字符替代下划线等操作,但仍出现相同错误。请问我哪里操作有误?
解答
问题出在YAML配置的CloudFormation引用语法错误,你没有使用YAML规定的!Ref标签来引用资源/参数,导致Ref被当作字符串的一部分,传递了包含大写字母的无效值给Glue资源,进而触发错误提示。
需要修正以下两处:
修正CatalogId的引用:
将TestDatabase和StudentTable中的CatalogId: Ref AWS::AccountId改为CatalogId: !Ref AWS::AccountId修正DatabaseName的引用:
将StudentTable中的DatabaseName: Ref TestDatabase改为DatabaseName: !Ref TestDatabase
修正后的完整代码:
org: testorg service: TestAthenaServerless provider: name: aws runtime: nodejs20.x functions: hello: handler: handler.hello events: - httpApi: path: / method: get resources: Resources: ReportsBucket: Type: AWS::S3::Bucket Properties: BucketName: test-bucket TestDatabase: Type: AWS::Glue::Database Properties: DatabaseInput: Name: school_db Description: testing athena serverless CatalogId: !Ref AWS::AccountId StudentTable: Type: AWS::Glue::Table Properties: DatabaseName: !Ref TestDatabase CatalogId: !Ref AWS::AccountId TableInput: Name: student_table Description: Serverless test table TableType: EXTERNAL_TABLE StorageDescriptor: Columns: - {Name: student_name, Type: string} - {Name: student_id, Type: string} - {Name: roll_no, Type: string} - {Name: gender, Type: string} Location: s3://test-bucket/students Parameters: {}
错误原因说明:
错误的引用写法会让CloudFormation把Ref TestDatabase当作数据库名字符串处理,这个字符串包含大写字母(R、T、D等),虽然表名本身是全小写,但Glue在验证资源依赖时,会检查关联的数据库名是否符合规范(不能含大写),最终导致系统抛出看似指向表名的错误提示。
内容的提问来源于stack exchange,提问作者raju mb
相关产品推荐
相关产品推荐

