'How to get parameters in cloudformation template to launch using 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


Solution 1:[1]

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.)

Solution 2:[2]

I used aws go sdk v2 to get it working.

Add "github.com/aws/aws-sdk-go-v2/service/cloudformation" in the import section and then,

Try this out:-

Parameters: []types.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"),
                },
            },

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian
Solution 2 Vinu Joseph