为了启用AWS_IAM auth和CORS,我制作了sam模板,如下所示。
但是,我说错了
创建更改集失败:服务生ChangeSetCreateComplete失败:服务生遇到终端故障状态:失败。原因:转换AWS:: Serverless -2016-10-31失败:无效的Serverless应用程序规范文档。发现错误数: 1. id ListFunction的资源无效。id ListFunctionCors的事件无效。无法在路径/list的API方法选项上设置Authorizers,因为相关API没有定义任何授权器.
这里怎么了?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ListApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
DefaultAuthorizer: AWS_IAM
Cors:
AllowMethods: "'*'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
ListFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: app_src/
Handler: app.lambda_handler
Runtime: python3.7
Events:
ListFunction:
Type: Api
Properties:
RestApiId: !Ref ListApi
Path: /list
Method: GET
ListFunctionCors:
Type: Api
Properties:
RestApiId: !Ref ListApi
Path: /list
Method: OPTIONS
Auth:
Authorizer: 'NONE'发布于 2019-05-31 03:41:19
只是遇到了同样的问题,并找到了答案。根据https://github.com/awslabs/serverless-application-model/issues/781,该函数事件应该是:
ListFunctionCors:
Type: Api
Properties:
RestApiId: !Ref ListApi
Path: /list
Method: OPTIONS
Auth:
Authorizer: null我是Authorizer: null,不是Authorizer: 'NONE'。
希望这能有所帮助!
发布于 2019-07-04 15:20:42
我为这个问题找到的解决方案更多的是一个解决办法。您得到的错误消息的来源似乎是这里。
据我理解,当您选择拥有AWS_IAM以外的方法授权器时,不允许有3种情况: 1.没有定义授权者2.选择定义的授权人中不存在的NONE以外的授权人3.选择NONE作为授权器,而不设置默认的授权器。
以上情况不处理AWS_IAM授权人的情况。它是唯一类型的授权程序,它只被设置为DefaultAuthorizer,而不必在Authorizers字典中列出它。
因此,这里的解决方法是定义一个虚拟的自定义授权程序,并且永远不要使用它。
在你的例子中,这样做是可以的:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ListApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
DefaultAuthorizer: AWS_IAM
Authorizers:
Dummy:
FunctionArn: !GetAtt ListFunction.Arn
Cors:
AllowMethods: "'*'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
ListFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: app_src/
Handler: app.lambda_handler
Runtime: python3.7
Events:
ListFunction:
Type: Api
Properties:
RestApiId: !Ref ListApi
Path: /list
Method: GET
ListFunctionCors:
Type: Api
Properties:
RestApiId: !Ref ListApi
Path: /list
Method: OPTIONS
Auth:
Authorizer: 'NONE'我在您的API中添加了一个定制的lambda授权器Dummy,它从未被使用过。我使用了您已经定义的lambda的arn,但是您可以创建一个占位符lambda来代替。
底线:当只使用Authorizer: 'NONE'时,您目前可以在SAM中使用DefaultAuthorizer: AWS_IAM的唯一方法是将任何其他授权程序添加到API中。
注意: Authorizer的null值是在最初的GitHub问题中提出的,但是在实现过程中,它们得到了NONE值,正如文档中所描述的那样。
https://stackoverflow.com/questions/55946030
复制相似问题