我已经注意到,我的SAM部署正在使用我对自定义API网关响应所做的更改来更新API网关的配置,但实际上并没有将它们部署到API网关阶段来使它们生效。 SAM部署后,如果我进入API网关控制台,选择我的API,打开“操作”菜单,选择“部署API”,选择我的阶段,然后点击部署更改,然后上线就不会出现问题。我是否还需要执行其他步骤才能让SAM部署将更新的配置部署到阶段?
我举了一个重现此问题的示例,这是我的template.yml
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Parameters:
ProjectId:
Type: String
Description: CodeStar projectId used to associate new resources to team members
CodeDeployRole:
Type: String
Description: IAM role to allow AWS CodeDeploy to manage deployment of AWS Lambda functions
Stage:
Type: String
Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
Default: "Prod"
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Sub "${Stage}"
MissingAuthGatewayResponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
ResponseTemplates:
application/json: "{'message': 'Not found.'}"
ResponseType: MISSING_AUTHENTICATION_TOKEN
RestApiId: !Ref MyApi
StatusCode: "403"
HelloWorld:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "awscodestar-${ProjectId}-lambda-HelloWorld"
Handler: index.handler
Runtime: python3.7
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
GetEvent:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: get
LambdaExecutionRole:
Description: Creating service role in IAM for AWS Lambda
Type: AWS::IAM::Role
Properties:
RoleName: !Sub "CodeStar-${ProjectId}-Execution${Stage}"
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
PermissionsBoundary: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/CodeStar_${ProjectId}_PermissionsBoundary"
Outputs:
ApiURL:
Description: "API URL"
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/"
答案 0 :(得分:1)
是的。我发现这种行为也很烦人。让我们添加另一个AWS::ApiGateway::Deployment
类型的资源,并使用RestApiId
属性连接到您的Api。现在,当您sam deploy
时,它将使用提供的StageName
Resources
...
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- HelloWorld
Properties:
RestApiId:
Ref: MyApi
StageName: !Sub "${Stage}"
答案 1 :(得分:1)
我遇到了同样的问题:部署已完成,但未更新阶段。问题是 cloudformation 没有考虑同名的部署,所以如果你修改你的部署名称,它会按预期重新部署。
为了解决这个问题,我们使用了这个插件 https://github.com/paprins/serverless-apigateway-deployment-timestamp,它基本上是在部署名称中添加时间戳。例如对于这个部署
MyDeployment:
Type: AWS::ApiGateway::Deployment
完成的部署将类似于
APIProxyDeployment19878797197:
Type: AWS::ApiGateway::Deployment
其他选项是预处理描述符修改部署名称,但对我来说,该插件是一个更简单的解决方案。