gpt4 book ai didi

amazon-web-services - 如何将 AWS GO SDK 与 RESTful API 结合使用?

转载 作者:数据小太阳 更新时间:2023-10-29 03:20:05 27 4
gpt4 key购买 nike

我不熟悉使用 AWS 开发工具包和构建 API。但是,我正在尝试找到一种构建应用程序的方法,如果我单击网络浏览器上的按钮,我希望它触发 amazon sdk Go 函数来构建 AMI。我该怎么做?

我经历了this tutorial on RESTful API with Go .但是,我对 Amazon SDK GO 功能如何与 API 协同工作感到困惑。所以我有类似下面的代码。我只是不知道我在高层次上是否做对了。

package main

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gorilla/mux"
"net/http"
)

func (c *EC2) CreateLaunchTemplate(input *CreateLaunchTemplateInput) (*CreateLaunchTemplateOutput, error) {
// w.Header().Set("Content-Type", "application/json")
svc := ec2.New(session.New())
input := &ec2.CreateLaunchTemplateInput{
LaunchTemplateData: &ec2.RequestLaunchTemplateData{
ImageId: aws.String("ami-0cc142296677e2132"),
InstanceType: aws.String("t2.micro"),
NetworkInterfaces: []*ec2.LaunchTemplateInstanceNetworkInterfaceSpecificationRequest{
{
AssociatePublicIpAddress: aws.Bool(true),
DeviceIndex: aws.Int64(0),
Ipv6AddressCount: aws.Int64(1),
SubnetId: aws.String("subnet-03a04de08c5c6cb8e"),
},
},
TagSpecifications: []*ec2.LaunchTemplateTagSpecificationRequest{
{
ResourceType: aws.String("instance"),
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String("webserver"),
},
},
},
},
},
LaunchTemplateName: aws.String("my-template"),
VersionDescription: aws.String("WebVersion1"),
}

result, err := svc.CreateLaunchTemplate(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
}

func main() {
// Init Router
r := mux.NewRouter()

// Route Handlers / Endpoints
r.HandleFunc("/api/create_ami", CreateLaunchTemplate).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", r))
}

最佳答案

我最终解决了问题...我只是不明白 golang 和 aws sdk 的语法。但我能够使用按钮旋转 ami。

package main

import (
"fmt"
"log"
"net/http"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gorilla/mux"
)

func createInstance(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-1")},
)

// Create EC2 service client
svc := ec2.New(sess)

// Specify the details of the instance that you want to create.
runResult, err := svc.RunInstances(&ec2.RunInstancesInput{
// An Amazon Linux AMI ID for t2.micro instances in the us-west-2 region
ImageId: aws.String("ami-xxxxxx"),
InstanceType: aws.String("t2.micro"),
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
})

if err != nil {
fmt.Println("Could not create instance", err)
return
}

fmt.Println("Created instance", *runResult.Instances[0].InstanceId)

// Add tags to the created instance
_, errtag := svc.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{runResult.Instances[0].InstanceId},
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String("test"),
},
},
})
if errtag != nil {
log.Println("Could not create tags for instance", runResult.Instances[0].InstanceId, errtag)
return
}
}

func main() {

r := mux.NewRouter() //init the router

fmt.Println("Successfully tagged instance")


r.HandleFunc("/api/instance", createInstance).Methods("GET")
log.Fatal(http.ListenAndServe(":8500", r))
}

关于amazon-web-services - 如何将 AWS GO SDK 与 RESTful API 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com