Enable CORS in AWS API Gateway with aws-cli - amazon-web-services

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
There is a typo mistake
method.reponse.header.Access-Control-Allow-Origin
It must be:
method.response.header.Access-Control-Allow-Origin
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
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": "'"'"'*'"'"'"}'

Related

How to add headers in existing AWS API gateway integration request

I have created a Integration Request, I am trying to add headers to that request using AWS cloudshell:
aws apigateway update-integration --rest-api-id abcdefgh --resource-id
abcdefghj --http-method POST --request-parameters
{"integration.request.header.X-mobile":"context.authorizer.mobile"}
But it is not working, I have tried PUT integration as well, It was giving the below error:
An error occurred (BadRequestException) when calling the
PutIntegration operation: Enumeration value for HttpMethod must be
non-empty
Try add those command options:
--integration-type eg. AWS_PROXY
--integration-method eg. POST
--integration-uri eg. arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaForSimpleProxy.Arn}/invocations
Here you will find values exmple: Lambda Proxy
I had similar issue in cloudformation, when you are using integration some options are conditional AWS::ApiGateway::Method Integration
Unfortunately aws cli docs doesn't mention this.
Adding
--integration-http-method POST
solved it for me when doing this with AWS CLI.
As per the documentation:
For Lambda integrations, you must use the HTTP method of POST for the
integration request, according to the specification of the Lambda
service action for function invocations.

AWS cannot delete RestApi

On deleting AWS Rest API from UI or through AWS console or terminal with command:
aws apigateway delete-rest-api --rest-api-id 1234123412
(mentioned in aws docs)
I faced the error saying to delete base-path mappings related to RestApi in your domain,
I tried deleting it with the following cmd given in aws docs:
aws apigateway delete-base-path-mapping --domain-name 'api.domain.tld' --base-path 'dev'
I got error: An error occurred (NotFoundException) when calling DeleteBasePathMapping operation. Invalid base path mapping identifier specified
Delete the corresponding domain name from the UI (under 'Custom domain names').
After which, the RestApi can be deleted.

Invalid ARN when performing tagging operation on aws wafv2

I am trying to view and add tags on my web acls using aws wafv2 cli command.
Other command seems to be working properly but I am getting the following error when using ARN in tagging.
The command:
aws wafv2 list-tags-for-resource \
--resource-arn arn:aws:wafv2:us-east-1:<account_id>:global/webacl/<acl_name>/<acl_id>
Output:
An error occurred (WAFInvalidParameterException) when calling the TagResource operation:
Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other
information separated by colons or slashes., field: RESOURCE_ARN, parameter: <arn>
Any idea on why this is happening? I understand that the old version aws waf uses a different format. But I am using wafv2 now so I think I am using the correct URL format already.
Just confirmed the fix. As the comment above indicated, I just need to add the --region parameter and it needs to match the region indicated in the ARN.
Though I am not sure why this is happening as the region in my ~/.aws/config is default to us-east-1 already.

“Lambda function ARN must be in same account” while `aws apigateway put-integration`

I am trying to follow this otherwise excellent post to deploy a Java-based AWS lambda app. I'm scripting it out as instructed in that post. I receive the following error when calling aws apigateway put-integration:
An error occurred (BadRequestException) when calling the
PutIntegration operation: Lambda function ARN must be in same account
I'm trying to script the creation of an API gateway to a lambda function. Here's the full deploy.sh script. I have on my local machine the requisite AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_ACCOUNT_ID environment variables.
I don't know enough to understand what that error is telling me. AFAIK, the access secret and id and account ID are all from the same IAM user.
My AWS CLI Script
aws apigateway put-integration --region ap-south-1 --rest-api-id xxttj6inea --resource-id bgncc0 --http-method GET --type AWS --integration-http-method POST --uri arn:aws:apigateway:ap-south-1:lambda:path/2019-09-20/functions/arn:arn:aws:lambda:ap-south-1:***********:function:Hello/invocations
Thank you very much in advance for any ideas/help.
This error can happen because of two reasons:
Your AWS CLI settings are incorrect. I'll implore you to check the AWS CLI configuration file, and run test API calls for the same to verify if everything is set up appropriately.
Your Lambda function belongs in one account, and your REST API in API GW belongs in another account.

No Integration defined for method error message: x-amazon-apigateway-integration

I am using the the AWS API Gateway importer and I am running the following command to update an existing API:
./aws-api-import.cmd --update API-ID --deploy TEST_STAGE /yourImporterInstallation/tst/resources/swagger/uber.yaml
However, I am returned with the following error:
com.amazonaws.services.apigateway.model.BadRequestException: No integration defined for method (Service: null; Status Code: 400; Error Code: null; Request ID: ID)
I have found out that the sample json (I am using uber.json from /tst/resources/swagger) does not include the "x-amazon-apigateway-integration", but I am not sure how to add it. What is the command? Can you point me to the right resource? My final goal is to find a ways for systematically importing, updating, deleting and deploying an API on AWS.
Thank you
EDIT & ANSWER:
So, I have resolved the issue by adding a Mock Type for the Integration Method (it could have been a Lambda or HTTP, it is just to get it working first)
aws apigateway put-integration --rest-api-id API-ID --resource-id RESOURCE-ID --http-method GET --type MOCK --request-templates '{ "application/json": "{\"statusCode\": 200}" }' --region us-east-1
Then I can successfully run:
./aws-api-import.cmd --update API-ID --deploy TEST_STAGE /yourImporterInstallation/tst/resources/swagger/uber.yaml
However, I have multiple endpoints and GET methods, so I am looking for a command that would allow me to add the same Integration Method to multiple or even all Methods. Can you help?
Thank you