how to count the number of methods other than OPTION on AWS API Gateway with python

0 votes

I need to count the number of methods on AWS API Gateway. I'm very new to python scripting. It doesn't care about any path, and it's just that the OPTIONS method doesn't count. GET, POST, PUT, etc. sections (exclude OPTION)

Here I attach the code that I'm currently trying to implement.

import boto3

Create a client for the API Gateway service

client = boto3.client('apigateway')

Get the ID of the API Gateway

api_id = 'my_api_id'

Get the list of resources in the API Gateway

resources = client.get_resources(restApiId=api_id)

Initialize a variable to store the number of methods

method_count = 0

# Get the list of methods for each resource
for resource in resources['items']:
    resource_methods = client.get_methods(restApiId=api_id, resourceId=resource['id'])
    for method in resource_methods['items']:
        if method['httpMethod'] != 'OPTIONS':
            method_count += 1

# Print the number of methods
print(method_count)

I got this error and am still confused about doing the calculations. Please help. Your response is precious.

Traceback (most recent call last):
  File "aws_count_endpoint_api-2.py", line 17, in <module>
    resource_methods = client.get_methods(restApiId=api_id, resourceId=resource['id'])
  File "/home/users/.local/lib/python3.8/site-packages/botocore/client.py", line 646, in __getattr__
    raise AttributeError(
AttributeError: 'APIGateway' object has no attribute 'get_methods'

Feb 14, 2023 in AWS by Ashwini
• 5,430 points
639 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
The error you are seeing is because the boto3 APIGateway client doesn't have a get_methods() method. Instead, you can use the get_method() method to get the details of a specific method, and then check the httpMethod attribute of the method to count the number of non-OPTIONS methods.

Here's an updated version of your script that should work:

import boto3

# Create a client for the API Gateway service
client = boto3.client('apigateway')

# Get the ID of the API Gateway
api_id = 'my_api_id'

# Get the list of resources in the API Gateway
resources = client.get_resources(restApiId=api_id)

# Initialize a variable to store the number of methods
method_count = 0

# Get the list of methods for each resource
for resource in resources['items']:
    resource_methods = client.get_resource(
        restApiId=api_id, resourceId=resource['id'])
    for method in resource_methods['resourceMethods'].values():
        if method['httpMethod'] != 'OPTIONS':
            method_count += 1

# Print the number of methods
print(method_count)

In this script, we use the get_resource() method to get the details of each resource, and then access the resourceMethods attribute to get a dictionary of HTTP methods for that resource. We then iterate over the values of this dictionary and check the httpMethod attribute of each method to count the number of non-OPTIONS methods.

Note that the resourceMethods dictionary only contains the methods that have been defined for a specific resource. If you have any methods defined at the API Gateway level (i.e., not associated with any specific resource), you will need to retrieve these separately using the get_method() method.
answered Feb 16, 2023 by anonymous

edited Mar 5

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

How to add SSL certificate to AWS EC2 with the help of new AWS Certificate Manager service

refer this link  https://aws.amazon.com/certificate-manager/faqs/ You can't install the certificates ...READ MORE

answered Jul 19, 2018 in AWS by Priyaj
• 58,020 points
1,972 views
0 votes
1 answer

How can I just increase the size of my root disk on AWS EC2 for use with Elastic Beanstalk?

This can be done using the following ...READ MORE

answered Nov 12, 2018 in AWS by Archana
• 5,640 points
3,417 views
0 votes
1 answer

How to add SSL certificate to AWS EC2 with the help of new AWS Certificate Manager service

You can't install the certificates created by ...READ MORE

answered Feb 21, 2022 in AWS by Korak
• 5,820 points
854 views
0 votes
1 answer
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,020 points
10,326 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP