aihaddad
aihaddad

Reputation: 101

Getting Invalid mapping expression specified when setting GatewayResponse

The default API Gateway responses come in this form: {"message":$context.error.messageString}. Changing this template via the console, to add wrap it in an "error" object for example, works fine. However, when I try it in CDK, it errors during deployment with Invalid mapping expression specified

Code (notice this is literally the default template):

webServiceApi.addGatewayResponse('4XX', {
   type: apigw.ResponseType.DEFAULT_4XX,
   responseHeaders: COMMON_HEADERS,
   templates: {
      'application/json': '{"message":$context.error.messageString}',
   },
});

This happens with any change as well including all the AWS-written examples I could find. It's under-documented everywhere in AWS: CDK, SDK, API Gateway and CloudFormation so it's very difficult for me to figure out what's wrong there?

CloudFormation Error

Resource handler returned message: "Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: *] (Service: ApiGateway, Status Code: 400, Request ID: [TRUNCATED], Extended Request ID: null)" (RequestToken: [TRUNCATED], HandlerErrorCode: InvalidRequest)

Upvotes: 2

Views: 2475

Answers (2)

Ralph Willgoss
Ralph Willgoss

Reputation: 12183

Similar to the OP, this error is very misleading and indicates a syntax error:

UPDATE_FAILED: RootMethod (AWS::ApiGateway::Method)
Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: true] 
(Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: <snip>; Proxy: null)

For me it was the difference between 'true' and "'true'"

method.response.header.Access-Control-Allow-Credentials: "'true'"

Upvotes: 1

aihaddad
aihaddad

Reputation: 101

Apparantly the error was not with the mapping template, it was the headers needed to "'*'" for the origin instead of "*". That is NOT a useful error message

Upvotes: 7

Related Questions