SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 0 Java
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.ec2.Hello]
def hello_ec2(ec2_client):
"""
Use the AWS SDK for Python (Boto3) to list the security groups in your account.
This example uses the default settings specified in your shared credentials
and config files.
:param ec2_client: A Boto3 EC2 client. This client provides low-level
access to AWS EC2 services.
"""
print("Hello, Amazon EC2! Let's list up to 10 of your security groups:")
try:
paginator = ec2_client.get_paginator("describe_security_groups")
response_iterator = paginator.paginate(PaginationConfig={'MaxItems': 10}) # List only 10 security groups.
logging.basicConfig(level=logging.INFO) # Enable logging.
for page in response_iterator:
for sg in page["SecurityGroups"]:
logger.info(f"\t{sg['GroupId']}: {sg['GroupName']}")
except ClientError as err:
logger.error("Failed to list security groups.")
if err.response["Error"]["Code"] == "AccessDeniedException":
logger.error("You do not have permission to list security groups.")
raise
if __name__ == "__main__":
hello_ec2(boto3.client("ec2"))
# snippet-end:[python.example_code.ec2.Hello]