amazon-web-services - 如何将 AWS GO SDK 与 RESTful API 结合使用?
问题描述
我不熟悉使用 AWS 开发工具包和构建 API。但是,我正在尝试找到一种构建应用程序的方法,如果我单击 Web 浏览器上的按钮,我希望它触发 amazon sdk Go 功能来构建 AMI。我该怎么办?
我用 Go 浏览了这个关于 RESTful API 的教程。但是,我对 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))
}
推荐阅读
- keras - 如何在 LSTM 自动编码器中计算带有零填充的输入的 softmax
- sql - 如何查询属性的未配对值
- amazon-s3 - 如何将文件从 AWS EFS 复制到 AWS S3?
- google-translation-api - 用于检查我在 Google Translation API 中的余额的 API
- python - 如何将 Pandas 系列转换为日期对象
- neural-network - 使用 TensorRT Caffe Parser 解析 Mobilenet-SSD 时出错,“ditcaffe.LayerParameter”没有名为“prior_box_param”的字段
- csv - Neo4j:导入 csv 最有效的解决方案是什么?
- sql - 如何使用触发器填充表
- android - 无法从 Firebase 获取子值
- nix - `nix-darwin` 提供了什么?