ResponseHeadersPolicy

class aws_cdk.aws_cloudfront.ResponseHeadersPolicy(scope, id, *, comment=None, cors_behavior=None, custom_headers_behavior=None, remove_headers=None, response_headers_policy_name=None, security_headers_behavior=None, server_timing_sampling_rate=None)

Bases: Resource

A Response Headers Policy configuration.

Resource:

AWS::CloudFront::ResponseHeadersPolicy

ExampleMetadata:

infused

Example:

# Using an existing managed response headers policy
# bucket_origin: origins.S3Origin

cloudfront.Distribution(self, "myDistManagedPolicy",
    default_behavior=cloudfront.BehaviorOptions(
        origin=bucket_origin,
        response_headers_policy=cloudfront.ResponseHeadersPolicy.CORS_ALLOW_ALL_ORIGINS
    )
)

# Creating a custom response headers policy -- all parameters optional
my_response_headers_policy = cloudfront.ResponseHeadersPolicy(self, "ResponseHeadersPolicy",
    response_headers_policy_name="MyPolicy",
    comment="A default policy",
    cors_behavior=cloudfront.ResponseHeadersCorsBehavior(
        access_control_allow_credentials=False,
        access_control_allow_headers=["X-Custom-Header-1", "X-Custom-Header-2"],
        access_control_allow_methods=["GET", "POST"],
        access_control_allow_origins=["*"],
        access_control_expose_headers=["X-Custom-Header-1", "X-Custom-Header-2"],
        access_control_max_age=Duration.seconds(600),
        origin_override=True
    ),
    custom_headers_behavior=cloudfront.ResponseCustomHeadersBehavior(
        custom_headers=[cloudfront.ResponseCustomHeader(header="X-Amz-Date", value="some-value", override=True), cloudfront.ResponseCustomHeader(header="X-Amz-Security-Token", value="some-value", override=False)
        ]
    ),
    security_headers_behavior=cloudfront.ResponseSecurityHeadersBehavior(
        content_security_policy=cloudfront.ResponseHeadersContentSecurityPolicy(content_security_policy="default-src https:;", override=True),
        content_type_options=cloudfront.ResponseHeadersContentTypeOptions(override=True),
        frame_options=cloudfront.ResponseHeadersFrameOptions(frame_option=cloudfront.HeadersFrameOption.DENY, override=True),
        referrer_policy=cloudfront.ResponseHeadersReferrerPolicy(referrer_policy=cloudfront.HeadersReferrerPolicy.NO_REFERRER, override=True),
        strict_transport_security=cloudfront.ResponseHeadersStrictTransportSecurity(access_control_max_age=Duration.seconds(600), include_subdomains=True, override=True),
        xss_protection=cloudfront.ResponseHeadersXSSProtection(protection=True, mode_block=True, report_uri="https://example.com/csp-report", override=True)
    ),
    remove_headers=["Server"],
    server_timing_sampling_rate=50
)
cloudfront.Distribution(self, "myDistCustomPolicy",
    default_behavior=cloudfront.BehaviorOptions(
        origin=bucket_origin,
        response_headers_policy=my_response_headers_policy
    )
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • comment (Optional[str]) – A comment to describe the response headers policy. Default: - no comment

  • cors_behavior (Union[ResponseHeadersCorsBehavior, Dict[str, Any], None]) – A configuration for a set of HTTP response headers that are used for cross-origin resource sharing (CORS). Default: - no cors behavior

  • custom_headers_behavior (Union[ResponseCustomHeadersBehavior, Dict[str, Any], None]) – A configuration for a set of custom HTTP response headers. Default: - no custom headers behavior

  • remove_headers (Optional[Sequence[str]]) – A list of HTTP response headers that CloudFront removes from HTTP responses that it sends to viewers. Default: - no headers are removed

  • response_headers_policy_name (Optional[str]) – A unique name to identify the response headers policy. Default: - generated from the id

  • security_headers_behavior (Union[ResponseSecurityHeadersBehavior, Dict[str, Any], None]) – A configuration for a set of security-related HTTP response headers. Default: - no security headers behavior

  • server_timing_sampling_rate (Union[int, float, None]) – The percentage of responses that you want CloudFront to add the Server-Timing header to. Default: - no Server-Timing header is added to HTTP responses

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

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

CORS_ALLOW_ALL_ORIGINS = <jsii._reference_map.InterfaceDynamicProxy object>
CORS_ALLOW_ALL_ORIGINS_AND_SECURITY_HEADERS = <jsii._reference_map.InterfaceDynamicProxy object>
CORS_ALLOW_ALL_ORIGINS_WITH_PREFLIGHT = <jsii._reference_map.InterfaceDynamicProxy object>
CORS_ALLOW_ALL_ORIGINS_WITH_PREFLIGHT_AND_SECURITY_HEADERS = <jsii._reference_map.InterfaceDynamicProxy object>
SECURITY_HEADERS = <jsii._reference_map.InterfaceDynamicProxy object>
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.

node

The tree node.

response_headers_policy_id

The ID of the response headers policy.

stack

The stack in which this resource is defined.

Static Methods

classmethod from_response_headers_policy_id(scope, id, response_headers_policy_id)

Import an existing Response Headers Policy from its ID.

Parameters:
  • scope (Construct) –

  • id (str) –

  • response_headers_policy_id (str) –

Return type:

IResponseHeadersPolicy

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