<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Igal</title>
    <description>The latest articles on DEV Community by Igal (@harel).</description>
    <link>https://dev.to/harel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1395039%2Fa49f5d00-259a-4488-8fcb-4e2a780be5ae.png</url>
      <title>DEV Community: Igal</title>
      <link>https://dev.to/harel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harel"/>
    <language>en</language>
    <item>
      <title>Terraform and AWS CloudFront Continuous Deployment: A Workaround Solution</title>
      <dc:creator>Igal</dc:creator>
      <pubDate>Fri, 29 Mar 2024 08:36:01 +0000</pubDate>
      <link>https://dev.to/harel/terraform-and-aws-cloudfront-continuous-deployment-a-workaround-solution-1hjm</link>
      <guid>https://dev.to/harel/terraform-and-aws-cloudfront-continuous-deployment-a-workaround-solution-1hjm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbtwk3u6uoji92d02txo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbtwk3u6uoji92d02txo.png" alt="AWS CloudFront configuration using Terraform " width="512" height="512"&gt;&lt;/a&gt;&lt;br&gt;
This article explores a challenge encountered when setting up a continuous deployment workflow for AWS CloudFront using Terraform. We’ll delve into the issue, the limitations of Terraform’s current implementation, and a workaround solution for achieving continuous deployment with Terraform.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Issue: Conflicting Resource Creation
&lt;/h2&gt;

&lt;p&gt;AWS CloudFront recently introduced a “continuous deployment” feature. This allows testing configuration changes on a staging instance before rolling them out to the primary distribution. While Terraform supports managing both CloudFront distributions and the new continuous deployment policy, there’s currently a limitation.&lt;/p&gt;

&lt;p&gt;The Terraform provider (version 5.42.0 as of this writing) doesn’t allow creating a primary CloudFront distribution with a pre-defined continuous deployment policy. This leads to an error message stating “InvalidArgument: Continuous deployment policy is not supported during distribution creation.”&lt;/p&gt;

&lt;h2&gt;
  
  
  The Workaround: Sequential Deployment with Conditional Logic
&lt;/h2&gt;

&lt;p&gt;To overcome this limitation, we can leverage Terraform’s conditional logic capabilities. Here’s the solution:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define a Boolean Variable: Introduce a variable named is_set_continuous_deployment. This will act as a flag to determine if the continuous deployment policy should be linked to the primary distribution.&lt;/li&gt;
&lt;li&gt;Conditional Assignment in Primary Distribution: Within the aws_cloudfront_distribution resource for the primary distribution, conditionally set the continuous_deployment_policy_id attribute. Use a ternary operator to assign the ID of the aws_cloudfront_continuous_deployment_policy resource (created earlier) only if is_set_continuous_deployment is set to true. Otherwise, leave it empty.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;continuous_deployment_policy_id = var. is_set_continuous_deployment ?
aws_cloudfront_continuous_deployment_policy.this.id : ""

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Sequential Terraform Runs: This solution requires two separate Terraform runs.
First Run (is_set_continuous_deployment=false): In the initial run, set is_set_continuous_deployment to false. This will create both the primary and staging CloudFront distributions without attempting to link the policy to the primary one.
Second Run (is_set_continuous_deployment=true): After the first run successfully creates both distributions, execute Terraform again with is_set_continuous_deployment set to true. This will link the pre-existing continuous deployment policy to the primary distribution.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While Terraform doesn’t yet natively support creating a primary CloudFront distribution with a pre-defined continuous deployment policy, this workaround offers a solution for achieving continuous deployment functionality. This approach leverages conditional logic and sequential Terraform runs to configure the desired setup.&lt;/p&gt;

&lt;p&gt;Note: This solution is specific to Terraform version 5.42.0. Future updates might address this limitation, so staying updated on Terraform releases is recommended.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>terrafrom</category>
      <category>cloudfront</category>
    </item>
  </channel>
</rss>
