能否使用导出栈的LogicalId?Fn::GetAtt调用报错问题
Let's break down why you're hitting this CloudFormation error and how to fix it step by step:
1. Verify the Logical ID Exists Exactly as Typed
CloudFormation is case-sensitive when it comes to resource Logical IDs. First, double-check that your custom API Gateway resource is defined in the resources: Resources section of your serverless.yml with the exact ID rApiGatewayVehRestApi:
resources: Resources: rApiGatewayVehRestApi: Type: AWS::ApiGateway::RestApi Properties: Name: VehicleServiceAPI # Add any other required properties here (like Description, etc.)
If there's even a tiny typo (e.g., rapigatewayvehrestapi or RApiGatewayVehRestApi), CloudFormation won't recognize the resource.
2. Ensure You're Referencing the Resource Correctly
When using Fn::GetAtt to fetch the RootResourceId, make sure the syntax is correct and the resource lives in the same CloudFormation template (which it should if you're defining it in your Serverless config):
# Example: Using RootResourceId in a child API resource rApiGatewayVehResource: Type: AWS::ApiGateway::Resource Properties: ParentId: Fn::GetAtt: [rApiGatewayVehRestApi, RootResourceId] # Correct syntax RestApiId: Ref: rApiGatewayVehRestApi PathPart: "vehicles"
3. Avoid Conflicts with Serverless's Default API Gateway
If you're using both a custom API Gateway (rApiGatewayVehRestApi) and Serverless's auto-generated default API (created when you add events to your functions), you might be accidentally referencing the wrong resource. To fix this:
- Disable the default API Gateway in your provider config (Serverless v3+):
provider: name: aws runtime: nodejs6.10 # Quick note: This runtime is EOL—upgrade to nodejs18.x or later for security support stage: dev region: us-east-1 # Fill in your target region apiGateway: disableDefaultApi: true # Turns off the auto-generated API - Make sure all your function events explicitly reference your custom API's
RestApiId.
4. If Referencing an Exported Resource (Cross-Stack)
If you're trying to pull the RootResourceId from a previously deployed stack where you exported rApiGatewayVehRestApi, you can't use Fn::GetAtt. Instead, use Fn::ImportValue:
First, confirm the original stack exports the value:
# In the original stack's serverless.yml resources: Outputs: ApiGatewayRootResourceId: Value: Fn::GetAtt: [rApiGatewayVehRestApi, RootResourceId] Export: Name: ${self:service}-${self:stage}-RootResourceId
Then reference it in your current stack:
ParentId: Fn::ImportValue: ${self:service}-${self:stage}-RootResourceId
Common Pitfalls to Watch For
- Typos in Logical IDs (case sensitivity is brutal here!)
- Trying to reference a resource defined in a different stack without using
ImportValue - Forgetting to disable the default API Gateway when using a custom one
- Using an outdated Node.js runtime (nodejs6.10 hasn't been supported since 2019—upgrade to avoid other issues!)
内容的提问来源于stack exchange,提问作者Abirami

