简体   繁体   中英

Passing CloudFormation parameters to AppSync Resolver

So I have my CloudFormation template defined to include a Parameters section with several parameters including

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.

    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.

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 . 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.

My current approach is to hardcode a default value of 20 for limit in the request mapping template of the ScanDataFunction . I am using a DynamoDB resolver for this operation. 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. CloudFormation isn't going to process any of that. The solution I have generally seen is to use the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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