How to add headers in existing AWS API gateway integration request - amazon-web-services

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.

Related

AWS API Gateway integration with AWS Event Bridge(Cloudwatch Events) in Cloudformation Script

Original Requirement:
Create a route/path on AWS Api Gateway which connects API Gateway directly to AWS Event Bridge (Cloudwatch Events) and puts/pushes event on an event bus of it.
Was able to create it and executes just fine when done from AWS Console.
Actual Problem:
When writing the AWS Cloudformation script for this API Gateway, it looks like this:
EventsPostMethod:
Type: AWS::ApiGateway::Method
Properties:
ResourceId:
Ref: EventsResource
RestApiId:
Ref: RestAPI
HttpMethod: POST
AuthorizationType: NONE
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:cloudwatchEvents:action/PutEvents
RequestParameters:
integration.request.header.X-Amz-Target: "'AWSEvents.PutEvents'"
RequestTemplate:
some-script-here...
Notice the Uri value:
"arn:aws:apigateway:${AWS::Region}:cloudwatchEvents:action/PutEvents"
arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}
According to AWS Docs the value of uri should be following:
For AWS or AWS_PROXY integrations, the URI is of the form arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}. Here, {Region} is the API Gateway region (e.g., us-east-1); {service} is the name of the integrated AWS service (e.g., s3); and {subdomain} is a designated subdomain supported by certain AWS service for fast host-name lookup. action can be used for an AWS service action-based API, using an Action={name}&{p1}={v1}&p2={v2}... query string. The ensuing {service_api} refers to a supported action {name} plus any required input parameters. Alternatively, path can be used for an AWS service path-based API. The ensuing service_api refers to the path to an AWS service resource, including the region of the integrated AWS service, if applicable. For example, for integration with the S3 API of GetObject, the uri can be either arn:aws:apigateway:us-west-2:s3:action/GetObject&Bucket={bucket}&Key={key} or arn:aws:apigateway:us-west-2:s3:path/{bucket}/{key}
You must have noticed that I replaced the service with cloudwatchEvents in the above mentioned uri.
Now, error Given by AWS Cloudformation Console during Publish of API Gateway:
AWS Service of type cloudwatchEvents not supported (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 07bae22c-d198-4595-8de9-6ea23763eff5; Proxy: null)
Now I have tried replacing service with
cloudwatch
eventBridge
cloudwatchEvent
event-bus
This is the real problem. What should I place in service in uri so that it accepts ?
Based on the comments,
The URI should be something like below for events:
arn:aws:apigateway:${AWS::Region}:events:action/PutEvents

“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.

API Gateway export API definition with Postman

I was checking out a couple methods from amazon-api-gateway-developer-guide and I stumbled with api-gateway-export-api, I managed to get the AWSCLI command (aws apigateway get-export --parameters extensions='apigateway' --rest-api-id abcdefg123 --stage-name dev --export-type swagger latestswagger2.json --profile profile --region us-east-1) working by adding the --profile and --region parameters, but for whatever reason I can't get the Request URL to return the API definition response.
I am trying to do the following couple things (Postman):
GET request with the URL: https://apigateway.us-east-1.amazonaws.com/restapis/abcdefg123/stages/dev/exports/oas30
Add authorization type AWS-Signature to the request with all the
parameters filled (AccessKey, SecretKey, AWS Region,
Service Name and Session Token)
The Authorization, x-Amz-Date and x-Amz-Security token are generated successfully, as far as I can tell
I am also sending a the headers Host (apigateway.us-east-1.amazonaws.com) and Accept (application/yaml)
This results in the following error:
{"logref":"2734hu2r2873","message":"User:
arn:aws:sts::7216832187:assumed-role/DEVELOPER/xxxx is not authorized to perform:
apigateway:GET on resource:
arn:aws:apigateway:us-east-1::/restapis/abcdefg123 /stages/dev/exports/oas30"}
I was actually getting the same error with the AWSCLI command before I added the --profile and --region parameters. I already checked out a couple posts about issues like mine, this one is an example export swagger api definition from api gateway via http request?, but I am doing basically the same thing (sending the same headers, same host and URL) and getting this error. I don't think my access key, secret key, token or any of that information might be wrong... because it's the credentials I use for the AWSCLI command.
Thank you for the taking the time to read and/or reply to my post, I really appreciate any feedback in anything.
The point of trying to get this request to work is that I wanted to add it as an HTTP proxy to an API, unfortunately I couldn't get it to work.
My problem was that I have a couple profiles in my session and each one of them has different permissions, I found no way to specify the profile I wanted to use in the request and thus it caused the error mentioned above.
In the end I opted to write a Lambda function with proxy integration and everything went fine, after giving the lambda profile the apigateway:GET permission.
If I have a misconception about anything I wrote about or I am using a term in an incorrect way please correct me, I am still fairly new to AWS and have a lot to learn!

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
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": "'"'"'*'"'"'"}'

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