English 中文(简体)
Enable CORS in AWS API Gateway with aws-cli
原标题:

I m currently writing script to programmatically enable CORS once a resource is added to an API Endpoint on AWS API Gateway. After exploring the put-integration-response function for hours. I almost got a breakthrough, but here is an error I m getting:

An error occurred (BadRequestException) when calling the 
PutIntegrationResponse operation: Invalid mapping expression specified: 
Validation Result: warnings : [], errors : [No method response exists 
for method.]

Here is the script I m using to enable CORS:

aws apigateway put-integration-response --rest-api-id XXXXX --resource 
-id XXXX --http-method GET --status-code 200 --selection-pattern 200 -- 
response-parameters  {"method.reponse.header.Access-Control-Allow- 
Origin": " " * " ", "method.response.header.Access-Control-Allow- 
Headers": " " integration.request.header.Authorization " "} 

The weird thing I found was the AWS documentation seems to be out of date with the current version of the aws-cli It tooks me hours to fix some basic issues I had with the api call.

Will be grateful for any ideas.

Cheers! Nyah

最佳回答

Couple of issues found in your AWS CLI command for aws apigateway put-integration-response

  1. There is a typo mistake

method.reponse.header.Access-Control-Allow-Origin

It must be:

   method.response.header.Access-Control-Allow-Origin
  1. To set a value * to Access-Control-Allow-Origin you need to use " " " * " " " instead of " " * " " In response-parameters you can set method.reponse.header.Access-Control-Allow-Origin, but can not set method.response.header.Access-Control-Allow-Headers
  2. The reason of the error

PutIntegrationResponse operation: Invalid mapping expression specified

is because you are trying to set method.response.header.Access-Control-Allow-Headers in response-parameters

Below should be the final AWS CLI command

aws apigateway put-integration-response --rest-api-id XXXXX --resource-id XXXX --http-method GET --status-code 200 --selection-pattern 200 
--response-parameters  {"method.response.header.Access-Control-Allow-Origin": " " " * " " "} 
问题回答

End to end example of setting up CORS for a MOCK integration for OPTIONS method, equivalent to the "Enable API Gateway CORS" checkbox in the Create Resource dialog from the Management Console.

Order of operation is important, assuming you already have REST-API created with id api1111, and root resource id prid2222

# Create resource with id: res3333
aws apigateway create-resource 
--rest-api-id api1111 
--parent-id prid2222 
--path-part "resourcepath"

aws apigateway put-method --rest-api-id api1111 --resource-id res3333
  --http-method OPTIONS --authorization-type NONE 

aws apigateway put-integration 
  --rest-api-id api1111 --resource-id res3333 --http-method OPTIONS 
  --type MOCK

# Create methods and declare available response parameters 
aws apigateway put-method-response 
  --rest-api-id api1111 --resource-id res3333 --http-method OPTIONS 
  --status-code 200 
  --response-parameters  {"method.response.header.Access-Control-Allow-Headers": true, "method.response.header.Access-Control-Allow-Methods": true, "method.response.header.Access-Control-Allow-Origin": true}   
  --response-models  {"application/json": "Empty"} 

aws apigateway put-integration-response  
  --rest-api-id api1111 --resource-id res3333 --http-method OPTIONS 
  --status-code 200  
  --response-parameters  {"method.response.header.Access-Control-Allow-Methods": "  DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT  ",
        "method.response.header.Access-Control-Allow-Headers": "  Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token  ",
        "method.response.header.Access-Control-Allow-Origin": "  *  "} 

Also notice an alternative method of specifying the values for response paremeters.





相关问题
Mount windows shared drive to MWAA in bootscript

In MWAA startup script sudo yum install samba-client cifs-utils -y sudo mount.cifs //dev/test/drop /mnt/dev/test-o username=testuser,password= pwd ,domain=XX Executing above commonds giving error - ...

How to get Amazon Seller Central orders programmatically?

We have been manually been keying Amazon orders into our system and would like to automate it. However, I can t seem to figure out how to go about it. Their documentation is barely there. There is: ...

Using a CDN like Amazon S3 to control access to media

I want to use Amazon S3/CloudFront to store flash files. These files must be private as they will be accessed by members. This will be done by storing each file with a link to Amazon using a mysql ...

unable to connect to database on AWS

actually I have my website build with Joomla hosted on hostmonster but all Joomla website need a database support to run this database is on AWS configuration files need to be updated for that I ...

Using EC2 Load Balancing with Existing Wordpress Blog

I currently have a virtual dedicated server through Media Temple that I use to run several high traffic Wordpress blogs. Both tend to receive sudden StumbleUpon traffic surges that (I m assuming) ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

热门标签