aws-cdk-lib.aws_cloudfront.CloudFrontWebDistribution

class CloudFrontWebDistribution (construct)

LanguageType name
.NETAmazon.CDK.AWS.CloudFront.CloudFrontWebDistribution
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awscloudfront#CloudFrontWebDistribution
Javasoftware.amazon.awscdk.services.cloudfront.CloudFrontWebDistribution
Pythonaws_cdk.aws_cloudfront.CloudFrontWebDistribution
TypeScript (source)aws-cdk-lib » aws_cloudfront » CloudFrontWebDistribution

Implements IConstruct, IDependable, IResource, IDistribution

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:

const sourceBucket = new s3.Bucket(this, 'Bucket');

const distribution = new cloudfront.CloudFrontWebDistribution(this, 'MyDistribution', {
  originConfigs: [
    {
      s3OriginSource: {
      s3BucketSource: sourceBucket,
      },
      behaviors : [ {isDefaultBehavior: 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.

Example

declare const sourceBucket: s3.Bucket;
const viewerCertificate = cloudfront.ViewerCertificate.fromIamCertificate('MYIAMROLEIDENTIFIER', {
  aliases: ['MYALIAS'],
});

new cloudfront.CloudFrontWebDistribution(this, 'MyCfWebDistribution', {
  originConfigs: [
    {
      s3OriginSource: {
        s3BucketSource: sourceBucket,
      },
      behaviors : [ {isDefaultBehavior: true} ],
    },
  ],
  viewerCertificate: viewerCertificate,
});

Initializer

new CloudFrontWebDistribution(scope: Construct, id: string, props: CloudFrontWebDistributionProps)

Parameters

  • scope Construct
  • id string
  • props CloudFrontWebDistributionProps

Construct Props

NameTypeDescription
originConfigsSourceConfiguration[]The origin configurations for this distribution.
comment?stringA comment for this distribution in the CloudFront console.
defaultRootObject?stringThe default object to serve.
enableIpV6?booleanIf your distribution should have IPv6 enabled.
enabled?booleanEnable or disable the distribution.
errorConfigurations?CustomErrorResponseProperty[]How CloudFront should handle requests that are not successful (eg PageNotFound).
geoRestriction?GeoRestrictionControls the countries in which your content is distributed.
httpVersion?HttpVersionThe max supported HTTP Versions.
loggingConfig?LoggingConfigurationOptional - if we should enable logging.
priceClass?PriceClassThe price class for the distribution (this impacts how many locations CloudFront uses for your distribution, and billing).
viewerCertificate?ViewerCertificateSpecifies 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.
viewerProtocolPolicy?ViewerProtocolPolicyThe default viewer policy for incoming clients.
webACLId?stringUnique identifier that specifies the AWS WAF web ACL to associate with this CloudFront distribution.

originConfigs

Type: SourceConfiguration[]

The origin configurations for this distribution.

Behaviors are a part of the origin.


comment?

Type: string (optional, default: No comment is added to distribution.)

A comment for this distribution in the CloudFront console.


defaultRootObject?

Type: string (optional, default: "index.html" is served.)

The default object to serve.


enableIpV6?

Type: boolean (optional, default: true)

If your distribution should have IPv6 enabled.


enabled?

Type: boolean (optional, default: true)

Enable or disable the distribution.


errorConfigurations?

Type: CustomErrorResponseProperty[] (optional, default: No custom error configuration.)

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.


geoRestriction?

Type: GeoRestriction (optional, default: No geo restriction)

Controls the countries in which your content is distributed.


httpVersion?

Type: HttpVersion (optional, default: HttpVersion.HTTP2)

The max supported HTTP Versions.


loggingConfig?

Type: LoggingConfiguration (optional, default: no logging is enabled by default.)

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.


priceClass?

Type: PriceClass (optional, default: PriceClass.PRICE_CLASS_100 the cheapest option for CloudFront is picked by default.)

The price class for the distribution (this impacts how many locations CloudFront uses for your distribution, and billing).


viewerCertificate?

Type: ViewerCertificate (optional, default: ViewerCertificate.fromCloudFrontDefaultCertificate())

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.

See also: https://aws.amazon.com/premiumsupport/knowledge-center/custom-ssl-certificate-cloudfront/


viewerProtocolPolicy?

Type: ViewerProtocolPolicy (optional, default: RedirectToHTTPs)

The default viewer policy for incoming clients.


webACLId?

Type: string (optional, default: No AWS Web Application Firewall web access control list (web ACL).)

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.

See also: https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateDistribution.html#API_CreateDistribution_RequestParameters.

Properties

NameTypeDescription
distributionDomainNamestringThe domain name created by CloudFront for this distribution.
distributionIdstringThe distribution ID for this distribution.
envResourceEnvironmentThe environment this resource belongs to.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.
loggingBucket?IBucketThe logging bucket for this CloudFront distribution.

distributionDomainName

Type: string

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.)


distributionId

Type: string

The distribution ID for this distribution.


env

Type: ResourceEnvironment

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

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.


loggingBucket?

Type: IBucket (optional)

The logging bucket for this CloudFront distribution.

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

Methods

NameDescription
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
grant(identity, ...actions)Adds an IAM policy statement associated with this distribution to an IAM principal's policy.
grantCreateInvalidation(identity)Grant to create invalidations for this bucket to an IAM principal (Role/Group/User).
toString()Returns a string representation of this construct.
static fromDistributionAttributes(scope, id, attrs)Creates a construct that represents an external (imported) distribution.

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

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).


grant(identity, ...actions)

public grant(identity: IGrantable, ...actions: string[]): Grant

Parameters

  • identity IGrantable — The principal.
  • actions string — The set of actions to allow (i.e. "cloudfront:ListInvalidations").

Returns

  • Grant

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


grantCreateInvalidation(identity)

public grantCreateInvalidation(identity: IGrantable): Grant

Parameters

  • identity IGrantable — The principal.

Returns

  • Grant

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


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromDistributionAttributes(scope, id, attrs)

public static fromDistributionAttributes(scope: Construct, id: string, attrs: CloudFrontWebDistributionAttributes): IDistribution

Parameters

  • scope Construct
  • id string
  • attrs CloudFrontWebDistributionAttributes

Returns

  • IDistribution

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