Registry / aws / aws-cdk-aws-cloudfront

aws-cdk-aws-cloudfront

JSON →
library1.204.0pypypiunverified

The `aws-cdk-aws-cloudfront` package provides CDK constructs for defining AWS CloudFront distributions and related resources using Python. It's part of the AWS Cloud Development Kit (CDK) v1 ecosystem, enabling infrastructure-as-code for CloudFront. AWS CDK is actively maintained by Amazon Web Services, with frequent minor releases and security updates, often on a weekly or bi-weekly cadence. The current version covered here is 1.204.0.

pip install aws-cdk.core aws-cdk.aws-s3 aws-cdk.aws-cloudfront aws-cdk.aws-cloudfront-origins
INSTALL
IMPORT
SIG · AWS-CDK-AWS-CLOUDF
A
aws-cdk-aws-cloudfront
awspythonv1.204.0
Install
8.9s avg
Import
Disk
77MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.204.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 73.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 8.9s · import 0.000s · 74MB
77MB installed
● package 77MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

core
from aws_cdk import core as cdk
aws_s3
from aws_cdk import aws_s3 as s3
aws_cloudfront
from aws_cdk import aws_cloudfront as cloudfront
import aws_cdk.aws_cloudfront
Commonly imported as 'cloudfront' for brevity and consistency.
aws_cloudfront_origins
from aws_cdk import aws_cloudfront_origins as origins
from aws_cdk.aws_cloudfront import origins
The origins library is a separate package from the core cloudfront constructs.
Distribution
from aws_cdk.aws_cloudfront import Distribution
OriginAccessIdentity
from aws_cdk.aws_cloudfront import OriginAccessIdentity

This quickstart demonstrates how to create a basic AWS CloudFront distribution in CDK v1. It configures an S3 bucket as the origin and uses an Origin Access Identity (OAI) for secure access. The distribution is set to redirect HTTP requests to HTTPS and specifies `index.html` as the default root object. Remember to configure your AWS credentials and `CDK_DEFAULT_ACCOUNT`/`CDK_DEFAULT_REGION` environment variables.

import os from aws_cdk import ( core as cdk, aws_s3 as s3, aws_cloudfront as cloudfront, aws_cloudfront_origins as origins, ) class CloudFrontDistroStack(cdk.Stack): def __init__(self, scope: cdk.App, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Create an S3 bucket to serve as the origin for CloudFront bucket = s3.Bucket( self, "WebsiteBucket", versioned=False, removal_policy=cdk.RemovalPolicy.DESTROY, auto_delete_objects=True # Be cautious with auto_delete_objects in production ) # Create an Origin Access Identity (OAI) for CloudFront to securely access S3 # In AWS CDK v2, Origin Access Control (OAC) is the recommended alternative. oai = cloudfront.OriginAccessIdentity( self, "OAI", comment="Allows CloudFront to access S3 bucket" ) # Grant the OAI read permissions to the S3 bucket bucket.grant_read(oai) # Create a CloudFront Distribution cloudfront.Distribution( self, "MyDistribution", default_behavior=cloudfront.BehaviorOptions( origin=origins.S3Origin(bucket, origin_access_identity=oai), viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS ), default_root_object="index.html" # Assumes an index.html file in your S3 bucket ) app = cdk.App() CloudFrontDistroStack(app, "MyCloudFrontDistroStack", env=cdk.Environment( account=os.environ.get('CDK_DEFAULT_ACCOUNT'), region=os.environ.get('CDK_DEFAULT_REGION') ) ) app.synth()
Debug
Known issues
breakingMigration from AWS CDK v1 to v2 involves significant breaking changes. Package names often change (e.g., `aws_cdk.aws_cloudfront` might become `aws_cdk.aws_cloudfront_alpha` or `aws_cdk.aws_cloudfront` in v2, depending on stability). Construct patterns and parameter names also differ. Projects built with v1 are not directly compatible with v2.
fix
Refer to the official AWS CDK v2 migration guide. This package is for CDK v1; for v2, use `aws-cdk-lib` and its submodules (e.g., `aws_cdk_lib.aws_cloudfront`).
affects: All v1.x versions when migrating to v2.x
gotchaCDK v1 primarily uses Origin Access Identity (OAI) for secure CloudFront access to S3. AWS now recommends Origin Access Control (OAC) as a more secure and flexible alternative, which is the standard in CDK v2. While OAI works for v1, be aware of this shift for future migrations or greenfield v2 projects.
fix
For CDK v1, continue using `cloudfront.OriginAccessIdentity`. For CDK v2, prioritize `aws_cloudfront.CfnOriginAccessControl` or higher-level constructs that integrate OAC.
affects: All v1.x versions
gotchaIncorrect configuration of CloudFront Cache Policies and Origin Request Policies can lead to unexpected caching behavior, missing headers/cookies/query strings at the origin, or security vulnerabilities (e.g., caching sensitive data).
fix
Carefully review the default and custom Cache/Origin Request Policies. Test your CloudFront distribution thoroughly for desired caching behavior and ensure all necessary request attributes (headers, cookies, query strings) are forwarded to the origin when required, but not cached unnecessarily.
affects: All v1.x versions
Upgrade
Version history
1.204.0latest on PyPI · released Jun 19, 2023
Audit
Dependencies
aws-cdk.corerequiredThe foundational CDK library required for all constructs.
aws-cdk.aws-s3requiredCommonly used for S3 bucket origins for CloudFront distributions.
aws-cdk.aws-cloudfront-originsrequiredProvides helper constructs for common origin types (e.g., S3Origin, HttpOrigin).
Agent activity
16 hits · last 30 days
node
16
Resources
aws-cdk-aws-cloudfront — pip install aws-cdk-aws-cloudfront · libregistry