Skip to content
v2.277.0

BehaviorOptions Struct ​

Options for creating a new behavior.

Example ​

ts
// Creates a distribution from a Network Load Balancer
declare const vpc: ec2.Vpc;
// Create a network load balancer in a VPC. 'internetFacing' can be 'false'.
const nlb = new elbv2.NetworkLoadBalancer(this, 'NLB', {
  vpc,
  internetFacing: false,
  vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED },
  securityGroups: [new ec2.SecurityGroup(this, 'NLB-SG', { vpc })],
});
new cloudfront.Distribution(this, 'myDist', {
  defaultBehavior: { origin: origins.VpcOrigin.withNetworkLoadBalancer(nlb) },
});
python
# Creates a distribution from a Network Load Balancer
# vpc: ec2.Vpc

# Create a network load balancer in a VPC. 'internetFacing' can be 'false'.
nlb = elbv2.NetworkLoadBalancer(self, "NLB",
    vpc=vpc,
    internet_facing=False,
    vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_ISOLATED),
    security_groups=[ec2.SecurityGroup(self, "NLB-SG", vpc=vpc)]
)
cloudfront.Distribution(self, "myDist",
    default_behavior=cloudfront.BehaviorOptions(origin=origins.VpcOrigin.with_network_load_balancer(nlb))
)
java
// Creates a distribution from a Network Load Balancer
Vpc vpc;

// Create a network load balancer in a VPC. 'internetFacing' can be 'false'.
NetworkLoadBalancer nlb = NetworkLoadBalancer.Builder.create(this, "NLB")
        .vpc(vpc)
        .internetFacing(false)
        .vpcSubnets(SubnetSelection.builder().subnetType(SubnetType.PRIVATE_ISOLATED).build())
        .securityGroups(List.of(SecurityGroup.Builder.create(this, "NLB-SG").vpc(vpc).build()))
        .build();
Distribution.Builder.create(this, "myDist")
        .defaultBehavior(BehaviorOptions.builder().origin(VpcOrigin.withNetworkLoadBalancer(nlb)).build())
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
// Creates a distribution from a Network Load Balancer
Vpc vpc;

// Create a network load balancer in a VPC. 'internetFacing' can be 'false'.
var nlb = new NetworkLoadBalancer(this, "NLB", new NetworkLoadBalancerProps {
    Vpc = vpc,
    InternetFacing = false,
    VpcSubnets = new SubnetSelection { SubnetType = SubnetType.PRIVATE_ISOLATED },
    SecurityGroups = new [] { new SecurityGroup(this, "NLB-SG", new SecurityGroupProps { Vpc = vpc }) }
});
new Distribution(this, "myDist", new DistributionProps {
    DefaultBehavior = new BehaviorOptions { Origin = VpcOrigin.WithNetworkLoadBalancer(nlb) }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Creates a distribution from a Network Load Balancer
var vpc Vpc

// Create a network load balancer in a VPC. 'internetFacing' can be 'false'.
nlb := elbv2.NewNetworkLoadBalancer(this, jsii.String("NLB"), &NetworkLoadBalancerProps{
	Vpc: Vpc,
	InternetFacing: jsii.Boolean(false),
	VpcSubnets: &SubnetSelection{
		SubnetType: ec2.SubnetType_PRIVATE_ISOLATED,
	},
	SecurityGroups: []ISecurityGroup{
		ec2.NewSecurityGroup(this, jsii.String("NLB-SG"), &SecurityGroupProps{
			Vpc: *Vpc,
		}),
	},
})
cloudfront.NewDistribution(this, jsii.String("myDist"), &DistributionProps{
	DefaultBehavior: &BehaviorOptions{
		Origin: origins.VpcOrigin_WithNetworkLoadBalancer(nlb),
	},
})

Properties ​

allowedMethodsoptionalAllowedMethodsallowed_methodsoptionalAllowedMethodsallowedMethodsoptionalAllowedMethodsAllowedMethodsoptionalAllowedMethodsAllowedMethodsoptionalAllowedMethods = AllowedMethods.ALLOW_GET_HEAD #
HTTP methods to allow for this behavior.
cachedMethodsoptionalCachedMethodscached_methodsoptionalCachedMethodscachedMethodsoptionalCachedMethodsCachedMethodsoptionalCachedMethodsCachedMethodsoptionalCachedMethods = CachedMethods.CACHE_GET_HEAD #
HTTP methods to cache for this behavior.
cachePolicyoptionalICachePolicyRefcache_policyoptionalICachePolicyRefcachePolicyoptionalICachePolicyRefCachePolicyoptionalICachePolicyRefCachePolicyoptionalICachePolicyRef = CachePolicy.CACHING_OPTIMIZED #
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.

compressoptionalbooleancompressoptionalboolcompressoptionalBooleanCompressoptionalboolCompressoptional*bool = true #
Whether you want CloudFront to automatically compress certain files for this cache behavior.
edgeLambdasoptionalEdgeLambda[]edge_lambdasoptionaltyping.List[EdgeLambda]edgeLambdasoptionalList<EdgeLambda>EdgeLambdasoptionalEdgeLambda[]EdgeLambdasoptional*[]EdgeLambda #
The Lambda@Edge functions to invoke before serving the contents.

Default: no Lambda functions will be invoked

enableGrpcoptionalbooleanenable_grpcoptionalboolenableGrpcoptionalBooleanEnableGrpcoptionalboolEnableGrpcoptional*bool = false #
Enables your CloudFront distribution to receive gRPC requests and to proxy them directly to your origins.
notes

If the enableGrpc is set to true, the following restrictions apply:

  • The allowedMethods property must be AllowedMethods.ALLOW_ALL to include POST method because gRPC only supports POST method.
  • The httpVersion property must be HttpVersion.HTTP2 or HttpVersion.HTTP2_AND_3 because gRPC only supports versions including HTTP/2.
  • The edgeLambdas property can't be specified because gRPC is not supported with Lambda@Edge.
functionAssociationsoptionalFunctionAssociation[]function_associationsoptionaltyping.List[FunctionAssociation]functionAssociationsoptionalList<FunctionAssociation>FunctionAssociationsoptionalFunctionAssociation[]FunctionAssociationsoptional*[]FunctionAssociation #
The CloudFront functions to invoke before serving the contents.

Default: no functions will be invoked

originIOriginoriginIOriginoriginIOriginOriginIOriginOriginIOrigin required #
The origin that you want CloudFront to route requests to when they match this behavior.
originRequestPolicyoptionalIOriginRequestPolicyReforigin_request_policyoptionalIOriginRequestPolicyReforiginRequestPolicyoptionalIOriginRequestPolicyRefOriginRequestPolicyoptionalIOriginRequestPolicyRefOriginRequestPolicyoptionalIOriginRequestPolicyRef = none #
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.

realtimeLogConfigoptionalIRealtimeLogConfigRefrealtime_log_configoptionalIRealtimeLogConfigRefrealtimeLogConfigoptionalIRealtimeLogConfigRefRealtimeLogConfigoptionalIRealtimeLogConfigRefRealtimeLogConfigoptionalIRealtimeLogConfigRef = none #
The real-time log configuration to be attached to this cache behavior.
responseHeadersPolicyoptionalIResponseHeadersPolicyRefresponse_headers_policyoptionalIResponseHeadersPolicyRefresponseHeadersPolicyoptionalIResponseHeadersPolicyRefResponseHeadersPolicyoptionalIResponseHeadersPolicyRefResponseHeadersPolicyoptionalIResponseHeadersPolicyRef = none #
The response headers policy for this behavior.

The response headers policy determines which headers are included in responses

smoothStreamingoptionalbooleansmooth_streamingoptionalboolsmoothStreamingoptionalBooleanSmoothStreamingoptionalboolSmoothStreamingoptional*bool = false #
Set this to true to indicate you want to distribute media files in the Microsoft Smooth Streaming format using this behavior.
trustedKeyGroupsoptionalIKeyGroupRef[]trusted_key_groupsoptionaltyping.List[IKeyGroupRef]trustedKeyGroupsoptionalList<IKeyGroupRef>TrustedKeyGroupsoptionalIKeyGroupRef[]TrustedKeyGroupsoptional*[]IKeyGroupRef #
A list of Key Groups that CloudFront can use to validate signed URLs or signed cookies.

Default: no KeyGroups are associated with cache behavior

viewerProtocolPolicyoptionalViewerProtocolPolicyviewer_protocol_policyoptionalViewerProtocolPolicyviewerProtocolPolicyoptionalViewerProtocolPolicyViewerProtocolPolicyoptionalViewerProtocolPolicyViewerProtocolPolicyoptionalViewerProtocolPolicy #
The protocol that viewers can use to access the files controlled by this behavior.

Default: ViewerProtocolPolicy.REDIRECT_TO_HTTPS if the @aws-cdk/aws-cloudfront:defaultViewerProtocolPolicyRedirectToHttps feature flag is enabled, otherwise ViewerProtocolPolicy.ALLOW_ALL