Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

aws sam - CloudFormation findInMap returns null

I'm creating an AWS SAM template and would like to use mappings to store some commonly used paths. My mappings definitions look like that:

Mappings:
  envs:
    np:
      vpcId: vpc-1412343432
    prod:
      vpcId: vpc-4123121323
  paths:
    example:
      foo1: /{stage}/foo1/
      foo2: /{stage}/foo2/
      foo3: /{stage}/foo3/

and later in the template, I have a function definition with !findInMap

  myFunc:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.myFunc
      Role: !GetAtt myFunc.Arn
      Events:
        foo:
          Type: HttpApi
          Properties:
            Path: !FindInMap [ paths, example, foo1 ]
            Method: get
            ApiId: !Ref fooApi

When I run sam deploy it returns an error Event with id [foo] is invalid. Api Event must have a String specified for 'Path'. It looks that findInMap returns empty value. Do you know what am I doing wrong?

question from:https://stackoverflow.com/questions/65937747/cloudformation-findinmap-returns-null

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think the problem is with !FindInMap. I quickly validated it using following addition to my template.

Outputs:
  MaverickOutput:
    Value: !FindInMap [ paths, example, foo1 ]

The value (/{stage}/foo1/) was reflected in the CloudFormation stack output post sam deploy.

Coming to the reason why it did not work with the Api event. If you follow this link to official docs, you can see this.

The object describing an Api event source type. If an AWS::Serverless::Api resource is defined, the path and method values must correspond to an operation in the OpenAPI definition of the API.

This combined with the error (Api Event must have a String specified for 'Path'.") that you are receiving, I think it is fair to assume that this resource does not accept references which will be resolved during deployment time.

PS: I could not find any documentation to support my theory, but another fact which supports this is that the maps can also store numbers and there won't be any way to know what the final value is going to be until actual deployment takes place. Therefore, if deployment time references were allowed, you might actually refer to a number for the Path value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...