Distribution

class aws_cdk.aws_cloudfront.Distribution(scope, id, *, default_behavior, additional_behaviors=None, certificate=None, comment=None, default_root_object=None, domain_names=None, enabled=None, enable_ipv6=None, enable_logging=None, error_responses=None, geo_restriction=None, http_version=None, log_bucket=None, log_file_prefix=None, log_includes_cookies=None, minimum_protocol_version=None, price_class=None, ssl_support_method=None, web_acl_id=None)

Bases: Resource

A CloudFront distribution with associated origin(s) and caching behavior(s).

ExampleMetadata:

infused

Example:

# Adding an existing Lambda@Edge function created in a different stack
# to a CloudFront distribution.
# s3_bucket: s3.Bucket

function_version = lambda_.Version.from_version_arn(self, "Version", "arn:aws:lambda:us-east-1:123456789012:function:functionName:1")

cloudfront.Distribution(self, "distro",
    default_behavior=cloudfront.BehaviorOptions(
        origin=origins.S3Origin(s3_bucket),
        edge_lambdas=[cloudfront.EdgeLambda(
            function_version=function_version,
            event_type=cloudfront.LambdaEdgeEventType.VIEWER_REQUEST
        )
        ]
    )
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • default_behavior (Union[BehaviorOptions, Dict[str, Any]]) – The default behavior for the distribution.

  • additional_behaviors (Optional[Mapping[str, Union[BehaviorOptions, Dict[str, Any]]]]) – Additional behaviors for the distribution, mapped by the pathPattern that specifies which requests to apply the behavior to. Default: - no additional behaviors are added.

  • certificate (Optional[ICertificate]) – A certificate to associate with the distribution. The certificate must be located in N. Virginia (us-east-1). Default: - the CloudFront wildcard certificate (*.cloudfront.net) will be used.

  • comment (Optional[str]) – Any comments you want to include about the distribution. Default: - no comment

  • default_root_object (Optional[str]) – The object that you want CloudFront to request from your origin (for example, index.html) when a viewer requests the root URL for your distribution. If no default object is set, the request goes to the origin’s root (e.g., example.com/). Default: - no default root object

  • domain_names (Optional[Sequence[str]]) – Alternative domain names for this distribution. If you want to use your own domain name, such as www.example.com, instead of the cloudfront.net domain name, you can add an alternate domain name to your distribution. If you attach a certificate to the distribution, you must add (at least one of) the domain names of the certificate to this list. Default: - The distribution will only support the default generated name (e.g., d111111abcdef8.cloudfront.net)

  • enabled (Optional[bool]) – Enable or disable the distribution. Default: true

  • enable_ipv6 (Optional[bool]) – Whether CloudFront will respond to IPv6 DNS requests with an IPv6 address. If you specify false, CloudFront responds to IPv6 DNS requests with the DNS response code NOERROR and with no IP addresses. This allows viewers to submit a second request, for an IPv4 address for your distribution. Default: true

  • enable_logging (Optional[bool]) – Enable access logging for the distribution. Default: - false, unless logBucket is specified.

  • error_responses (Optional[Sequence[Union[ErrorResponse, Dict[str, Any]]]]) – How CloudFront should handle requests that are not successful (e.g., PageNotFound). Default: - No custom error responses.

  • geo_restriction (Optional[GeoRestriction]) – Controls the countries in which your content is distributed. Default: - No geographic restrictions

  • http_version (Optional[HttpVersion]) – Specify the maximum HTTP version that you want viewers to use to communicate with CloudFront. For viewers and CloudFront to use HTTP/2, viewers must support TLS 1.2 or later, and must support server name identification (SNI). Default: HttpVersion.HTTP2

  • log_bucket (Optional[IBucket]) – The Amazon S3 bucket to store the access logs in. Default: - A bucket is created if enableLogging is true

  • log_file_prefix (Optional[str]) – An optional string that you want CloudFront to prefix to the access log filenames for this distribution. Default: - no prefix

  • log_includes_cookies (Optional[bool]) – Specifies whether you want CloudFront to include cookies in access logs. Default: false

  • minimum_protocol_version (Optional[SecurityPolicyProtocol]) – The minimum version of the SSL protocol that you want CloudFront to use for HTTPS connections. CloudFront serves your objects only to browsers or devices that support at least the SSL version that you specify. Default: - SecurityPolicyProtocol.TLS_V1_2_2021 if the ‘

  • price_class (Optional[PriceClass]) – The price class that corresponds with the maximum price that you want to pay for CloudFront service. If you specify PriceClass_All, CloudFront responds to requests for your objects from all CloudFront edge locations. If you specify a price class other than PriceClass_All, CloudFront serves your objects from the CloudFront edge location that has the lowest latency among the edge locations in your price class. Default: PriceClass.PRICE_CLASS_ALL

  • ssl_support_method (Optional[SSLMethod]) – The SSL method CloudFront will use for your distribution. Server Name Indication (SNI) - is an extension to the TLS computer networking protocol by which a client indicates which hostname it is attempting to connect to at the start of the handshaking process. This allows a server to present multiple certificates on the same IP address and TCP port number and hence allows multiple secure (HTTPS) websites (or any other service over TLS) to be served by the same IP address without requiring all those sites to use the same certificate. CloudFront can use SNI to host multiple distributions on the same IP - which a large majority of clients will support. If your clients cannot support SNI however - CloudFront can use dedicated IPs for your distribution - but there is a prorated monthly charge for using this feature. By default, we use SNI - but you can optionally enable dedicated IPs (VIP). See the CloudFront SSL for more details about pricing : https://aws.amazon.com/cloudfront/custom-ssl-domains/ Default: SSLMethod.SNI

  • web_acl_id (Optional[str]) – Unique identifier that specifies the AWS WAF web ACL to associate with this CloudFront distribution. To specify a web ACL created using the latest version of AWS WAF, use the ACL ARN, for example arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a. To specify a web ACL created using AWS WAF Classic, use the ACL ID, for example 473e64fd-f30b-4765-81a0-62ad96dd167a. Default: - No AWS Web Application Firewall web access control list (web ACL).

Methods

add_behavior(path_pattern, origin, *, allowed_methods=None, cached_methods=None, cache_policy=None, compress=None, edge_lambdas=None, function_associations=None, origin_request_policy=None, response_headers_policy=None, smooth_streaming=None, trusted_key_groups=None, viewer_protocol_policy=None)

Adds a new behavior to this distribution for the given pathPattern.

Parameters:
  • path_pattern (str) – the path pattern (e.g., ‘images/*’) that specifies which requests to apply the behavior to.

  • origin (IOrigin) – the origin to use for this behavior.

  • allowed_methods (Optional[AllowedMethods]) – HTTP methods to allow for this behavior. Default: AllowedMethods.ALLOW_GET_HEAD

  • cached_methods (Optional[CachedMethods]) – HTTP methods to cache for this behavior. Default: CachedMethods.CACHE_GET_HEAD

  • cache_policy (Optional[ICachePolicy]) – The cache policy for this behavior. The cache policy determines what values are included in the cache key, and the time-to-live (TTL) values for the cache. Default: CachePolicy.CACHING_OPTIMIZED

  • compress (Optional[bool]) – Whether you want CloudFront to automatically compress certain files for this cache behavior. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#compressed-content-cloudfront-file-types for file types CloudFront will compress. Default: true

  • edge_lambdas (Optional[Sequence[Union[EdgeLambda, Dict[str, Any]]]]) – The Lambda@Edge functions to invoke before serving the contents. Default: - no Lambda functions will be invoked

  • function_associations (Optional[Sequence[Union[FunctionAssociation, Dict[str, Any]]]]) – The CloudFront functions to invoke before serving the contents. Default: - no functions will be invoked

  • origin_request_policy (Optional[IOriginRequestPolicy]) – The origin request policy for this behavior. The origin request policy determines which values (e.g., headers, cookies) are included in requests that CloudFront sends to the origin. Default: - none

  • response_headers_policy (Optional[IResponseHeadersPolicy]) – The response headers policy for this behavior. The response headers policy determines which headers are included in responses Default: - none

  • smooth_streaming (Optional[bool]) – Set this to true to indicate you want to distribute media files in the Microsoft Smooth Streaming format using this behavior. Default: false

  • trusted_key_groups (Optional[Sequence[IKeyGroup]]) – A list of Key Groups that CloudFront can use to validate signed URLs or signed cookies. Default: - no KeyGroups are associated with cache behavior

  • viewer_protocol_policy (Optional[ViewerProtocolPolicy]) – The protocol that viewers can use to access the files controlled by this behavior. Default: ViewerProtocolPolicy.ALLOW_ALL

Return type:

None

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:

policy (RemovalPolicy) –

Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

distribution_domain_name

The domain name of the Distribution, such as d111111abcdef8.cloudfront.net.

distribution_id

The distribution ID for this distribution.

domain_name

The domain name of the Distribution, such as d111111abcdef8.cloudfront.net.

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

node

The construct tree node associated with this construct.

stack

The stack in which this resource is defined.

Static Methods

classmethod from_distribution_attributes(scope, id, *, distribution_id, domain_name)

Creates a Distribution construct that represents an external (imported) distribution.

Parameters:
  • scope (Construct) –

  • id (str) –

  • distribution_id (str) – The distribution ID for this distribution.

  • domain_name (str) – The generated domain name of the Distribution, such as d111111abcdef8.cloudfront.net.

Return type:

IDistribution

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool