Reputation: 79
I have an API with AWS API Gateway and Go Lambda functions.
I have an issue, each time when i deploy new version of SAM it Overwrite Gateway responses on API Gateway.
I tried to put it in SAM template but it seems it does not work or I'm not doing it correctly.
Here is SAM template:
Resources:
MyGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
DefaultAuthorizer: AdminTokenAuthorizer
AddDefaultAuthorizerToCorsPreflight: False
Authorizers:
AdminTokenAuthorizer:
FunctionArn: !GetAtt AdminAuthorizerFunction.Arn
DeviceTokenAuthorizer:
FunctionArn: !GetAtt DeviceAuthorizerFunction.Arn
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId: !Ref MyGateway
GatewayResponseDefaultUnauthorized:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: UNAUTHORIZED
RestApiId: !Ref MyGateway
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_5XX
RestApiId: !Ref MyGateway
Here is screenshot for Response on AWS console:
As you can seeheaders are empty for DEFAULT_4XX, UNAUTHORIZED and DEFAULT_5XX.
What i notice is when i rename GatewayResponse resources in template, for example if i rename GatewayResponseDefault4XX to GatewayResponseDefault4XXRename it will set headers correctly for DEFAULT_4XX but if i change something else and deploy it again without changes on GatewayResponses resource it will remove headers from all GatewayResponses.
Upvotes: 4
Views: 3072
Reputation: 7331
You are mixing the parameters. To do this you need to pass it through the parameter GatewayResponses
.
Here's a basic example:
Resources:
MyGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
GatewayResponses:
Default4XX:
ResponseTemplates:
"application/json": {"message": "A super different message"}
Default5XX:
StatusCode: 500 # You can set a the status code too
ResponseTemplates:
"application/json": {"message": "And now... something different"}
Upvotes: 1