{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "API Gateway resource stack creation using Amplify CLI",

  "Parameters": {
    "authRoleName": {
      "Type": "String"
    },
    "unauthRoleName": {
      "Type": "String"
    },
    "env": {
      "Type": "String"
    },

    "authKsUsersUserPoolId": {
      "Type": "String",
      "Default": "authKsUsersUserPoolId"
    },

    "functionAdminQueries8dc795a4Arn": {
      "Type": "String",
      "Default": "functionAdminQueries8dc795a4Arn"
    },

    "functionAdminQueries8dc795a4Name": {
      "Type": "String",
      "Default": "functionAdminQueries8dc795a4Name"
    }
  },
  "Conditions": {
    "ShouldNotCreateEnvResources": {
      "Fn::Equals": [
        {
          "Ref": "env"
        },
        "NONE"
      ]
    }
  },
  "Resources": {
    "AdminQueries": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Description": "",
        "Name": "AdminQueries",
        "Body": {
          "swagger": "2.0",
          "info": {
            "version": "2018-05-24T17:52:00Z",
            "title": "AdminQueries"
          },
          "host": {
            "Fn::Join": [
              "",
              [
                "apigateway.",
                {
                  "Ref": "AWS::Region"
                },
                ".amazonaws.com"
              ]
            ]
          },
          "basePath": {
            "Fn::If": [
              "ShouldNotCreateEnvResources",
              "/Prod",
              {
                "Fn::Join": [
                  "",
                  [
                    "/",
                    {
                      "Ref": "env"
                    }
                  ]
                ]
              }
            ]
          },
          "schemes": ["https"],
          "paths": {
            "/{proxy+}": {
              "options": {
                "consumes": ["application/json"],
                "produces": ["application/json"],
                "responses": {
                  "200": {
                    "description": "200 response",
                    "schema": {
                      "$ref": "#/definitions/Empty"
                    },
                    "headers": {
                      "Access-Control-Allow-Origin": {
                        "type": "string"
                      },
                      "Access-Control-Allow-Methods": {
                        "type": "string"
                      },
                      "Access-Control-Allow-Headers": {
                        "type": "string"
                      }
                    }
                  }
                },
                "x-amazon-apigateway-integration": {
                  "responses": {
                    "default": {
                      "statusCode": "200",
                      "responseParameters": {
                        "method.response.header.Access-Control-Allow-Methods": "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'",
                        "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
                        "method.response.header.Access-Control-Allow-Origin": "'*'"
                      }
                    }
                  },
                  "passthroughBehavior": "when_no_match",
                  "requestTemplates": {
                    "application/json": "{\"statusCode\": 200}"
                  },
                  "type": "mock"
                }
              },
              "x-amazon-apigateway-any-method": {
                "produces": ["application/json"],
                "parameters": [
                  {
                    "name": "proxy",
                    "in": "path",
                    "required": true,
                    "type": "string"
                  },
                  {
                    "name": "Authorization",
                    "in": "header",
                    "required": false,
                    "type": "string"
                  }
                ],
                "responses": {},
                "security": [
                  {
                    "Cognito": ["aws.cognito.signin.user.admin"]
                  }
                ],
                "x-amazon-apigateway-request-validator": "Validate query string parameters and headers",
                "x-amazon-apigateway-integration": {
                  "uri": {
                    "Fn::Join": [
                      "",
                      [
                        "arn:aws:apigateway:",
                        {
                          "Ref": "AWS::Region"
                        },
                        ":lambda:path/2015-03-31/functions/",
                        {
                          "Ref": "functionAdminQueries8dc795a4Arn"
                        },
                        "/invocations"
                      ]
                    ]
                  },
                  "passthroughBehavior": "when_no_match",
                  "httpMethod": "POST",
                  "cacheNamespace": "n40eb9",
                  "cacheKeyParameters": ["method.request.path.proxy"],
                  "contentHandling": "CONVERT_TO_TEXT",
                  "type": "aws_proxy"
                }
              }
            }
          },
          "securityDefinitions": {
            "Cognito": {
              "type": "apiKey",
              "name": "Authorization",
              "in": "header",
              "x-amazon-apigateway-authtype": "cognito_user_pools",
              "x-amazon-apigateway-authorizer": {
                "providerARNs": [
                  {
                    "Fn::Join": [
                      "",
                      [
                        "arn:aws:cognito-idp:",
                        {
                          "Ref": "AWS::Region"
                        },
                        ":",
                        {
                          "Ref": "AWS::AccountId"
                        },
                        ":userpool/",
                        {
                          "Ref": "authKsUsersUserPoolId"
                        }
                      ]
                    ]
                  }
                ],
                "type": "cognito_user_pools"
              }
            }
          },
          "definitions": {
            "Empty": {
              "type": "object",
              "title": "Empty Schema"
            }
          },
          "x-amazon-apigateway-request-validators": {
            "Validate query string parameters and headers": {
              "validateRequestParameters": true,
              "validateRequestBody": false
            }
          }
        }
      }
    },
    "DeploymentAPIGWAdminQueries": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
        "Description": "The Development stage deployment of your API.",
        "StageName": {
          "Fn::If": [
            "ShouldNotCreateEnvResources",
            "Prod",
            {
              "Ref": "env"
            }
          ]
        },
        "RestApiId": {
          "Ref": "AdminQueries"
        }
      }
    },
    "AdminQueriesAPIGWPolicyForLambda": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Ref": "functionAdminQueries8dc795a4Name"
        },

        "Action": "lambda:InvokeFunction",
        "Principal": "apigateway.amazonaws.com",
        "SourceArn": {
          "Fn::Join": [
            "",
            [
              "arn:aws:execute-api:",
              {
                "Ref": "AWS::Region"
              },
              ":",
              {
                "Ref": "AWS::AccountId"
              },
              ":",
              {
                "Ref": "AdminQueries"
              },
              "/*/*/*"
            ]
          ]
        }
      }
    }
  },
  "Outputs": {
    "RootUrl": {
      "Description": "Root URL of the API gateway",
      "Value": {
        "Fn::Join": [
          "",
          [
            "https://",
            { "Ref": "AdminQueries" },
            ".execute-api.",
            { "Ref": "AWS::Region" },
            ".amazonaws.com/",
            {
              "Fn::If": [
                "ShouldNotCreateEnvResources",
                "Prod",
                { "Ref": "env" }
              ]
            }
          ]
        ]
      }
    },
    "ApiName": {
      "Description": "API Friendly name",
      "Value": "AdminQueries"
    }
  }
}
