

# Attaching an existing CloudFront distribution
<a name="attaching-existing-distribution"></a>

This section provides instructions for integrating the solution with your existing CloudFront distribution for both architectures.

**Note**  
In the following instructions, UUID is used to reference the deployment UUID of your Dynamic Image Transformation for Amazon CloudFront stack. You can find this value by inspecting the Physical ID of a `AWS::CloudFront::Function` deployed in your stack, and extracting the value found after the word `modifier-`.

# Lambda architecture
<a name="api-gateway-existing-distribution"></a>

If you’ve deployed the Lambda architecture stack and have set the Use Existing CloudFront Distribution template parameter to Yes, use the following instructions to complete your setup.

## Setting the Origin
<a name="setting-the-origin"></a>

1. In the CloudFront console, navigate to the distribution you indicated in the Existing CloudFront Distribution ID template parameter.

1. Select the Origins tab and click **Create origin**.

1. Set the Origin domain as the API Gateway execution link. This value can be found by placing the Physical ID of the stack’s AWS::ApiGateway::RestApi in the search field and selecting LambdaRestApi under API Gateway.

1. Set the Origin path to `/image`.

1. Select **Create origin**.

## Setting the behavior
<a name="setting-the-behavior"></a>

1. In the CloudFront console, navigate to the distribution you indicated in the Existing CloudFront Distribution ID template parameter.

1. Select the Behaviors tab and choose **Create behavior** 

1. Set the Path pattern you’d like to point to your solution instance, in a Solution created distribution, this is Default (\$1)

1. Set the Origin to the Origin created in the previous section.

1. Set the Viewer Protocol policy to `Redirect HTTP to HTTPS` 

1. Set the Cache Policy to the one named `ServerlessImageHandler-${UUID}`.

1. Set the Origin request policy to the one named `ServerlessImageHandler-${UUID}`.

1. Set the Viewer request Function type to CloudFront Functions, and the Function ARN to the one named `sih-apig-request-modifier-${UUID}`.

1. Select **Create behavior**.

# ECS architecture
<a name="ecs-existing-distribution"></a>

For the ECS architecture, deploy the solution as-is to provision all necessary resources. Then manually configure your existing CloudFront distribution to use the solution’s resources.

## Deploy the solution
<a name="deploy-the-solution-2"></a>

1. Deploy the ECS architecture CloudFormation template without specifying an existing CloudFront distribution.

1. The solution will create its own CloudFront distribution along with all required resources including the Application Load Balancer and CloudFront function.

## Configure your existing distribution
<a name="configure-your-existing-distribution"></a>

After the solution deployment is complete:

1. In the CloudFront console, navigate to your existing CloudFront distribution.

1. Select the Origins tab and click **Create origin**.

1. Set the Origin domain as the Application Load Balancer DNS name. This value can be found in the CloudFormation stack outputs under the key `LoadBalancerDNS`.

1. Leave the Origin path empty (default).

1. Select **Create origin**.

## Create behavior for image processing
<a name="create-behavior-for-image-processing"></a>

1. In your existing CloudFront distribution, select the Behaviors tab and choose **Create behavior**.

1. Set the Path pattern for image requests (e.g., `/images/*` or your preferred pattern).

1. Set the Origin to the ALB origin created in the previous step.

1. Set the Viewer Protocol policy to `Redirect HTTP to HTTPS`.

1. Set the Cache Policy to the one named `dit-chache-policy` (created by the solution).

1. Set the Response headers policy to the one named `SecurityHeadersPolicy` (created by the solution).

1. Set the Viewer request Function type to CloudFront Functions, and the Function ARN to the one named `dit-header-normalization` (created by the solution).

1. Select **Create behavior**.

This approach allows you to leverage the solution’s provisioned resources while maintaining control over your existing CloudFront distribution configuration.