英文:
can anyone help me with this code im trying to create an instance for an ec2 machine using Golang and aws sdk
问题
有人可以帮我解决这段代码吗?我正在尝试使用Golang和AWS SDK创建一个EC2实例,但一直出现错误。
无法创建新实例:NoCredentialProviders: no valid providers in chain. Deprecated.
要查看详细的错误信息,请参阅aws.Config.CredentialsChainVerboseErrors
进程以退出代码0结束
我对AWS非常陌生,所以任何帮助、指导、教程或其他任何相关资源都将非常棒。
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: %v\n", newInstance.Instances)
}
我尝试了很多示例。我在Windows上使用xset设置了环境变量,但我真的很困惑。这只是我的爱好,但我真的卡在这里。
英文:
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: %v\n", 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
答案1
得分: 2
由于SDK无法找到用于与AWS进行身份验证的凭据,所以出现了这个问题。可以通过设置AWS环境变量来解决此问题。
请参阅指定凭据部分
> set AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
> set AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
提供程序链按以下顺序查找凭据。
> - 环境变量。
> - 共享凭据文件。
> - 如果您的应用程序使用ECS任务定义或RunTask API操作,使用任务的IAM角色。
> - 如果您的应用程序在Amazon EC2实例上运行,使用Amazon EC2的IAM角色。
或者您可以直接在代码中提供凭据
但是一般情况下,不建议在代码中硬编码凭据,而是使用其他凭据方法。
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", ""),
},
})
英文:
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", ""),
},
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论