Building awsctl using Golang #1

A few days back, I came up with the idea of awsctl CLI which will be kubectl style and will be easy to generate information about aws resources. I decided to live stream the development of the project so that it will help beginners to understand the process and lifecycle of the OpenSource project and will help the audience to learn how to write the CLI tool. here’s the summary of first(16th Feb 2023) stream: ...

February 17, 2023 · 1 min · Suraj Narwade

Basic Authentication in HTTP API requests in Golang

In Golang, implementing basic authentication in an HTTP API request is relatively straightforward. Once we construct the request, then we have to call the SetBasicAuth() method and pass username & password package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "http://google.com", nil) if err != nil { log.Fatal(err) } req.SetBasicAuth("admin", "password") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", bodyText) } It is equivalent of, ...

February 12, 2023 · 2 min · Suraj Narwade

Civo Object Store as a Terraform Backend

Recently, Civo Cloud launched an object store that is object Storage and S3-compatible. Read more about it here: https://www.civo.com/learn/using-civo-object-stores In my Cloud Heist - Civo series on youtube, one of the viewers asked how we can store terraform state in the bucket, similar to how we do in AWS space. Here’s the solution for the same, Let’s create the object store resource "civo_object_store" "statefile" { name = "state" max_size_gb = 500 region = "LON1" } Now apply this config. It will create the bucket and local state file. terraform apply now let’s verify from the console that the bucket is created (make sure you are in the correct region) we will use civo CLI to get the information about the object store. # state is the name of the bucket/objectstore $ civo objectstore show state ID : 9177e5e5-f04f-4a9b-bc25-6e028ca54187 Name : state Size : 500 Object Store Endpoint : objectstore.lon1.civo.com Region : LON1 Access Key : <ACCESS_KEY> Status : ready To access the secret key run: civo objectstore credential secret --access-key=<ACCESS_KEY> * note down the access key from the final output and run the following command to get the required environment variables. since we are using s3 backend, which is mainly used for AWS, we will have to use environment variables with the prefix AWS ...

February 12, 2023 · 2 min · Suraj Narwade

[SOLVED] Error: multiple EC2 Instances matched; use additional constraints to reduce matches to a single EC2 Instance

When you are using data sources for aws_instance probably to fetch the IP address or something else, data "aws_instance" "foo" { filter { name = "tag:Name" values = ["instance-name"] } } You must have faced the following issues while using Terraform for EC2 instances. │ Error: multiple EC2 Instances matched; use additional constraints to reduce matches to a single EC2 Instance Why this issue occurs? There’s a fair chance that the previous instance with the same name is destroyed, but there’s still an entry with the terminated state. ...

February 12, 2023 · 1 min · Suraj Narwade

Installing & Setting up Golang

Golang, also known as Go, is a programming language developed by Google. Today, it is used by many organisations for their web applications and CLI tools. Many OpenSource projects in cloud-native spaces, such as Kubernetes and Docker, were written using Go. Golang is known for its simplicity, efficiency, and scalability. This blog post will walk you through installing and setting up Golang on your computer. Downloading the Go Go to this link https://go.dev/dl/ and select the build depending on your operating system, as shown in the following image. ...

January 19, 2023 · 2 min · Suraj Narwade

Difference between Setting & Adding the Headers in HTTP API in Golang

While working with APIs, headers are an essential aspect. Sometimes we set them, or sometimes we consume them and make decisions. When I was exploring headers in Golang, I came across two methods. Headers.Set() & Headers.Add() Initially, I thought they were the same. But then I wondered if they are the same, then why two methods? Let’s understand. I will be using a proxy which will use Add & Set on headers, as shown below, ...

January 18, 2023 · 2 min · Suraj Narwade

Why close the HTTP API response body in Golang? What if you don't...

In the previous article, we saw how to make the HTTP GET API request in Golang. While going through the example, we discussed closing the response body. In this article, Let’s discuss why it is essential to close the response body. What is the response body? The response body is a stream of data that is read from the server. Why should I close it? Ensure all data has been read and the resources associated with it are freed up. What happens if I don’t close it? It can lead to resource leaks. It can cause the program to consume more memory and resources than necessary, leading to performance issues. It can also cause issues with the underlying connection as transport may not reuse HTTP/1.x “keep-alive” TCP connections if the Body is not read to completion and closed. From the official docs, ...

January 14, 2023 · 2 min · Suraj Narwade

How to make an HTTP GET request in Golang?

Golang is one of the widely used languages for designing API Clients. While you design the client, one of the important aspects of the client is fetching the data from the API in some format using a GET request. Here’s the curl command example, which mimics the GET request which we will write code for in Golang, $ curl -XGET localhost:8080 {"message":"hello world !!!"} Note: I already have a basic webserver running, Hence I can get the output on localhost. ...

January 13, 2023 · 2 min · Suraj Narwade

How to use Golang with MySQL?

Golang is a widely used language for building scalable and reliable web applications. When it comes to Storing data (relational), MySQL is one of the old and preferred ways as a database for many organisations. This blog post will look at how to use Golang with MySQL by establishing the connection and printing the version using a simple query. In upcoming blog posts, we will see advanced operations. Prerequisites To follow along with this tutorial, you should have the following installed on your machine: ...

January 9, 2023 · 3 min · Suraj Narwade

Read Terraform's plan the better way

Last year, I started exploring and studying terraform for work. While studying, I mostly relied on Terraform plan command to check the output plan. As terraform module I was writing got bigger, the plan got bigger too, and I needed to store the plan in a file and then read & analyse the file later. For example, I want to read the plan for the following resource & store it in a file, ...

December 19, 2022 · 2 min · Suraj Narwade