<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Terraform archivos - Geko Cloud</title>
	<atom:link href="https://geko.cloud/en/tag/terraform-en/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Servicios de consultoría cloud y devops</description>
	<lastBuildDate>Thu, 04 Nov 2021 08:41:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.7</generator>

<image>
	<url>https://geko.cloud/wp-content/uploads/2021/08/cropped-geko-fav-150x150.png</url>
	<title>Terraform archivos - Geko Cloud</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A quick view of modules in Terraform</title>
		<link>https://geko.cloud/en/a-quick-view-of-modules-in-terraform/</link>
					<comments>https://geko.cloud/en/a-quick-view-of-modules-in-terraform/#respond</comments>
		
		<dc:creator><![CDATA[Jose Luis Sánchez]]></dc:creator>
		<pubDate>Mon, 18 Jan 2021 17:35:34 +0000</pubDate>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Terraform]]></category>
		<guid isPermaLink="false">https://geko2.factoryfy.com/a-quick-view-of-modules-in-terraform/</guid>

					<description><![CDATA[<p>Introduction Modules in Terraform are a collection of resources that can be linked or just created individually. When we call a module, we are calling a file with a bunch of resources with some parameters inside, parameters we need either to specify in the resource itself or in another file which stores our variables. Why [&#8230;]</p>
<p>La entrada <a href="https://geko.cloud/en/a-quick-view-of-modules-in-terraform/">A quick view of modules in Terraform</a> se publicó primero en <a href="https://geko.cloud/en/">Geko Cloud</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2 class="western"><span style="font-family: Open Sans, sans-serif;"><span style="color: #404040;"><span style="font-size: xx-large;">I</span></span><span style="color: #404040;"><span style="font-size: xx-large;">ntroduction</span></span></span></h2>
<p><span style="font-family: Open Sans, sans-serif;"><span style="color: #3a3a3a;"><span style="font-size: medium;">Modules in <strong>Terraform</strong> are a collection of resources that can be linked or just created individually. When we call a module, we are calling a file with a bunch of resources with some parameters inside, parameters we need either to specify in the resource itself or </span></span><span style="color: #3a3a3a;"><span style="font-size: medium;">in another file which stores </span></span><span style="color: #3a3a3a;"><span style="font-size: medium;">our variables.</span></span></span></p>
<h2>Why do we use Terraform?</h2>
<ul>
<li><span style="color: #000000;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">It is a very useful tool to develop, change and have a versioning of our infrastructure in an efficient and safe way.</span></span></span></li>
<li><span style="color: #000000;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">It is a tool that allows you to define infrastructure as code and enables you to change and track the infrastructure with ease.</span></span></span></li>
<li><span style="color: #000000;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">It is independent of the platform or ‘cloud agnostic’ and allows working with multiple cloud providers.</span></span></span></li>
</ul>
<h3 class="western"><span style="font-family: Open Sans, sans-serif;"><span style="color: #404040;"><span style="font-size: large;">What do we need for this lab?</span></span></span></h3>
<ul>
<li><span style="font-family: Open Sans, sans-serif;"><strong>Terraform</strong> installed. In this case w</span><span style="font-family: 'Open Sans', sans-serif;">e run v.0.12.7.</span></li>
<li><span style="font-family: Open Sans, sans-serif;">In our case, we are using <strong>AWS</strong> as our <strong>Cloud Provider</strong>, so we need a user with programmatic access. We need as well to give access to this user with the proper IAM Policy.</span></li>
<li><span style="font-family: Open Sans, sans-serif;">Your preferred code editor.</span></li>
</ul>
<h2 class="western"><span style="color: #404040;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: xx-large;">Let’s get started!</span></span></span></h2>
<p><span style="font-family: Open Sans, sans-serif;">Usually, first we should either set up a <strong>Terraform</strong> module with our required parameters or just get some official module from the network. In our case, I did myself a module with an S3 Bucket and its own policy and therefore, I specify what options I’d like the module to have.</span></p>
<p><span style="font-family: Open Sans, sans-serif;">It’d look something like this:</span></p>
<div class="wp-block-codemirror-blocks code-block">
<pre class="CodeMirror" data-setting="{">resource "aws_s3_bucket" "this" {
  bucket            = var.bucket_name
  acl               = var.type_acl
  force_destroy     = var.destroy

  tags = {
    Name            = var.tag_name
  }
  versioning {
    enabled         = var.versioning
  } 
}

resource "aws_s3_bucket_policy" "this" {
  bucket = var.bucket_name
  policy = templatefile("${path.module}/templates/s3_origin_access_identity.json", {
    origin_access_identity_arn         = aws_cloudfront_origin_access_identity.this.iam_arn,
    origin_access_identity_bucket_name = var.bucket_name
  })
}</pre>
</div>
<p><span style="font-family: Open Sans, sans-serif;">As you may noticed, everything is “<i><b>variablized</b></i><em>”<b></b></em><i><b>. </b></i>Hence, we’ll need to create a file with our variables stored with their custom values.</span></p>
<p><span style="font-family: Open Sans, sans-serif;">In the same folder we have our file “main.tf”, we create our “variables.tf”:</span></p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{">### S3 VARIABLES

variable "bucket_name"      { }
variable "tag_name"         { }
variable "type_acl"         { }
variable "versioning"       { }
variable "destroy"          { }
variable "aliases"          { }
variable "certificate_arn"  { }</pre>
</div>
<p><span style="font-family: Open Sans, sans-serif;">In this particular case, you may ask yourselves why these variables don’t have any value stored. And the answer is, we’re using a main file called “terragrunt.hcl” which will store all our values when we call the module.</span></p>
<p><span style="font-family: Open Sans, sans-serif;">Another thing that may be confusing is how to specfiy the bucket policy. We can either specifyit with a heredoc file (EOF) or straightaway with a function of <strong>Terraform</strong> called “templatefile”.</span></p>
<p><span style="font-family: Open Sans, sans-serif;">The first example is in <strong>Terraform Documentation</strong>, a website that I strongly recommend you to visit to understand how powerful modules and its attributes are. </span></p>
<p><span style="font-family: Open Sans, sans-serif;">The second example, and the one we used, it’d look something like this:</span></p>
<p><span style="font-family: Open Sans, sans-serif;">On the same folder, we must have a folder called “templates” and inside, introduce the bucket policy as a JSON file.</span></p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{">{
    "Version": "2012-10-17",
    "Id": "PolicyForCloudFrontPrivateS3Content",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "${origin_access_identity_arn}"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::${origin_access_identity_bucket_name}/*"
        }
    ]
  }</pre>
</div>
<p><span style="font-family: Open Sans, sans-serif;">You may’ve noticed we introduced ${origin_access_identity_arn} and </span><span style="font-family: 'Open Sans', sans-serif;">${origin_access_identity_bucket_name} and this is because we want to set such values manually like this:</span></p>
<p>&nbsp;</p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{">policy = templatefile("${path.module}/templates/s3_origin_access_identity.json", {
    origin_access_identity_arn         = aws_cloudfront_origin_access_identity.this.iam_arn,
    origin_access_identity_bucket_name = var.bucket_name
  })</pre>
</div>
<p><span style="font-family: Open Sans, sans-serif;">As you see, we introduce the<i> “</i><i>arn”</i><i> </i>calling another resource called “aws_cloudfront_origin_access_identity” and the bucket name we introduce it as we have seen before with our variables file.</span></p>
<p><span style="font-family: Open Sans, sans-serif;">Now you just need to specify your <strong>Terraform</strong> provider and to call the module with another file with the values you have specified and deploy it:</span></p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{">provider "aws" {
  region     = var.region
  version    = "~&gt; 2.0"
}

provider "aws" {
  alias  = "aws_us_east_1"
  region = "us-east-1"
}
module "s3" {
    source               = "../../../../../../modules/s3-cloudfront/"
    bucket_name          = var.bucket_name
    tag_name             = var.tag_name
    type_acl             = var.type_acl
    versioning           = var.versioning
    destroy              = var.destroy
    aliases              = var.aliases
    certificate_arn      = var.certificate_arn    
}</pre>
</div>
<p><span style="font-family: Open Sans, sans-serif;">Deploy it with a<strong> terraform init</strong>, terraform plan and terraform apply.</span></p>
<h2 class="western"><span style="color: #404040;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: xx-large;">Conclusion</span></span></span></h2>
<p><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">So, we saw how to deploy an <strong>S3 Bucket</strong> with its policy with <strong>Terraform</strong>. I strongly recommend you visit the official <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs">Terraform Documentation</a> to keep learning about modules and all related to them.</span></span></span></p>
<p><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">I invite you </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">all in case you need </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">information about the </span></span></span><span style="color: #c5d31e;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;"><b><strong><a href="https://geko.cloud/en/devops/">DevOps</a> </strong></b></span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">world, </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">to</span></span></span> <span style="color: #009e00;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;"><a href="https://geko.cloud/en/contact/">contact us</a> </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">and keep checking </span></span></span><span style="color: #009e00;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;"><a href="https://geko.cloud/en/blog/labs/">our blog</a> </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">to find other useful </span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">posts</span></span></span><span style="color: #3a3a3a;"><span style="font-family: Open Sans, sans-serif;"><span style="font-size: medium;">.</span></span></span></p>
<p><a href="https://geko.cloud/en/contact/"><img decoding="async" class="aligncenter wp-image-3265" src="https://geko2.factoryfy.com/wp-content/uploads/geko-1-150x150.png" alt="" width="82" height="82" /></a></p>
<p>La entrada <a href="https://geko.cloud/en/a-quick-view-of-modules-in-terraform/">A quick view of modules in Terraform</a> se publicó primero en <a href="https://geko.cloud/en/">Geko Cloud</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://geko.cloud/en/a-quick-view-of-modules-in-terraform/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Upgrade GKE public-cluster’s Terraform module</title>
		<link>https://geko.cloud/en/upgrade-gke-public-clusters-terraform-module/</link>
					<comments>https://geko.cloud/en/upgrade-gke-public-clusters-terraform-module/#respond</comments>
		
		<dc:creator><![CDATA[Jose Luis Sánchez]]></dc:creator>
		<pubDate>Wed, 13 May 2020 06:24:40 +0000</pubDate>
				<category><![CDATA[News]]></category>
		<category><![CDATA[GKE]]></category>
		<category><![CDATA[Google Cloud]]></category>
		<category><![CDATA[Terraform]]></category>
		<guid isPermaLink="false">https://geko2.factoryfy.com/upgrade-gke-public-clusters-terraform-module/</guid>

					<description><![CDATA[<p>Introduction From time to time Google introduces new features and changes that sometimes also force the Terraform modules to upgrade themselves. It was our case at Geko, where we were using the GKE module for public-cluster deployment&#38;management at version 5.x. A few days ago, when we planned to update some parameters it came that Google [&#8230;]</p>
<p>La entrada <a href="https://geko.cloud/en/upgrade-gke-public-clusters-terraform-module/">Upgrade GKE public-cluster’s Terraform module</a> se publicó primero en <a href="https://geko.cloud/en/">Geko Cloud</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3>Introduction</h3>
<p>From time to time Google introduces new features and changes that sometimes also force the <a href="https://www.terraform.io/docs/configuration/modules.html">Terraform modules</a> to upgrade themselves. It was our case at <a href="https://geko2.factoryfy.com/">Geko</a>, where we were using the <a href="https://registry.terraform.io/modules/terraform-google-modules/kubernetes-engine/google/5.1.1/submodules/beta-public-cluster">GKE module for public-cluster deployment&amp;management at version 5.x</a>. A few days ago, when we planned to update some parameters it came that Google had removed the support for the Kubernetes dashboard. It was completely deprecated and the module was failing because of it, so we were forced to upgrade the module in order to meet the new conditions. There were up to 3 major version upgrades available, so we decided to go for it and use the latest one. However, it was not a standalone solution as it required to handle Terraform state&#8217;s incoherences.</p>
<p>The aim of this lab is to learn how to <strong>upgrade</strong> the official <strong>Terraform module</strong> intended to deploy&amp;manage a <strong>public GKE cluster</strong>. We will specially deal with module&#8217;s (<a href="https://registry.terraform.io/modules/terraform-google-modules/kubernetes-engine/google/8.1.0/submodules/beta-public-cluster">kubernetes-engine.beta-public-cluster</a>) breaking changes, and we will manage to obtain the consistent status we previously had before the failure which preceded the upgrade.</p>
<p><strong>Estimated time to finish this lab</strong>: ~20 minutes</p>
<h3>1. Remove the previous resources</h3>
<p><strong>It&#8217;s strongly encouraged to perform a <em>tfstate</em> file backup before continue!</strong></p>
<p>It&#8217;s especially important to remove all the conflicting resources from the Terraform state as soon as they are bounded between them using dependencies. The goal here is to remove any deprecated binding prior to importing them again from the current &#8220;picture&#8221; there&#8217;s already deployed.</p>
<p>The main components on a Kubernetes cluster are the networks (and subnetworks), the node pool and the cluster itself. Let&#8217;s focus on them.</p>
<div class="wp-block-codemirror-blocks code-block">
<pre class="CodeMirror" data-setting="{">terraform state rm module.gke.google_container_cluster.primary
terraform state rm module.gke.google_container_node_pool.pools[0]
terraform state rm module.vpc.google_compute_network.network
terraform state rm module.vpc.google_compute_subnetwork.subnetwork[0]
terraform state rm module.vpc.google_compute_subnetwork.subnetwork[1]</pre>
</div>
<h3>2. Upgrade versions</h3>
<p>Once removed the previous states the next step is to set the version for the required modules to the current latest version. For the GKE module the latest now it&#8217;s 8.1.0, but it will be allowed to automatically adopt minor upgrades (&#8220;~&gt;&#8221;).</p>
<h5>Upgrade the GKE cluster module</h5>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{"> module "gke" {
   source  = "terraform-google-modules/kubernetes-engine/google//modules/beta-public-cluster"
-  version = "~&gt; 5.0"
+  version = "~&gt; 8.1"
</pre>
</div>
<h5>Upgrade the VPC module</h5>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{"> module "vpc" {
-  source  = "github.com/terraform-google-modules/terraform-google-network?ref=v1.1.0"
+  source  = "github.com/terraform-google-modules/terraform-google-network?ref=v2.3.0"</pre>
</div>
<h5>Check the new resources</h5>
<p>In order to find out if the new resources have experienced a name change (due to the modules upgrade), a <a href="https://www.terraform.io/docs/commands/plan.html"><strong>Terraform plan</strong></a> is strongly encouraged.</p>
<p>On this case it has been found that some module&#8217;s internal hierarchy and also list&#8217;s indexes have changed.</p>
<div class="wp-block-codemirror-blocks code-block">
<pre class="CodeMirror" data-setting="{">  module.gke.google_container_cluster.primary
  
<b>-</b> module.gke.google_container_node_pool.pools[0]
<b>+</b> module.gke.google_container_node_pool.pools["default-node-pool"]

  module.vpc.google_compute_network.network
  
<b>-</b> module.vpc.google_compute_subnetwork.subnetwork[0]
<b>+</b> module.vpc.module.subnets.google_compute_subnetwork.subnetwork["southamerica-east1/my-cluster-public"]

<b>-</b> module.vpc.google_compute_subnetwork.subnetwork[1]
<b>+</b> module.vpc.module.subnets.google_compute_subnetwork.subnetwork["southamerica-east1/my-cluster-private"]</pre>
</div>
<h3>3. Import fresh resources</h3>
<p>Keep in mind that the zone/region depends on your kind of cluster. If it&#8217;s zonal you must use the master zone (e.g. <em>southamerica-east1-a</em>). On the other hand, if it&#8217;s a regional cluster you must use the region (e.g. <em>southamerica-east1</em>). The following example assumes a regional cluster located at <em>southamerica-east1</em>, in the project &#8220;<strong>my-project</strong>&#8220;, and with a cluster name &#8220;<strong>my-cluster</strong>&#8220;. The network names were set accordingly to the cluster&#8217;s name, just adding the suffixes &#8220;private&#8221; and &#8220;public&#8221; to the subnets to properly differentiate them.</p>
<p><strong>Note also the new module hierarchy and indexing.</strong></p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{"># Global vars
REGION="southamerica-east1"
PROJECT="my-project"
CLUSTER="my-cluster"

# Cluster
CLUSTER_LOCAL="module.gke.google_container_cluster.primary"
CLUSTER_REMOTE="${PROJECT}/${REGION}/${CLUSTER}"
terraform import $CLUSTER_LOCAL $CLUSTER_REMOTE

# Node pool
POOL_LOCAL="module.gke.google_container_node_pool.pools["default-node-pool"]"
POOL_REMOTE="${CLUSTER_REMOTE}/default-node-pool"
terraform import $POOL_LOCAL $POOL_REMOTE

# Subnetworks
BASE_SUBNET_LOCAL="module.vpc.module.subnets.google_compute_subnetwork.subnetwork"

## Public
PUBLIC_SUBNET_LOCAL="${BASE_SUBNET_LOCAL}["${REGION}/${CLUSTER}-public"]"
PUBLIC_SUBNET_REMOTE="${CLUSTER_REMOTE}-public"
terraform import $PUBLIC_SUBNET_LOCAL $PUBLIC_SUBNET_REMOTE

## Private
PRIVATE_SUBNET_LOCAL="${BASE_SUBNET_LOCAL}["${REGION}/${CLUSTER}-private"]"
PRIVATE_SUBNET_REMOTE="${CLUSTER_REMOTE}-private"
terraform import $PRIVATE_SUBNET_LOCAL $PRIVATE_SUBNET_REMOTE

# Network
NETWORK_LOCAL="module.vpc.module.vpc.google_compute_network.network"
NETWORK_REMOTE="${PROJECT}/${CLUSTER}"
terraform import $NETWORK_LOCAL $NETWORK_REMOTE</pre>
</div>
<h3> 4. Update parameters</h3>
<p>It&#8217;s very likely you will encounter that after a Terraform plan the <strong><em>google_container_cluster</em> </strong>resource still needs to be updated due to a <em>subnetwork</em> parameter change. The new subnet keys have made the indexes to change their order. Just edit your GKE module to replace the <em>subnetwork</em> parameter as below.</p>
<div class="wp-block-codemirror-blocks code-block ">
<pre class="CodeMirror" data-setting="{"><b>-</b> subnetwork = module.vpc.subnets_names[<b>0</b>]
<b>+</b> subnetwork = module.vpc.subnets_names[<b>1</b>]</pre>
</div>
<h3>Conclusion</h3>
<p>As you may have read above, sometimes -when relying on third parties- could happen that a breaking change is introduced and you get yourself into troubles to get the service back again. Beside this, the solution could introduce collateral damages which will require additional sub-solutions. On this particular case regarding Terraform, dealing with inconsistent states is not really common nor recommended, but it comes that is the only method you have available to solve them on your tool-set.</p>
<hr />
<p>I hope you&#8217;ve enjoyed this post and I encourage you to <a href="https://geko.cloud/en/blog/">check our blog for other posts</a> that you might find helpful. <a href="https://geko.cloud/en/contact/">Do not hesitate to contact us</a> if you would like us to help you on your projects.</p>
<p>See you on the next post!</p>
<p>La entrada <a href="https://geko.cloud/en/upgrade-gke-public-clusters-terraform-module/">Upgrade GKE public-cluster’s Terraform module</a> se publicó primero en <a href="https://geko.cloud/en/">Geko Cloud</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://geko.cloud/en/upgrade-gke-public-clusters-terraform-module/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
