# ResponseHeadersPolicy

A Response Headers Policy configuration.

Extends: Construct › Resource › ResponseHeadersPolicy
Implements: IResource, IResponseHeadersPolicy, IEnvironmentAware, IResponseHeadersPolicyRef, IConstruct, IDependable

- CloudFormation: [`AWS::CloudFront::ResponseHeadersPolicy`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-cloudfront-responseheaderspolicy.html)
- L1 type: [`CfnResponseHeadersPolicy`](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CfnResponseHeadersPolicy.html)
- Source: [`aws-cloudfront/lib/response-headers-policy.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-cloudfront/lib/response-headers-policy.ts#L88)
- Type last changed: v2.224.0 · 13 Nov 2025

## Example

```ts
// Using an existing managed response headers policy
declare const bucketOrigin: origins.S3Origin;
new cloudfront.Distribution(this, 'myDistManagedPolicy', {
  defaultBehavior: {
    origin: bucketOrigin,
    responseHeadersPolicy: cloudfront.ResponseHeadersPolicy.CORS_ALLOW_ALL_ORIGINS,
  },
});

// Creating a custom response headers policy -- all parameters optional
const myResponseHeadersPolicy = new cloudfront.ResponseHeadersPolicy(this, 'ResponseHeadersPolicy', {
  responseHeadersPolicyName: 'MyPolicy',
  comment: 'A default policy',
  corsBehavior: {
    accessControlAllowCredentials: false,
    accessControlAllowHeaders: ['X-Custom-Header-1', 'X-Custom-Header-2'],
    accessControlAllowMethods: ['GET', 'POST'],
    accessControlAllowOrigins: ['*'],
    accessControlExposeHeaders: ['X-Custom-Header-1', 'X-Custom-Header-2'],
    accessControlMaxAge: Duration.seconds(600),
    originOverride: true,
  },
  customHeadersBehavior: {
    customHeaders: [
      { header: 'X-Amz-Date', value: 'some-value', override: true },
      { header: 'X-Amz-Security-Token', value: 'some-value', override: false },
    ],
  },
  securityHeadersBehavior: {
    contentSecurityPolicy: { contentSecurityPolicy: 'default-src https:;', override: true },
    contentTypeOptions: { override: true },
    frameOptions: { frameOption: cloudfront.HeadersFrameOption.DENY, override: true },
    referrerPolicy: { referrerPolicy: cloudfront.HeadersReferrerPolicy.NO_REFERRER, override: true },
    strictTransportSecurity: { accessControlMaxAge: Duration.seconds(600), includeSubdomains: true, override: true },
    xssProtection: { protection: true, modeBlock: false, reportUri: 'https://example.com/csp-report', override: true },
  },
  removeHeaders: ['Server'],
  serverTimingSamplingRate: 50,
});
new cloudfront.Distribution(this, 'myDistCustomPolicy', {
  defaultBehavior: {
    origin: bucketOrigin,
    responseHeadersPolicy: myResponseHeadersPolicy,
  },
});
```

## Initializer

```ts
new ResponseHeadersPolicy(scope: Construct, id: string, props?: ResponseHeadersPolicyProps)
```

## Construct Props

- `comment`: string — A comment to describe the response headers policy.

  **Default:** no comment

- `corsBehavior`: [ResponseHeadersCorsBehavior](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.ResponseHeadersCorsBehavior.html) — A configuration for a set of HTTP response headers that are used for cross-origin resource sharing (CORS).

  **Default:** no cors behavior

- `customHeadersBehavior`: [ResponseCustomHeadersBehavior](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.ResponseCustomHeadersBehavior.html) — A configuration for a set of custom HTTP response headers.

  **Default:** no custom headers behavior

- `removeHeaders`: string[] — A list of HTTP response headers that CloudFront removes from HTTP responses that it sends to viewers.

  **Default:** no headers are removed

- `responseHeadersPolicyName`: string — A unique name to identify the response headers policy.

  **Default:** generated from the `id`

- `securityHeadersBehavior`: [ResponseSecurityHeadersBehavior](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.ResponseSecurityHeadersBehavior.html) — A configuration for a set of security-related HTTP response headers.

  **Default:** no security headers behavior

- `serverTimingSamplingRate`: number — 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

## Properties

- `CORS_ALLOW_ALL_ORIGINS`: [IResponseHeadersPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.IResponseHeadersPolicy.html) (static) — Use this managed policy to allow simple CORS requests from any origin.

- `CORS_ALLOW_ALL_ORIGINS_AND_SECURITY_HEADERS`: [IResponseHeadersPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.IResponseHeadersPolicy.html) (static) — Use this managed policy to allow simple CORS requests from any origin and add a set of security headers to all responses that CloudFront sends to viewers.

- `CORS_ALLOW_ALL_ORIGINS_WITH_PREFLIGHT`: [IResponseHeadersPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.IResponseHeadersPolicy.html) (static) — Use this managed policy to allow CORS requests from any origin, including preflight requests.

- `CORS_ALLOW_ALL_ORIGINS_WITH_PREFLIGHT_AND_SECURITY_HEADERS`: [IResponseHeadersPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.IResponseHeadersPolicy.html) (static) — Use this managed policy to allow CORS requests from any origin, including preflight requests, and add a set of security headers to all responses that CloudFront sends to viewers.

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `responseHeadersPolicyId`: string — The ID of the response headers policy.

- `responseHeadersPolicyRef`: [ResponseHeadersPolicyReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_cloudfront.ResponseHeadersPolicyReference.html) — A reference to a ResponseHeadersPolicy resource.

- `SECURITY_HEADERS`: [IResponseHeadersPolicy](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.IResponseHeadersPolicy.html) (static) — Use this managed policy to add a set of security headers to all responses that CloudFront sends to viewers.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

## Methods

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — 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`).

- `fromResponseHeadersPolicyId(scope: Construct, id: string, responseHeadersPolicyId: string): IResponseHeadersPolicy` (static) — Import an existing Response Headers Policy from its ID.

- `isConstruct(x: any): boolean` (static) — 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 object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
