Creating an EC2 Instance in AWS using Go: A Step-by-Step Guide
Learn How to Use the AWS SDK in Go to Create and Manage EC2 Instances
Table of contents
Intro ๐
Amazon Elastic Compute Cloud (EC2) is a popular web service that provides resizable compute capacity in the cloud. With EC2, users can quickly and easily launch virtual machines (VMs) in the AWS cloud to host their applications and workloads.
In this blog post, I will guide you through the process of creating an EC2 instance using the Go programming language and the AWS SDK.
Here is a video about this topic ๐
Now let's dive into the code and understand how it works.
Prerequisites ๐ฅ
You should have a basic understanding of the Go programming language and AWS. You will also need an AWS account and access to the AWS console.
Create an EC2 Instance and AMI Image ๐ชฃ
To create an EC2 instance, follow the steps below:
Log in to the AWS Management Console.
Select EC2 from the list of services.
Click the Launch Instance button to launch a new instance.
Choose the Amazon Machine Image (AMI) that you want to use for your instance. An AMI is a pre-configured virtual machine image that contains the necessary information to launch an instance.
Select the instance type that you want to use. The instance type determines the computing resources (CPU, memory, storage, and networking) that will be available to your instance.
Keep the number of instances 1 just for testing purposes.
Skip the security groups and tag for your instances if you want to.
Review your instance launch settings and click the Launch button to launch your instances.
To create an AMI image, follow the steps below:
Once the instance is created, select it and create an AMI image from it.
Click the Actions button and select the Image and templates from the dropdown menu.
Click the Create Image menu to create your AMI image.
Configure the settings for your AMI image, including the name, description, and any block device mappings that you want to include in the image.
Click the Create Image button to create your AMI image.
Go to the Images menu that is present in the left corner and click on AMIs.
Copy the AMI ID because you will need it further.
Setting up the Development Environment ๐ฌ
To get started, we need to install the AWS SDK for Go. We can do this by running the following command:
go get -u github.com/aws/aws-sdk-go
This command installs the latest version of the AWS SDK for Go.
Import the libraries ๐ฉ
First, import the required packages:
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
We imported
The "fmt" package for printing to the console.
The "log" package for error logging.
The "os" package for exiting the program in case of an error.
The AWS SDK for Go packages for accessing the EC2 service.
Create a new session ๐ฎ
Next, we create a new AWS session using the "session.NewSession" function:
session, err := session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
})
if err != nil {
log.Fatalf("failed to create a session %v\n", err)
os.Exit(1)
}
Create a new session with the AWS SDK. The session is created in the main() function and the AWS Config object is passed in it that specifies the AWS region to use. In this case, the region is set to "ap-south-1" which is the Mumbai region in India.
If an error occurs while creating the session, we log the error and exit the program:
Create a service client ๐
Next, the code creates an EC2 client using the session object. The EC2 client is used to interact and communicate with the EC2 service on AWS.
serviceClient := ec2.New(session)
Create an EC2 Instance ๐ค
After the service client, the createEC2Instance
function is called and this will create a new EC2 instance.
_, err = createEC2Instance(serviceClient)
if err != nil {
log.Fatalf("failed to create an instance %v", err)
}
The createEC2Instance
function takes in an EC2 client object and returns an instance ID and an error if any. The function creates an input parameter object that specifies the details of the EC2 instance to create, including the Amazon Machine Image (AMI) ID and the instance type.
Put the image id that we copied when we created the AMI image.
parameters := &ec2.RunInstancesInput{
ImageId: aws.String("image-id"),
InstanceType: aws.String("t2.micro"),
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
}
The RunInstances
method is then called on the EC2 client object with the input parameter object to create the EC2 instance.
It will return an error if the instance is not created.
createInstance, err := serviceClient.RunInstances(parameters)
if err != nil {
log.Fatalf("failed to create an instance %v\n", err)
os.Exit(1)
}
If the creation of the EC2 instance is successful, the function returns the instance ID.
instanceID := *createInstance.Instances[0].InstanceId
return instanceID, nil
Finally, the main
function prints out the instance ID of the newly created EC2 instance.
fmt.Printf("Created an EC2 instance and here is it's ID: %v\n", instanceID)
Repo ๐
The code that I have shown you in this blog is present in this repository.
Conclusion ๐โโ๏ธ
This code snippet demonstrates how to use the AWS SDK in Go to create an EC2 instance. This functionality can be used to dynamically create EC2 instances as needed, providing a scalable and cost-effective solution for running applications in the cloud.
As a Go developer, learning how to work with AWS services like EC2 can be a valuable skill to have and can open up many opportunities in the cloud computing industry.
That's it for now. Did you like this blog? Please let me know.
You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.
Happy Learning!