AWS Golang SDK v2 - How to add functions to Go AWS scripts

王林
Release: 2024-02-09 09:27:08
forward
964 people have browsed it

AWS Golang SDK v2 - 如何向 Go AWS 脚本添加函数

php Editor Apple brings you an introduction to AWS Golang SDK v2. If you are writing AWS scripts in Go, you may want to add some custom functions to your script to enhance its functionality. This article will show you how to use AWS Golang SDK v2 to add functions to Go AWS scripts to make your scripts more powerful and flexible. Whether you're a beginner or an experienced developer, you'll get useful tips and guidance from this article. Let’s explore together!

Question content

Try to separate each part of the script into functions so that the output can be used later. When trying to pass an instance to the printvolumesinfo function, I can't get that part to work.

[]instanceblockdevicemapping is part of the instance structure, but I'm not sure what to use as input to the function.

`

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/ec2"
)

var client *ec2.Client


func init() {
    cfg, err := config.LoadDefaultConfig(context.TODO())
    if err != nil {
        panic("configuration error, " + err.Error())
    }
    client = ec2.NewFromConfig(cfg)

}

func printVolumesInfo(volumes []ec2.InstanceBlockDeviceMapping) {
    for _, b := range volumes {
        fmt.Println("   " + *b.DeviceName)
        fmt.Println("   " + *b.Ebs.VolumeId)
    }
}

func main() {
    parms := &ec2.DescribeInstancesInput{}
    result, err := client.DescribeInstances(context.TODO(), parms)

    if err != nil {
        fmt.Println("Error calling ec2: ", err)
        return
    }

    for _, r := range result.Reservations {
        fmt.Println("Reservation ID: " + *r.ReservationId)
        fmt.Println("Instance IDs:")
        for _, i := range r.Instances {
            fmt.Println("   " + *i.InstanceId)
            printVolumesInfo(i.InstanceBlockDeviceMapping)
        }
    }
}
Copy after login

`

Error received: ./main.go:74:37: undefined: ec2.instanceblockdevicemapping

Try using different parameters, including []instanceblockdevicemapping and blockdevicemapping. Additionally, ec2 and client are used as values.

Solution

Check the documentation:https://www.php.cn/link/421b0ea70ab7fd681ae63a8f12695199

This field is called BlockDeviceMappings. And the type InstanceBlockDeviceMapping is in package github.com/aws/aws-sdk-go-v2/service/ec2/types instead of in package ec2 .

  1. Add github.com/aws/aws-sdk-go-v2/service/ec2/types` to your imports
  2. Change the parameter type of function printVolumes to volumes []ec2.InstanceBlockDeviceMapping
  3. Call this function as printVolumesInfo(i.BlockDeviceMappings)

The above is the detailed content of AWS Golang SDK v2 - How to add functions to Go AWS scripts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!