Serverless Framework创建AWS SimpleDB域名不符合预期问题排查
Let's break down why your SimpleDB domain is getting an auto-generated name instead of the custom one you specified, while your DynamoDB table works perfectly. The core issue here is that CloudFormation isn't applying your DomainName property correctly—here's how to fix it:
1. First, Verify Your Variable Resolution
The most likely culprit is a problem with how your SIMPLEDB environment variable is being parsed. Let's confirm this first:
- Run
serverless print --stage stagingin your terminal. This will output the final CloudFormation template that Serverless generates. - Look for the
LogSimpleDBTableresource'sDomainNamevalue. It should showgit-pushr-processing-stagingif everything's working right.- If it doesn't, double-check that your
SIMPLEDBvariable is defined directly underprovider.environment(same level asDYNAMODB_TABLE). A common mistake is putting it in thecustomblock or another section where Serverless can't pick it up.
- If it doesn't, double-check that your
2. Test with a Static Domain Name
To rule out variable issues entirely, try hardcoding the domain name temporarily:
resources: Resources: LogSimpleDBTable: Type: "AWS::SDB::Domain" Properties: DomainName: "git-pushr-processing-staging" Description: "SDB Domain to store data log"
Deploy this and check if the SimpleDB domain uses the exact name you specified. If it does, the problem was with variable resolution; if not, we need to look at CloudFormation-specific behavior.
3. Check for CloudFormation Quirks
AWS SimpleDB has a few rules that might be tripping you up:
- Domain Uniqueness: SimpleDB domains are unique per AWS account and region. If you already have a domain named
git-pushr-processing-stagingin your target region, CloudFormation will fail to create a new one—but it should throw an error, not auto-generate a name. Double-check the SimpleDB console to confirm this isn't the case. - CloudFormation/Serverless Version Bugs: Older versions of Serverless Framework or CloudFormation had occasional bugs with SimpleDB resource handling. Update Serverless to the latest version with
npm install -g serverlessand try deploying again.
4. Tweak Your YAML Configuration
Small syntax details in YAML can cause unexpected behavior:
- Ensure proper indentation:
DomainNamemust be indented directly underProperties, aligned withDescription. - Wrap your variable in quotes to avoid any YAML parsing ambiguity:
DomainName: "${self:provider.environment.SIMPLEDB}"
Fixed Example Configuration
Here's your full config with the recommended tweaks:
provider: name: aws stage: ${opt:stage, 'dev'} environment: SIMPLEDB: git-pushr-processing-${opt:stage, self:provider.stage} DYNAMODB_TABLE: git-pushr-processing-${opt:stage, self:provider.stage} resources: Resources: LogSimpleDBTable: Type: "AWS::SDB::Domain" Properties: DomainName: "${self:provider.environment.SIMPLEDB}" Description: "SDB Domain to store data log" TodosDynamoDbTable: Type: 'AWS::DynamoDB::Table' DeletionPolicy: Retain Properties: AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 TableName: ${self:provider.environment.DYNAMODB_TABLE} StreamSpecification: StreamViewType: NEW_AND_OLD_IMAGES
After making these changes, run serverless print again to confirm the DomainName is correctly resolved, then deploy with your original command. This should generate the SimpleDB domain name you expect.
内容的提问来源于stack exchange,提问作者Hassan Jalil

