3

I have created an api gateway from my existing api using boto3 import command.

apiClient = boto3.client('apigateway', awsregion)
api_response=apiClient.import_rest_api
(
   failOnWarnings=True,
   body=open('apifileswagger.json', 'rb').read()
)

But i cant modify integration request. I tried with following Boto3 command.

 apiClient = boto3.client('apigateway', awsregion)
 api_response=apiClient.put_integration
 (
   restApiId=apiName,
   resourceId='/api/v1/hub',
   httpMethod='GET',
   integrationHttpMethod='GET',
   type='AWS',
   uri='arn:aws:lambda:us-east-1:141697213513:function:test-lambda',
  )

But I got error like this

Unexpected error: An error occurred () when calling the PutIntegration operation: 

I need to change lambda function region & name using Boto3 command. is it possible? .

if it is possible what is the actual issue with this command?

2 Answers 2

1

In the put_integration() call listed above, your restApiId and resourceId look incorrect. Here's what you should do.

After importing your rest API, check to see if it is available by calling your apiClient's get_rest_apis(). If the API was imported correctly, you should see it listed in the response along with the API's ID (which is generated by AWS). Capture this ID for future operations.

Next, you'll need to look at all of the resources associated with this API by calling your apiClient's get_resources(). Capture the resource ID for the resource you wish to modify.

Using the API ID and resource ID, check to see if an integration config exists by calling your apiClient's get_integration(). If it does exist you can modify the integration request by calling update_integration(); if it does not exist, you need to create a new integration by calling put_integration() and passing the integration request as a parameter.


Here's an example of how that might look in code:

# Import API
api_response1 = apiClient.import_rest_api(failOnWarnings=True, body=open('apifileswagger.json', 'rb').read())
print(api_response1)

# Get API ID
api_response2 = apiClient.get_rest_apis()
for endpoint in api_response2['items']:
    if endpoint['name'] == "YOUR_API_NAME":
        api_ID = endpoint['id']

# Get Resource ID
api_response3 = apiClient.get_resources(restApiId=api_ID)
    for resource in api_response3['items']:
        if resource['path'] == "YOUR_PATH":
            resource_ID = resource['id']

# Check for Existing Integrations
api_response4 = apiClient.get_integration(restApiId=api_ID, resourceId=resource_ID , httpMethod='GET')
print(api_response4)

# Create Integration with Request
integration_request = { 'application/json': '{\r\n  "body" : $input.json(\'$\'),\r\n}' }
api_response5 = apiClient.put_integration(restApiId=api_ID, resourceId=resource_ID , httpMethod='GET', type='AWS', 
    integrationHttpMethod='GET', uri="YOUR_LAMBDA_URI", requestTemplates=integration_request)
print(api_response5)

All the methods listed above are explained in the Boto3 Documentation found here.

Sign up to request clarification or add additional context in comments.

Comments

0

As with most API Gateway updates to API definitions, in order to update an integration request, you have to do a PATCH and pass a body with a patch document using the expected format. See documentation here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.