2021-09-07 16:40:55 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to use the AWS SDK for Python (Boto3) with Amazon CloudFront to
perform distribution operations.
"""
import boto3
# snippet-start:[python.example_code.cloudfront.CloudFrontWrapper]
class CloudFrontWrapper :
""" Encapsulates Amazon CloudFront operations. """
2023-10-18 10:35:05 -07:00
2021-09-07 16:40:55 -07:00
def __init__ ( self , cloudfront_client ) :
"""
:param cloudfront_client: A Boto3 CloudFront client
"""
self . cloudfront_client = cloudfront_client
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.cloudfront.CloudFrontWrapper]
# snippet-start:[python.example_code.cloudfront.ListDistributions]
2021-09-07 16:40:55 -07:00
def list_distributions ( self ) :
print ( " CloudFront distributions: \n " )
distributions = self . cloudfront_client . list_distributions ( )
2023-10-18 10:35:05 -07:00
if distributions [ " DistributionList " ] [ " Quantity " ] > 0 :
for distribution in distributions [ " DistributionList " ] [ " Items " ] :
2021-09-07 16:40:55 -07:00
print ( f " Domain: { distribution [ ' DomainName ' ] } " )
print ( f " Distribution Id: { distribution [ ' Id ' ] } " )
2023-10-18 10:35:05 -07:00
print (
f " Certificate Source: "
f " { distribution [ ' ViewerCertificate ' ] [ ' CertificateSource ' ] } "
)
if distribution [ " ViewerCertificate " ] [ " CertificateSource " ] == " acm " :
print (
f " Certificate: { distribution [ ' ViewerCertificate ' ] [ ' Certificate ' ] } "
)
2021-09-07 16:40:55 -07:00
print ( " " )
else :
print ( " No CloudFront distributions detected. " )
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.cloudfront.ListDistributions]
# snippet-start:[python.example_code.cloudfront.UpdateDistribution]
2021-09-07 16:40:55 -07:00
def update_distribution ( self ) :
distribution_id = input (
" This script updates the comment for a CloudFront distribution. \n "
2023-10-18 10:35:05 -07:00
" Enter a CloudFront distribution ID: "
)
2021-09-07 16:40:55 -07:00
distribution_config_response = self . cloudfront_client . get_distribution_config (
2023-10-18 10:35:05 -07:00
Id = distribution_id
)
distribution_config = distribution_config_response [ " DistributionConfig " ]
distribution_etag = distribution_config_response [ " ETag " ]
2021-09-07 16:40:55 -07:00
2023-10-18 10:35:05 -07:00
distribution_config [ " Comment " ] = input (
2021-09-07 16:40:55 -07:00
f " \n The current comment for distribution { distribution_id } is "
f " ' { distribution_config [ ' Comment ' ] } ' . \n "
2023-10-18 10:35:05 -07:00
f " Enter a new comment: "
)
2021-09-07 16:40:55 -07:00
self . cloudfront_client . update_distribution (
2023-10-18 10:35:05 -07:00
DistributionConfig = distribution_config ,
Id = distribution_id ,
IfMatch = distribution_etag ,
)
2021-09-07 16:40:55 -07:00
print ( " Done! " )
2023-10-18 10:35:05 -07:00
2021-09-07 16:40:55 -07:00
# snippet-end:[python.example_code.cloudfront.UpdateDistribution]
def main ( ) :
2023-10-18 10:35:05 -07:00
cloudfront = CloudFrontWrapper ( boto3 . client ( " cloudfront " ) )
2021-09-07 16:40:55 -07:00
cloudfront . list_distributions ( )
cloudfront . update_distribution ( )
2023-10-18 10:35:05 -07:00
if __name__ == " __main__ " :
2021-09-07 16:40:55 -07:00
main ( )