简体   繁体   English

将 CloudFormation 参数传递给 AppSync 解析器

[英]Passing CloudFormation parameters to AppSync Resolver

So I have my CloudFormation template defined to include a Parameters section with several parameters including所以我将我的 CloudFormation 模板定义为包含一个带有多个参数的参数部分,包括

Parameters:

    DefaultLimit: 
        Type: Number

I also have a GraphQL API defined in which I am using AppSync PIPELINE resolvers to run multiple operations in sequence.我还定义了一个 GraphQL API,我在其中使用 AppSync PIPELINE 解析器按顺序运行多个操作。

    QueryResolver:
        Type: AWS::AppSync::Resolver
        DependsOn: AppSyncSchema
        Properties:
            ApiId: !GetAtt [AppSyncAPI, ApiId]
            TypeName: Query
            FieldName: getData
            Kind: PIPELINE
            PipelineConfig:
                Functions:
                    - !GetAtt AuthFunction.FunctionId
                    - !GetAtt ScanDataFunction.FunctionId
            RequestMappingTemplate: |
                { 
                    # Inject value of DefaultLimit in $context object
                }
            ResponseMappingTemplate: "$util.toJson($context.result)"

That all works as expected, except for injecting CFN parameter values in mapping templates.这一切都按预期工作,除了在映射模板中注入 CFN 参数值。

The issue I am having is this -- I would like to pass the value of DefaultLimit to the before RequestMappingTemplate so that the value is available to the ScanDataFunction .我遇到的问题是——我想将DefaultLimit的值传递给before的 RequestMappingTemplate,以便该值可用于ScanDataFunction The goal is for that value be used as the default limit value when the second function does, say a DynamoDB scan operation, and returns paginated results.目标是在第二个 function 执行时将该值用作默认limit值,比如 DynamoDB 扫描操作,并返回分页结果。

My current approach is to hardcode a default value of 20 for limit in the request mapping template of the ScanDataFunction .我当前的方法是在ScanDataFunction的请求映射模板中硬编码limit的默认值 20。 I am using a DynamoDB resolver for this operation.我为此操作使用 DynamoDB 解析器。 Instead, I would like to inject the parameter value because it would give me the flexibility to set different default values for different deployment environments.相反,我想注入参数值,因为它可以让我灵活地为不同的部署环境设置不同的默认值。

Any help with this would be appreciated.对此有任何帮助,我们将不胜感激。

The | | character in YAML starts a block and what you enter indented after that is all treated as text. YAML 中的字符开始一个块,之后您输入的缩进内容都被视为文本。 CloudFormation isn't going to process any of that. CloudFormation 不会处理任何这些。 The solution I have generally seen is to use the Join intrinsic function .我通常看到的解决方案是使用Join intrinsic function It ends up looking pretty bad can be difficult to maintain so I recommend using it sparingly.它最终看起来很糟糕并且难以维护,所以我建议谨慎使用它。 Below is a rough possible example:下面是一个粗略的可能示例:

Parameters:
    DefaultLimit: 
        Type: Number

Resourece:
    QueryResolver:
        Type: AWS::AppSync::Resolver
        DependsOn: AppSyncSchema
        Properties:
            ApiId: !GetAtt [AppSyncAPI, ApiId]
            TypeName: Query
            FieldName: getData
            Kind: PIPELINE
            PipelineConfig:
                Functions:
                    - !GetAtt AuthFunction.FunctionId
                    - !GetAtt ScanDataFunction.FunctionId
            RequestMappingTemplate:
              Fn::Join:
                - ""
                - - "Line 1 of the template\n"
                  - "Line 2 of the template, DefaultList="
                  - Ref: DefaultLimit
                  - "\nLine 3 of the template"
            ResponseMappingTemplate: "$util.toJson($context.result)"

Untested code warning未经测试的代码警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM