<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Barry K. on Medium]]></title>
        <description><![CDATA[Stories by Barry K. on Medium]]></description>
        <link>https://medium.com/@barry_k?source=rss-ea7db3e083e5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*N0n87_Q9ryFW-L9wgjM28Q.jpeg</url>
            <title>Stories by Barry K. on Medium</title>
            <link>https://medium.com/@barry_k?source=rss-ea7db3e083e5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 13:28:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@barry_k/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Managing Terraform State Efficiently with S3 and DynamoDB]]></title>
            <link>https://medium.com/@barry_k/managing-terraform-state-efficiently-with-s3-and-dynamodb-227c685ec6f4?source=rss-ea7db3e083e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/227c685ec6f4</guid>
            <category><![CDATA[terraform-state]]></category>
            <category><![CDATA[s3-bucket]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Barry K.]]></dc:creator>
            <pubDate>Sun, 01 Oct 2023 08:42:46 GMT</pubDate>
            <atom:updated>2023-10-01T08:42:46.664Z</atom:updated>
            <content:encoded><![CDATA[<p>Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure resources in a declarative manner. One critical aspect of Terraform is state management, which keeps track of the current state of your infrastructure and helps you make changes safely and efficiently. In this blog post, we’ll explore how to set up Terraform state management using Amazon S3 and DynamoDB, a popular and robust combination for remote state storage.</p><p><strong>Why Terraform Remote State Management?</strong></p><p>Terraform state is vital as it keeps track of the metadata of the managed resources and maps them to your configuration. Without the state, Terraform would be unable to know the existing infrastructure’s status and thus struggle to create, modify, or destroy resources accurately.</p><ul><li><strong>Collaboration</strong>: Remote state allows multiple team members to work on the same infrastructure code simultaneously without conflicts.</li><li><strong>Safety</strong>: Storing state remotely prevents accidental loss of sensitive data. If your local state is lost or corrupted, you can always retrieve the latest state from the remote backend.</li><li><strong>Concurrency</strong>: Remote state enables concurrent Terraform operations, which can speed up your infrastructure deployments.</li><li><strong>Locking</strong>: State locking prevents concurrent operations from stepping on each other’s toes, ensuring data integrity.</li></ul><p><strong>Setting Up Terraform State</strong></p><p>By default, Terraform stores the state in a local file named “terraform.tfstate”. However, this approach does not suit collaborative and scalable environments. To address this, Terraform provides Remote Backends, which enable storing the state file in a remote, shared, and centralised location.</p><p>Now, let’s get started with setting up Terraform state using Amazon S3 and DynamoDB.</p><p><strong>GitHub Repository</strong>: <a href="https://github.com/Barin-K/Terraform-State">https://github.com/Barin-K/Terraform-State</a></p><p><strong>Create an S3 Bucket: </strong>First, we’ll create an S3 bucket to store our Terraform state file. You can do this using the AWS CLI or through the AWS Management Console. But in this case, we will be using Terraform.</p><pre>resource &quot;aws_s3_bucket&quot; &quot;tfstate_bucket&quot; {<br>  bucket = var.bucket_name<br>  acl    = &quot;private&quot;<br><br>  versioning {<br>    enabled = true<br>  }<br><br>  lifecycle {<br>    prevent_destroy = false<br>  }<br><br>  tags = {<br>    Name = &quot;${var.environment}-s3&quot;<br>  }<br>}  </pre><pre>resource &quot;aws_dynamodb_table&quot; &quot;remotestate_table&quot; {<br>  name           = var.table_name<br>  hash_key       = &quot;LockID&quot;<br>  billing_mode   = &quot;PAY_PER_REQUEST&quot;<br><br>  attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<br><br>  tags = {<br>    Name = &quot;${var.environment}-Dynamo&quot;<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lCu2kvbBXlqTicjrrGs1RQ.png" /></figure><p><strong>Configure Terraform Backend: </strong>Now, let’s configure Terraform to use this remote state backend. In your Terraform configuration file (e.g., “backend.tf”), add the following block:</p><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket         = &quot;tfremotestate-bucket&quot;<br>    key            = &quot;backend/terraform.state&quot;<br>    region         = &quot;eu-west-1&quot;<br>    acl            = &quot;bucket-owner-full-control&quot;<br>    dynamodb_table = &quot;tfstate-lock&quot;<br>    <br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bOkpHHSiYAx5zFAcxjAZhA.png" /></figure><p><strong>Initialize and Apply: </strong>With the backend configuration in place, initialize your Terraform workspace and apply your infrastructure code as usual. Terraform will now use the specified S3 bucket and DynamoDB table for remote state management.</p><p>Setting up a remote backend for Terraform state is crucial for managing infrastructure in a collaborative, scalable, and secure manner. Using services like AWS S3 and DynamoDB not only provides security and versioning for the state file but also ensures consistency and prevents conflicts during concurrent operations.</p><p>By following the aforementioned steps, teams can efficiently leverage Terraform for infrastructure management, enhancing productivity, and minimizing risks associated with state mismanagement.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=227c685ec6f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backing Up an EC2 Instance with AWS Backup using Terraform]]></title>
            <link>https://medium.com/@barry_k/backing-up-an-ec2-instance-with-aws-backup-using-terraform-60a1a532fb97?source=rss-ea7db3e083e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/60a1a532fb97</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[backup]]></category>
            <dc:creator><![CDATA[Barry K.]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 09:56:00 GMT</pubDate>
            <atom:updated>2023-09-01T09:56:00.484Z</atom:updated>
            <content:encoded><![CDATA[<p>Businesses heavily depend on digital resources to operate and grow in our data-driven age. However, this also means there is a possibility of breaches in these resources due to data loss from file damage or corruption.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*5NOCk-Sn1BF94ckUF7VXuQ.jpeg" /><figcaption>Created by Author via draw.io</figcaption></figure><p>In this post, I will walk you through the steps of using the AWS Backup service to back up an Amazon Elastic Compute Cloud (Amazon EC2) instance using Terraform. AWS Backup is a fully managed backup service that simplifies the centralisation and automation of data backup across AWS services. AWS Backup automates backup tasks such as scheduling, retention, and life cycle management. This eliminates the need to develop custom scripts or manual processes.</p><h4><strong>The Prerequisites:</strong></h4><ul><li>An AWS Account</li><li>An EC2 Instance</li><li>Terraform Installed</li><li>AWS CLI configured with necessary permissions</li></ul><h4><strong>Terraform Implementation:</strong></h4><p><strong>Define AWS Provider: </strong>In the Terraform script (provider.tf), the AWS provider is defined, and my preferred AWS region is specified. You can replace the region to suit your preference.</p><p><strong>Create AWS Backup Vault: </strong>When you first start using AWS Backup, the service automatically creates a default backup vault for you. You can use this default vault or create additional custom vaults per your requirements. In this case, we will be creating a custom vault.</p><pre># Backup Vault Resource<br><br>resource &quot;aws_backup_vault&quot; &quot;my_vault&quot; {<br>  name        = &quot;my-backup-vault&quot;<br><br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UAIndIDtOhvwmWMwPPl0Ng.png" /></figure><p><strong>Create the Backup Plan:</strong></p><p>An AWS Backup plan is a set of rules that define when and how AWS resources are backed up. You could have multiple backup plans in place to cater to different sets of resources or various backup requirements. For instance, critical databases might have a daily backup schedule with a long retention period, while less critical data might be backed up weekly with a shorter retention.</p><p>By using AWS Backup plans, you ensure that your backup strategy is systematic, consistent, and automated, reducing the operational overhead and human errors associated with manual backups.</p><pre># Backup Plan Resource <br><br>resource &quot;aws_backup_plan&quot; &quot;my_back_plan&quot; {<br>  name = &quot;my-backup-plan&quot;<br><br>  rule {<br>    rule_name         = &quot;my-backup-rule&quot;<br>    target_vault_name = aws_backup_vault.my_vault.name<br>    schedule          = &quot;cron(0 11 * * ? *)&quot;<br>    completion_window = 60<br><br>    lifecycle {<br>      delete_after = 14<br>    }<br>  }<br>}  </pre><p>In the code snippet above, the backup plan resource is defined. The schedule specifies the backup schedule using a cron expression. In this case, backups will be triggered daily at 11:00 AM UTC. The completion window specifies that the backup should be completed within 60 minutes of the scheduled time and the lifecycle rule indicates that the backup should be automatically deleted after 14 days.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OcBRTMtHYtKkG85MIzakUQ.png" /></figure><p><strong>Define the AWS Backup Selection:</strong></p><p>A backup selection is the set of resources you choose to back up. These resources are specified based on either their resource IDs or by using tags to identify and group them. When creating a backup selection, it must be associated with a backup plan. This enables AWS Backup to determine what and when to back up. In this post, we will specify the EC2 instance by using its resource ID.</p><p>In the backup selection, an IAM role is required to enable the AWS Backup service to assume this role to back up the specified resources. This role provides the necessary permissions for AWS Backup to access and back up the resources you’ve identified in your backup selection.</p><p>Here is a snippet of the backup selection using the resource ID, the full backup selection code and project source code are uploaded to my <a href="https://github.com/Barin-K/aws-backup">GitHub repository</a>.</p><pre>resource &quot;aws_backup_selection&quot; &quot;myselection&quot; {<br>  iam_role_arn = aws_iam_role.my_bck_up_role.arn<br>  name         = &quot;test_selection&quot;<br>  plan_id      = aws_backup_plan.my_back_plan.id<br><br>  resources = [<br>    &quot;arn:aws:ec2:aws-region:aws-account-number:instance/your-instance-id&quot;<br>    <br>  ]<br>}</pre><p><strong>Apply your Terraform Script</strong>: Now, you can apply the Terraform configuration to create the necessary resources:</p><ul><li>Terraform plan</li><li>Terraform Apply</li><li>Terraform Destroy (To clean up all created resources)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W8yag6EqAPq_0NIIU05pDg.png" /></figure><p><strong>Conclusion: </strong>Overall, AWS Backup provides a robust and scalable solution for managing backups and ensuring data protection in AWS environments. It simplifies the backup process, automates workflows, and offers centralized management, making it easier for businesses to implement backup strategies and meet their data protection requirements.</p><p>With the above steps, you’ve automated the process of backing up an EC2 instance using AWS Backup with Terraform. It’s essential to monitor your backups regularly and test them to ensure they’re functioning as expected.</p><p>Terraform’s power lies in its ability to provide a reproducible infrastructure setup. If you’re using AWS Backup manually or looking to integrate it into your CI/CD pipelines, consider the above guide as a starting point.</p><p><strong>GitHub Repository</strong>: <a href="https://github.com/Barin-K/aws-backup">https://github.com/Barin-K/aws-backup</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60a1a532fb97" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Launch an Amazon EC2 Instance with Terraform]]></title>
            <link>https://medium.com/@barry_k/how-to-launch-an-amazon-ec2-instance-with-terraform-1f4cd3338c90?source=rss-ea7db3e083e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f4cd3338c90</guid>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Barry K.]]></dc:creator>
            <pubDate>Mon, 13 Feb 2023 21:16:03 GMT</pubDate>
            <atom:updated>2023-02-13T21:16:03.519Z</atom:updated>
            <content:encoded><![CDATA[<p>How organisations deploy and manage resources has completely changed since the emergence of cloud-based solutions. Infrastructure as Code (IaC) is one of the technologies which has improved resource management’s effectiveness and quickness in the cloud.</p><p>HashiCorp’s infrastructure as code technology is called Terraform and this tool helps you configure your cloud resources and infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kmhh3ydIEjwad1cLPs84Ww.jpeg" /><figcaption>Photo by HashiCorp on Google Images</figcaption></figure><p>Amazon Elastic Compute Cloud (Amazon EC2) is one of the numerous services provided by AWS that offers safe, scalable compute capability in the cloud. Amazon EC2 has a broad range of options that lets users create and deploy almost any application in the cloud and with the help of terraform, launching an EC2 instance is quick and simple.</p><p>In this cloud series for beginners and intermediates, I will be simplifying ways to deploy infrastructure into the Amazon Web Services (AWS) cloud, starting with launching an Amazon EC2 Instance via Terraform.</p><p><strong>The Basic Requirements</strong></p><ul><li>AWS Account</li><li>AWS User for Identity and Access Management</li><li>Code Editor</li><li>Basic understanding of Terraform</li></ul><p><strong>Terraform Configuration for Amazon EC2</strong></p><p>Although I used Visual Studio Code editor, any other code editor will work to produce similar results.</p><p><strong>Provider File</strong></p><p>Open your preferred code editor and create a folder where you will like the following configurations to be defined. Create a <strong>provider.tf</strong> file. Setting up the AWS provider enables Terraform to communicate with the numerous services that AWS offers. In this configuration, there is also a provider block that specifies the region to which you will like this instance deployed.</p><pre>#AWS Provider Configuration<br>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>  }<br>}<br><br>provider &quot;aws&quot; {<br>  region = &quot;eu-west-2&quot;<br>}</pre><p><strong>EC2 File</strong></p><p>This file will contain data and resource code blocks for your EC2 instance. I prefer naming my resource configuration files by their actual service names as it makes my code cleaner and well-structured (This is a personal preference). There are other naming conventions used for resource configuration files such as <strong>main.tf</strong> but in this case, I will be using an <strong>ec2.tf </strong>file<strong>.</strong></p><ul><li><strong>Data Block</strong></li></ul><pre># Data Source Configuration<br><br>data &quot;aws_ami&quot; &quot;ubuntu&quot; {<br>  most_recent = true<br><br>  filter {<br>    name   = &quot;name&quot;<br>    values = [&quot;ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*&quot;]<br>  }<br><br>  filter {<br>    name   = &quot;virtualization-type&quot;<br>    values = [&quot;hvm&quot;]<br>  }<br><br>  owners = [&quot;099720109477&quot;] # Canonical<br>}</pre><p>Information provided outside of Terraform can be accessed with the help of data sources and cloud providers such as AWS may offer data sources in addition to resource types. In the data block above, an Ubuntu Amazon Machine Image (AMI) is defined through the AWS-provided data source <strong>aws_ami. </strong>This<strong> </strong>will be utilised by the EC2 instance resource configuration.</p><ul><li><strong>Resource Block</strong></li></ul><p>In Terraform, a resource block helps you define and provide information about single or several infrastructure components such as an EC2 Instance. The EC2 instance resource block used in configuring our instance consists of an Amazon Machine Image (AMI), an instance type, a key name also known as a key pair and resource tags.</p><pre># Resource Configuration for EC2 Instance<br><br>resource &quot;aws_instance&quot; &quot;terraform_instance&quot; {<br>  ami           = data.aws_ami.ubuntu.id<br>  instance_type = var.instance_type<br>  key_name      = var.key_name<br><br>  tags = {<br>    Name = &quot;Terraform Instance&quot;<br>  }<br>}</pre><p>The AMI that was defined in the data block is passed into the resource block of the EC2 instance and you can easily create a key pair in the AWS console.</p><p><strong>Variables File</strong></p><p>A useful approach to define reusable values that set up your infrastructure in Terraform is by using variables. You can create a <strong>variables.tf </strong>file which will contain reusable values. The following variables can be used when trying out these examples.</p><pre>variable &quot;instance_type&quot; {<br>  description = &quot;The Instance type&quot;<br>  type        = string<br>  default     = &quot;t2.micro&quot;<br>}<br><br>variable &quot;key_name&quot; {<br>  description = &quot;Key to use for the Instance&quot;<br>  type        = string<br>  default     = &quot;your-key-pair&quot;<br>}</pre><p><strong>Deploying into the Cloud</strong></p><p>In my experience of using Terraform, this is arguably one of the most interesting parts. Deploying your infrastructure in the cloud via code. Once you have correctly created all necessary files and file configurations from the provided steps, you can navigate into the<strong> ec2.tf</strong> file directory preferably.</p><p>This can be done by right-clicking the <strong>ec2.tf</strong> file and clicking on the “Open integrated Terminal” option in the drop-down dialogue box, kindly see provided screenshot for further reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pNGpUHEekDzi0z4h7MEX5g.png" /></figure><p>To deploy an infrastructure with Terraform there are three commands you need to execute. They are as follows:</p><ul><li><strong>terraform init, </strong>which initialises your configuration and communicates with your cloud provider, in this scenario, AWS.</li><li><strong>terraform plan, </strong>this command gives you an overview of infrastructure configurations that terraform will execute in the cloud.</li><li><strong>terraform apply, </strong>this command prepares to launch the EC2 instance if an apply is successful with no bugs in your code, you will receive a yes or no prompt message. If you wish to proceed with the launch, you can type “yes” in your terminal and your instance will be deployed in the cloud.</li></ul><p>Now you can go back to the AWS console to see if your instance is up and running. As you can see from the tags (Name) in our EC2 resource block, the terraform instance has been successfully deployed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IXXWRzMwqcAXdFwvBDDcvg.jpeg" /></figure><p>If you want to clean up your environment after trying these steps, run the <strong>terraform destroy </strong>command.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f4cd3338c90" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>