Menu Close

Create Subnet in AWS using Go SDK

In this article, we will are going to learn how to Create Subnet in AWS using Go SDK with example in details.

A subnet, short for subnetwork, in Amazon Web Services (AWS) is a range of IP addresses within a Virtual Private Cloud (VPC) that helps you logically separate and organize your resources. A VPC is a virtual network dedicated to your AWS account, providing you with a private and isolated environment to launch resources.

Subnets are used to divide a VPC into smaller, more manageable sections. Each subnet operates within a specific Availability Zone (AZ), which are distinct locations within an AWS region that consist of one or more discrete data centers. This ensures that the resources in the subnet are available and operational even if there are issues with other AZs.

Prerequisites to create Subnet in AWS using the Go SDK:

  1. You should have an AWS account
  2. Go installed on your local machine (v1.16 or later).
  3. A basic understanding of Go programming

To begin, you’ll need to install the AWS SDK for Go. Open your terminal and run:

go get -u github.com/aws/aws-sdk-go

This command downloads and installs the SDK for Go.

Example to create Subnet:

package main


import (

	"fmt"

	"os"


	"github.com/aws/aws-sdk-go/aws"

	"github.com/aws/aws-sdk-go/aws/session"

	"github.com/aws/aws-sdk-go/service/ec2"

)


func newAWSSession(region string) (*session.Session, error) {

	sess, err := session.NewSession(&aws.Config{

		Region: aws.String(region),

	})

	if err != nil {

		return nil, err

	}

	return sess, nil

}


func createSubnet(sess *session.Session, vpcID, cidrBlock, availabilityZone string) (*ec2.Subnet, error) {

	svc := ec2.New(sess)


	input := &ec2.CreateSubnetInput{

		CidrBlock:        aws.String(cidrBlock),

		VpcId:            aws.String(vpcID),

		AvailabilityZone: aws.String(availabilityZone),

	}


	result, err := svc.CreateSubnet(input)

	if err != nil {

		return nil, err

	}


	return result.Subnet, nil

}

func main() {

	region := "us-west-2"

	vpcID := "vpc-xxxxxxxxxxxxxxxxx" // Replace with your VPC ID

	cidrBlock := "10.0.1.0/24"

	availabilityZone := "us-west-2a"


	sess, err := newAWSSession(region)

	if err != nil {

		fmt.Printf("Failed to create AWS session: %v\n", err)

		os.Exit(1)

	}


	subnet, err := createSubnet(sess, vpcID, cidrBlock, availabilityZone)

	if err != nil {

		fmt.Printf("Failed to create subnet: %v\n", err)

		os.Exit(1)

	}


	fmt.Printf("Subnet created successfully with ID: %s\n", *subnet.SubnetId)

}


Replace the vpcID variable with your VPC ID, and customize the cidrBlock and availabilityZone as needed. Run the program using go run main.go. If successful, you will see the ID of the newly created subnet.

Output:

go run main.go

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/golang-tutorial/

References:

https://en.wikipedia.org/wiki/Go_(programming_language)

Posted in AWS, Cloud, golang

Leave a Reply

Your email address will not be published. Required fields are marked *