В шаблоне SAM для API Gateway есть ошибки в Visual Studio (как часть решения без серверных приложений) - PullRequest
0 голосов
/ 06 мая 2019

Я пытаюсь создать шаблон SAM (serverless.template) в Visual Studio для публикации моего шлюза API. У меня есть несколько ошибок, возникающих при проверке шаблона (в Visual Studio), которые я не могу устранить. После публикации стек развертывается, но нет плана (ов) использования или API-ключей, созданных (или, скорее, они созданы, но недоступны для просмотра в консоли, они отображаются как «недопустимая ссылка», если вы пытаетесь просмотреть их через Cloudformation> Resources, и они вообще не отображаются в консоли API Gateway).

Ошибки:

ServerlessRestApiDeployment26aad1646f является неизвестной ссылкой

    "ServerlessRestApiProdStage": {
        "Type": "AWS::ApiGateway::Stage",
        "Properties": {
            "DeploymentId": {
                "Ref": "ServerlessRestApiDeployment26aad1646f"
            },
            "RestApiId": {
                "Ref": "ServerlessRestApi"
            },
            "StageName": "Prod"
        }
    },

И

ServerlessRestApiProdStage - недопустимый тип для этой ссылки

    "APIGatewayHeartInHandKey": {
        "Type": "AWS::ApiGateway::ApiKey",
        "DependsOn": [
            "ServerlessRestApi",
            "ServerlessRestApiProdStage"
        ],
        "Properties": {
            "Name": "HeartInHandApiKey",
            "Description": "Api Key for Heart In Hand",
            "Enabled": true,
            "GenerateDistinctId": true,
            "StageKeys": [
                {
                    "RestApiId": {
                        "Ref": "ServerlessRestApi"
                    },
                    "StageName": {
                        "Ref": "ServerlessRestApiProdStage"
                    }
                }
            ]
        }
    },

Полный шаблон SAM показан ниже.

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "API Gateway to access InSite data-store",
    "Resources": {
        "Get": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::Get",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "GetTableBasic": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableBasic",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/tables/{tableid}/{columnid}",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "GetColumnList": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetColumnList",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/list/columns/{tableid}",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "GetTableList": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableList",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/list/tables",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "PostClickCollectNotification": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::PostClickCollectNotification",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/datagw/general/webhook/ccnotify",
                            "Method": "POST"
                        }
                    }
                }
            }
        },
        "PostClickCollectStockUpdate": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::PostClickCollectStockUpdate",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/datagw/general/post/sohupdate",
                            "Method": "POST"
                        }
                    }
                }
            }
        },
        "GetTableResponse": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableResponse",
                "Runtime": "dotnetcore2.0",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "datagw/general/table/get/{tableid}",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "ServerlessRestApi": {
            "Type": "AWS::ApiGateway::RestApi",
            "Properties": {
                "Description":"This is a placeholder for the description of this web api",
                "ApiKeySourceType":"HEADER",
                "Body": {
                    "info": {
                        "version": "1.0",
                        "title": {
                            "Ref": "AWS::StackName"
                        }
                    },
                    "paths": {
                        "/list/tables": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableList.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "/list/columns/{tableid}": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetColumnList.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "datagw/general/table/get/{tableid}": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableResponse.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "/": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Get.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "/tables/{tableid}/{columnid}": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableBasic.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        }
                    },
                    "swagger": "2.0"
                }
            }
        },
        "ServerlessRestApiProdStage": {
            "Type": "AWS::ApiGateway::Stage",
            "Properties": {
                "DeploymentId": {
                    "Ref": "ServerlessRestApiDeployment26aad1646f"
                },
                "RestApiId": {
                    "Ref": "ServerlessRestApi"
                },
                "StageName": "Prod"
            }
        },
        "CustomLambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Policies": [
                    {
                        "PolicyName": "lambdaAccessApiKeys",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "apigateway:GET"
                                    ],
                                    "Resource": {
                                        "Fn::Sub": [
                                            "arn:aws:apigateway:ap-southeast-2::/apikeys/${__keyId__}",
                                            {
                                                "__keyId__": {
                                                    "Ref": "APIGatewayHeartInHandKey"
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "GetApiKeyValueLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    "ZipFile": {
                        "Fn::Join": [
                            "\n",
                            [
                                "import json",
                                "import boto3",
                                "client = boto3.client('apigateway')",
                                "def lambda_handler(event, context):",
                                "   response = client.get_api_key(",
                                "   apiKey= event['apiKeyId'],",
                                "   includeValue = True",
                                ")",
                                "   return {",
                                "       'statusCode': 200,",
                                "       'body': response['value']}"
                            ]
                        ]
                    }
                },
                "Handler": "index.lambda_handler",
                "Runtime": "python3.6",
                "Timeout": 30,
                "Role": {
                    "Fn::GetAtt": [
                        "CustomLambdaExecutionRole",
                        "Arn"
                    ]
                }
            }
        },
        "APIGatewayUsagePlanInternal": {
            "Type": "AWS::ApiGateway::UsagePlan",
            "Properties": {
                "ApiStages": [
                    {
                        "ApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "Stage": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ],
                "Description": "Internal Apps Usage Plan",
                "UsagePlanName": "Insite-datagw-InternalAppPlan"
            }
        },
        "APIGatewayUsagePlanExternal": {
            "Type": "AWS::ApiGateway::UsagePlan",
            "Properties": {
                "ApiStages": [
                    {
                        "ApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "Stage": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ],
                "Description": "External Apps Usage Plan",
                "UsagePlanName": "InSite-datagw-ExternalAppPlan"
            }
        },
        "APIGatewayHeartInHandKey": {
            "Type": "AWS::ApiGateway::ApiKey",
            "DependsOn": [
                "ServerlessRestApi",
                "ServerlessRestApiProdStage"
            ],
            "Properties": {
                "Name": "HeartInHandApiKey",
                "Description": "Api Key for Heart In Hand",
                "Enabled": true,
                "GenerateDistinctId": true,
                "StageKeys": [
                    {
                        "RestApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "StageName": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ]
            }
        },
        "LinkHeartInHandKey": {
            "Type": "AWS::ApiGateway::UsagePlanKey",
            "Properties": {
                "KeyId": {
                    "Ref": "APIGatewayHeartInHandKey"
                },
                "KeyType": "API_KEY",
                "UsagePlanId": {
                    "Ref": "APIGatewayUsagePlanInternal"
                }
            }
        },
        "APIGatewayPricelineSiteKey": {
            "Type": "AWS::ApiGateway::ApiKey",
            "DependsOn": [
                "ServerlessRestApi",
                "ServerlessRestApiProdStage"
            ],
            "Properties": {
                "Name": "PricelineSiteApiKey",
                "Description": "Api Key for Priceline Website",
                "Enabled": true,
                "GenerateDistinctId": true,
                "StageKeys": [
                    {
                        "RestApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "StageName": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ]
            }
        },
        "LinkPricelineSiteKey": {
            "Type": "AWS::ApiGateway::UsagePlanKey",
            "Properties": {
                "KeyId": {
                    "Ref": "APIGatewayPricelineSiteKey"
                },
                "KeyType": "API_KEY",
                "UsagePlanId": {
                    "Ref": "APIGatewayUsagePlanInternal"
                }
            }
        }
    },
    "Outputs": {
        "ApiURL": {
            "Description": "API endpoint URL for Prod environment",
            "Value": {
                "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
            }
        }
    }
}

1 Ответ

0 голосов
/ 07 мая 2019

похоже, что вы пытаетесь использовать функции ресурса AWS::Serverless::Api от SAM, не определяя AWS::Serverless::Api в своем шаблоне.

Чтобы исправить возникшие проблемы,похоже, что вам нужно:

Удалить ресурс ServerlessRestApiProdStage

Сэм сгенерирует этот ресурс (и ресурс развертывания) для вас, если вы используете AWS::Serverless::Apiресурс.

Преобразуйте ваш ресурс AWS::ApiGateway::RestApi в ресурс AWS::Serverless::Api:

  • Удалите свойство ApiKeySourceType и добавьте "x-amazon-apigateway-api-key-source" : "HEADER", к чванству,
  • Изменить тип с AWS::ApiGateway::RestApi на AWS::Serverless::Api
        "ServerlessRestApi": {
            "Type": "AWS::Serverless::RestApi",
            "Properties": {
                "Description":"This is a placeholder for the description of this web api",
                "Body": {
                    "info": {
                        "version": "1.0",
                        "title": {
                            "Ref": "AWS::StackName"
                        }
                    },
                    "x-amazon-apigateway-api-key-source" : "HEADER",
                    "paths": {
                    ...
...