# SourceConfiguration

A source configuration is a wrapper for CloudFront origins and behaviors.

An origin is what CloudFront will "be in front of" - that is, CloudFront will pull its assets from an origin.

If you're using s3 as a source - pass the `s3Origin` property, otherwise, pass the `customOriginSource` property.

One or the other must be passed, and it is invalid to pass both in the same SourceConfiguration.

- Source: [`aws-cloudfront/lib/web-distribution.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts#L152)

## Example

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
import { aws_cloudfront as cloudfront } from 'aws-cdk-lib';
import { aws_iam as iam } from 'aws-cdk-lib';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { aws_s3 as s3 } from 'aws-cdk-lib';
import { aws_cloudfront as interfaces_cloudfront } from 'aws-cdk-lib/interfaces';

declare const bucket: s3.Bucket;
declare const cloudFrontOriginAccessIdentityRef: interfaces_cloudfront.ICloudFrontOriginAccessIdentityRef & iam.IGrantable;
declare const functionRef: interfaces_cloudfront.IFunctionRef;
declare const keyGroupRef: interfaces_cloudfront.IKeyGroupRef;
declare const version: lambda.Version;
const sourceConfiguration: cloudfront.SourceConfiguration = {
  behaviors: [{
    allowedMethods: cloudfront.CloudFrontAllowedMethods.GET_HEAD,
    cachedMethods: cloudfront.CloudFrontAllowedCachedMethods.GET_HEAD,
    compress: false,
    defaultTtl: cdk.Duration.minutes(30),
    forwardedValues: {
      queryString: false,

      // the properties below are optional
      cookies: {
        forward: 'forward',

        // the properties below are optional
        whitelistedNames: ['whitelistedNames'],
      },
      headers: ['headers'],
      queryStringCacheKeys: ['queryStringCacheKeys'],
    },
    functionAssociations: [{
      eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
      function: functionRef,
    }],
    isDefaultBehavior: false,
    lambdaFunctionAssociations: [{
      eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
      lambdaFunction: version,

      // the properties below are optional
      includeBody: false,
    }],
    maxTtl: cdk.Duration.minutes(30),
    minTtl: cdk.Duration.minutes(30),
    pathPattern: 'pathPattern',
    trustedKeyGroups: [keyGroupRef],
    trustedSigners: ['trustedSigners'],
    viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.HTTPS_ONLY,
  }],

  // the properties below are optional
  connectionAttempts: 123,
  connectionTimeout: cdk.Duration.minutes(30),
  customOriginSource: {
    domainName: 'domainName',

    // the properties below are optional
    allowedOriginSSLVersions: [cloudfront.OriginSslPolicy.SSL_V3],
    httpPort: 123,
    httpsPort: 123,
    originHeaders: {
      originHeadersKey: 'originHeaders',
    },
    originKeepaliveTimeout: cdk.Duration.minutes(30),
    originPath: 'originPath',
    originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY,
    originReadTimeout: cdk.Duration.minutes(30),
    originShieldRegion: 'originShieldRegion',
  },
  failoverCriteriaStatusCodes: [cloudfront.FailoverStatusCode.FORBIDDEN],
  failoverCustomOriginSource: {
    domainName: 'domainName',

    // the properties below are optional
    allowedOriginSSLVersions: [cloudfront.OriginSslPolicy.SSL_V3],
    httpPort: 123,
    httpsPort: 123,
    originHeaders: {
      originHeadersKey: 'originHeaders',
    },
    originKeepaliveTimeout: cdk.Duration.minutes(30),
    originPath: 'originPath',
    originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY,
    originReadTimeout: cdk.Duration.minutes(30),
    originShieldRegion: 'originShieldRegion',
  },
  failoverS3OriginSource: {
    s3BucketSource: bucket,

    // the properties below are optional
    originAccessIdentity: cloudFrontOriginAccessIdentityRef,
    originHeaders: {
      originHeadersKey: 'originHeaders',
    },
    originPath: 'originPath',
    originShieldRegion: 'originShieldRegion',
  },
  originShieldRegion: 'originShieldRegion',
  s3OriginSource: {
    s3BucketSource: bucket,

    // the properties below are optional
    originAccessIdentity: cloudFrontOriginAccessIdentityRef,
    originHeaders: {
      originHeadersKey: 'originHeaders',
    },
    originPath: 'originPath',
    originShieldRegion: 'originShieldRegion',
  },
};
```

## Properties

- `behaviors`: [Behavior](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.Behavior.html)[] (required) — The behaviors associated with this source.

  At least one (default) behavior must be included.

- `connectionAttempts`: number = 3 — The number of times that CloudFront attempts to connect to the origin.

  You can specify 1, 2, or 3 as the number of attempts.

- `connectionTimeout`: [Duration](/cdk/api/v2/docs/aws-cdk-lib.Duration.html) = cdk.Duration.seconds(10) — The number of seconds that CloudFront waits when trying to establish a connection to the origin.

  You can specify a number of seconds between 1 and 10 (inclusive).

- `customOriginSource`: [CustomOriginConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CustomOriginConfig.html) — A custom origin source - for all non-s3 sources.

- `failoverCriteriaStatusCodes`: [FailoverStatusCode](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.FailoverStatusCode.html)[] — HTTP status code to failover to second origin.

  **Default:** [500, 502, 503, 504]

- `failoverCustomOriginSource`: [CustomOriginConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CustomOriginConfig.html) — A custom origin source for failover in case the s3OriginSource returns invalid status code.

  **Default:** no failover configuration

- `failoverS3OriginSource`: [S3OriginConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.S3OriginConfig.html) — An s3 origin source for failover in case the s3OriginSource returns invalid status code.

  **Default:** no failover configuration

- `originShieldRegion`: string — When you enable Origin Shield in the AWS Region that has the lowest latency to your origin, you can get better network performance.

  **Default:** origin shield not enabled

- `s3OriginSource`: [S3OriginConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.S3OriginConfig.html) — An s3 origin source - if you're using s3 for your assets.
