CloudFrontWebDistribution

class aws_cdk.aws_cloudfront.CloudFrontWebDistribution(scope, id, *, origin_configs, comment=None, default_root_object=None, enabled=None, enable_ip_v6=None, error_configurations=None, geo_restriction=None, http_version=None, logging_config=None, price_class=None, viewer_certificate=None, viewer_protocol_policy=None, web_acl_id=None)

Bases: Resource

Amazon CloudFront is a global content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to your viewers with low latency and high transfer speeds.

CloudFront fronts user provided content and caches it at edge locations across the world.

Here’s how you can use this construct:

source_bucket = s3.Bucket(self, "Bucket")

distribution = cloudfront.CloudFrontWebDistribution(self, "MyDistribution",
    origin_configs=[cloudfront.SourceConfiguration(
        s3_origin_source=cloudfront.S3OriginConfig(
            s3_bucket_source=source_bucket
        ),
        behaviors=[cloudfront.Behavior(is_default_behavior=True)]
    )
    ]
)

This will create a CloudFront distribution that uses your S3Bucket as it’s origin.

You can customize the distribution using additional properties from the CloudFrontWebDistributionProps interface.

Resource:

AWS::CloudFront::Distribution

ExampleMetadata:

infused

Example:

# source_bucket: s3.Bucket

viewer_certificate = cloudfront.ViewerCertificate.from_iam_certificate("MYIAMROLEIDENTIFIER",
    aliases=["MYALIAS"]
)

cloudfront.CloudFrontWebDistribution(self, "MyCfWebDistribution",
    origin_configs=[cloudfront.SourceConfiguration(
        s3_origin_source=cloudfront.S3OriginConfig(
            s3_bucket_source=source_bucket
        ),
        behaviors=[cloudfront.Behavior(is_default_behavior=True)]
    )
    ],
    viewer_certificate=viewer_certificate
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • origin_configs (Sequence[Union[SourceConfiguration, Dict[str, Any]]]) – The origin configurations for this distribution. Behaviors are a part of the origin.

  • comment (Optional[str]) – A comment for this distribution in the CloudFront console. Default: - No comment is added to distribution.

  • default_root_object (Optional[str]) – The default object to serve. Default: - “index.html” is served.

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

  • enable_ip_v6 (Optional[bool]) – If your distribution should have IPv6 enabled. Default: true

  • error_configurations (Optional[Sequence[Union[CustomErrorResponseProperty, Dict[str, Any]]]]) – How CloudFront should handle requests that are not successful (eg PageNotFound). By default, CloudFront does not replace HTTP status codes in the 4xx and 5xx range with custom error messages. CloudFront does not cache HTTP status codes. Default: - No custom error configuration.

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

  • http_version (Optional[HttpVersion]) – The max supported HTTP Versions. Default: HttpVersion.HTTP2

  • logging_config (Union[LoggingConfiguration, Dict[str, Any], None]) – Optional - if we should enable logging. You can pass an empty object ({}) to have us auto create a bucket for logging. Omission of this property indicates no logging is to be enabled. Default: - no logging is enabled by default.

  • price_class (Optional[PriceClass]) – The price class for the distribution (this impacts how many locations CloudFront uses for your distribution, and billing). Default: PriceClass.PRICE_CLASS_100 the cheapest option for CloudFront is picked by default.

  • viewer_certificate (Optional[ViewerCertificate]) – Specifies whether you want viewers to use HTTP or HTTPS to request your objects, whether you’re using an alternate domain name with HTTPS, and if so, if you’re using AWS Certificate Manager (ACM) or a third-party certificate authority. Default: ViewerCertificate.fromCloudFrontDefaultCertificate()

  • viewer_protocol_policy (Optional[ViewerProtocolPolicy]) – The default viewer policy for incoming clients. Default: RedirectToHTTPs

  • 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

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

grant(identity, *actions)

Adds an IAM policy statement associated with this distribution to an IAM principal’s policy.

Parameters:
  • identity (IGrantable) – The principal.

  • actions (str) – The set of actions to allow (i.e. “cloudfront:ListInvalidations”).

Return type:

Grant

grant_create_invalidation(identity)

Grant to create invalidations for this bucket to an IAM principal (Role/Group/User).

Parameters:

identity (IGrantable) – The principal.

Return type:

Grant

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

distribution_domain_name

The domain name created by CloudFront for this distribution.

If you are using aliases for your distribution, this is the domainName your DNS records should point to. (In Route53, you could create an ALIAS record to this value, for example.)

distribution_id

The distribution ID for this distribution.

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.

logging_bucket

The logging bucket for this CloudFront distribution.

If logging is not enabled for this distribution - this property will be undefined.

node

The tree node.

stack

The stack in which this resource is defined.

Static Methods

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

Creates a 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)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters:

construct (IConstruct) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool