skip to Main Content

can anyone help me with this code im trying to create an instance for an ec2 machine using Golang and aws sdk i keep getting an error

Couldn’t create new instance: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
Process finished with the exit code 0
im really new to aws so any help or pointers tutorials or anything like this would be awesome.

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/ec2"
)

func CreateInstance(client *ec2.EC2, imageId string, minCount int, maxCount int, instanceType string, keyName string) (*ec2.Reservation, error) {
    res, err := client.RunInstances(&ec2.RunInstancesInput{
        ImageId:      aws.String(imageId),
        MinCount:     aws.Int64(int64(minCount)),
        MaxCount:     aws.Int64(int64(maxCount)),
        InstanceType: aws.String(instanceType),
        KeyName:      aws.String(keyName),
    })

    if err != nil {
        return nil, err
    }

    return res, nil
}

func main() {
    sess, err := session.NewSessionWithOptions(session.Options{
        Profile: "default",
        Config: aws.Config{
            Region: aws.String("us-west-2"),
        },
    })

    if err != nil {
        fmt.Printf("Failed to initialize new session: %v", err)
        return
    }

    ec2Client := ec2.New(sess)

    keyName := "ec2--teamspeak"
    instanceType := "t4g.nano"
    minCount := 1
    maxCount := 1
    imageId := "ami-0b0154d3d8011b0cd"
    newInstance, err := CreateInstance(ec2Client, imageId, minCount, maxCount, instanceType, keyName)
    if err != nil {
        fmt.Printf("Couldn't create new instance: %v", err)
        return
    }

    fmt.Printf("Created new instance: %vn", newInstance.Instances)
}

i have tried many examples. I added environment setting using xset for windows to set variables .
but I’m really struggling. this is a hobbies but im really stuck here

2

Answers


  1. Ensure you have the AWS CLI installed and configured with the proper credentials.

    1.Install the AWS CLI if you haven’t already: https://aws.amazon.com/cli/

    2.Run aws configure to set up the credentials. You’ll be prompted to enter your AWS Access Key ID, AWS Secret Access Key, Default region name, and Default output format.

    Login or Signup to reply.
  2. It’s due to the SDK cannot find the credentials for authenticating with AWS. The issue can be resolved by setting the aws environment variables.

    See Specifying Credentials session

    set AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
    set AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY

    The provider chain looks for credentials in the following order.

    • Environment variables.
    • Shared credentials file.
    • If your application uses an ECS task definition or RunTask API operation, IAM role for tasks.
    • If your application is running on an Amazon EC2 instance, IAM role for Amazon EC2.

    Or You can provide the credentials directly in the code

    But in general, it is not recommended to hard-code credentials in your code, instead use other credentials methods.

        sess, err := session.NewSessionWithOptions(session.Options{
            Profile: "default",
            Config: aws.Config{
                Region:      aws.String("us-west-2"),
                Credentials: credentials.NewStaticCredentials("your-access-key", "your-secret-key", ""),
            },
        })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search