如何获取cloudformation模板中的参数以使用Amazon Go SDK启动?

问题描述:

I am having trouble writing a script in Golang to launch a cloudformation template that has several parameters. I am new to both sdk and golang, so I am running into several syntax errors.

I have tried running the code in VS studio.

func runCFTscript(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 Cloudformation service client
    svc := cloudformation.New(sess)

    // Specify the details of the instance that you want to create.
    runResult, err := svc.CreateStack(&cloudformation.CreateStackInput{


        Parameters: []cloudformation.Parameter{
            {
                ParameterKey:   aws.String("Keyname"),
                ParameterValue: aws.String("testXXX"),
                ParameterKey:   aws.String("InstanceType"),
                ParameterValue: aws.String("t2.micro"),
                ParameterKey:   aws.String("SSHLocation"),
                ParameterValue: aws.String("0.0.0.0/0"),
            },
        },
        StackName:   aws.String("test"),
        TemplateURL: aws.String("https://test.com"),
    })
}

Error code:

./cloudformation.go:27:3: cannot use []cloudformation.Parameter literal (type []cloudformation.Parameter) as type []*cloudformation.Parameter in field value
./cloudformation.go:31:5: duplicate field name in struct literal: ParameterKey
./cloudformation.go:32:5: duplicate field name in struct literal: ParameterValue
./cloudformation.go:33:5: duplicate field name in struct literal: ParameterKey
./cloudformation.go:34:5: duplicate field name in struct literal: ParameterValue
./main.go:55:6: main redeclared in this block

You're trying to provide a []Parameter containing a single Parameter object with duplicate fields (as the error says) where you need a. You need to pass a []*Parameter containing a pointer for each parameter you want to pass, all in the slice:

    Parameters: []*cloudformation.Parameter{
        &cloudformation.Parameter{
            ParameterKey:   aws.String("Keyname"),
            ParameterValue: aws.String("testXXX"),
        },
        &cloudformation.Parameter{
            ParameterKey:   aws.String("InstanceType"),
            ParameterValue: aws.String("t2.micro"),
        },
        &cloudformation.Parameter{
            ParameterKey:   aws.String("SSHLocation"),
            ParameterValue: aws.String("0.0.0.0/0"),
        },
    },

(It looks like you also have main declared twice in another file, but that source isn't shown and the error is unrelated.)