The CDK Construct Library for AWS CloudFront Origins
Project description
CloudFront Origins for the CDK CloudFront Library
---AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.
For more information on how to migrate, see the Migrating to AWS CDK v2 guide.
This library contains convenience methods for defining origins for a CloudFront distribution. You can use this library to create origins from S3 buckets, Elastic Load Balancing v2 load balancers, or any other domain name.
S3 Bucket
An S3 bucket can be added as an origin. If the bucket is configured as a website endpoint, the distribution can use S3 redirects and S3 custom error documents.
my_bucket = s3.Bucket(self, "myBucket")
cloudfront.Distribution(self, "myDist",
default_behavior=cloudfront.BehaviorOptions(origin=origins.S3Origin(my_bucket))
)
The above will treat the bucket differently based on if IBucket.isWebsite
is set or not. If the bucket is configured as a website, the bucket is
treated as an HTTP origin, and the built-in S3 redirects and error pages can be used. Otherwise, the bucket is handled as a bucket origin and
CloudFront's redirect and error handling will be used. In the latter case, the Origin will create an origin access identity and grant it access to the
underlying bucket. This can be used in conjunction with a bucket that is not public to require that your users access your content using CloudFront
URLs and not S3 URLs directly. Alternatively, a custom origin access identity can be passed to the S3 origin in the properties.
Adding Custom Headers
You can configure CloudFront to add custom headers to the requests that it sends to your origin. These custom headers enable you to send and gather information from your origin that you don’t get with typical viewer requests. These headers can even be customized for each origin. CloudFront supports custom headers for both for custom and Amazon S3 origins.
my_bucket = s3.Bucket(self, "myBucket")
cloudfront.Distribution(self, "myDist",
default_behavior=cloudfront.BehaviorOptions(origin=origins.S3Origin(my_bucket,
custom_headers={
"Foo": "bar"
}
))
)
ELBv2 Load Balancer
An Elastic Load Balancing (ELB) v2 load balancer may be used as an origin. In order for a load balancer to serve as an origin, it must be publicly
accessible (internetFacing
is true). Both Application and Network load balancers are supported.
import aws_cdk.aws_ec2 as ec2
import aws_cdk.aws_elasticloadbalancingv2 as elbv2
# vpc: ec2.Vpc
# Create an application load balancer in a VPC. 'internetFacing' must be 'true'
# for CloudFront to access the load balancer and use it as an origin.
lb = elbv2.ApplicationLoadBalancer(self, "LB",
vpc=vpc,
internet_facing=True
)
cloudfront.Distribution(self, "myDist",
default_behavior=cloudfront.BehaviorOptions(origin=origins.LoadBalancerV2Origin(lb))
)
The origin can also be customized to respond on different ports, have different connection properties, etc.
import aws_cdk.aws_elasticloadbalancingv2 as elbv2
# load_balancer: elbv2.ApplicationLoadBalancer
origin = origins.LoadBalancerV2Origin(load_balancer,
connection_attempts=3,
connection_timeout=Duration.seconds(5),
read_timeout=Duration.seconds(45),
keepalive_timeout=Duration.seconds(45),
protocol_policy=cloudfront.OriginProtocolPolicy.MATCH_VIEWER
)
Note that the readTimeout
and keepaliveTimeout
properties can extend their values over 60 seconds only if a limit increase request for CloudFront origin response timeout
quota has been approved in the target account; otherwise, values over 60 seconds will produce an error at deploy time. Consider that this value is
still limited to a maximum value of 180 seconds, which is a hard limit for that quota.
From an HTTP endpoint
Origins can also be created from any other HTTP endpoint, given the domain name, and optionally, other origin properties.
cloudfront.Distribution(self, "myDist",
default_behavior=cloudfront.BehaviorOptions(origin=origins.HttpOrigin("www.example.com"))
)
See the documentation of @aws-cdk/aws-cloudfront
for more information.
Failover Origins (Origin Groups)
You can set up CloudFront with origin failover for scenarios that require high availability.
To get started, you create an origin group with two origins: a primary and a secondary.
If the primary origin is unavailable, or returns specific HTTP response status codes that indicate a failure,
CloudFront automatically switches to the secondary origin.
You achieve that behavior in the CDK using the OriginGroup
class:
my_bucket = s3.Bucket(self, "myBucket")
cloudfront.Distribution(self, "myDist",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.OriginGroup(
primary_origin=origins.S3Origin(my_bucket),
fallback_origin=origins.HttpOrigin("www.example.com"),
# optional, defaults to: 500, 502, 503 and 504
fallback_status_codes=[404]
)
)
)
From an API Gateway REST API
Origins can be created from an API Gateway REST API. It is recommended to use a regional API in this case.
# api: apigateway.RestApi
cloudfront.Distribution(self, "Distribution",
default_behavior=cloudfront.BehaviorOptions(origin=origins.RestApiOrigin(api))
)
The origin path will automatically be set as the stage name.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aws-cdk.aws-cloudfront-origins-1.204.0.tar.gz
.
File metadata
- Download URL: aws-cdk.aws-cloudfront-origins-1.204.0.tar.gz
- Upload date:
- Size: 54.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df5ea1cd4eb7c6ff4fd80be47d3cd2a3db8868a16c7acaef41b934e76545d7eb |
|
MD5 | 4edead802d3c5884bd318ef78ae37e56 |
|
BLAKE2b-256 | 04ea197c736049ce1504e1683953f64441452ed0322698ea329a367f4bc3cba3 |
File details
Details for the file aws_cdk.aws_cloudfront_origins-1.204.0-py3-none-any.whl
.
File metadata
- Download URL: aws_cdk.aws_cloudfront_origins-1.204.0-py3-none-any.whl
- Upload date:
- Size: 52.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58640374ff55170b6273495383214061e0c8e53dcc61f0bc53773105204368f4 |
|
MD5 | 84fcf94fdb2c02a5b231223e74630ef0 |
|
BLAKE2b-256 | 582a9a3f8abeb7c274d522fe09e7703dc1da40b0546619061af33b70f9016eaa |