如何获取 cloudformation 模板中的参数以使用 Amazon Go SDK 启动?
How to get parameters in cloudformation template to launch using Amazon Go SDK?
我无法用 Golang 编写脚本来启动具有多个参数的 cloudformation 模板。我是 sdk 和 golang 的新手,所以我 运行 遇到了几个语法错误。
我试过 运行 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"),
})
}
错误代码:
./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
您正在尝试提供一个 []Parameter
,其中包含一个 Parameter
对象,在您需要的地方具有重复的字段(如错误所述)。您需要为要传递的每个参数传递一个 []*Parameter
,其中包含一个 指针 ,全部在切片中:
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"),
},
},
(看起来您也在另一个文件中声明了两次 main
,但未显示该来源并且错误无关。)
我用 aws go sdk v2 让它工作。
在导入部分添加“github.com/aws/aws-sdk-go-v2/service/cloudformation”,然后
试试这个:-
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"),
},
},
我无法用 Golang 编写脚本来启动具有多个参数的 cloudformation 模板。我是 sdk 和 golang 的新手,所以我 运行 遇到了几个语法错误。
我试过 运行 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"),
})
}
错误代码:
./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
您正在尝试提供一个 []Parameter
,其中包含一个 Parameter
对象,在您需要的地方具有重复的字段(如错误所述)。您需要为要传递的每个参数传递一个 []*Parameter
,其中包含一个 指针 ,全部在切片中:
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"),
},
},
(看起来您也在另一个文件中声明了两次 main
,但未显示该来源并且错误无关。)
我用 aws go sdk v2 让它工作。
在导入部分添加“github.com/aws/aws-sdk-go-v2/service/cloudformation”,然后
试试这个:-
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"),
},
},