如何使用AWS SDK更改EC2启动模板中的实例类型?

huangapple go评论81阅读模式
英文:

How to change instance type in EC2 Launch Template using AWS SDK?

问题

我正在寻找更改启动模板中某些内容的方法,例如实例类型。这意味着在此过程中创建一个新版本。

我已经查阅了Go和Python的SDK文档,但似乎都没有提供相应的参数来实现这一目标。

我指的是以下内容:
Go的函数
Python的函数

请帮助我解决这个问题...

英文:

I'm looking to change certain things in the launch template e.g. the instance type. Which means creating a new version while doing so.

I have gone through the SDK documentation for both Go and Python. Neither seem to have the paramenters that'd let me acheive the same.

I'm refering to these:
Go's function,
Python's function

Please help me out...

答案1

得分: 2

EC2启动模板是不可变的。如果需要修改当前启动模板版本,必须创建一个新版本。

以下是使用AWS SDK v2创建新版本并将其设置为默认版本的示例。

安装以下两个包:

"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

假设您已创建了AWS配置:

func createLaunchTemplateVersion(cfg aws.Config) {
    ec2client := ec2.NewFromConfig(cfg)
    template := ec2types.RequestLaunchTemplateData{
        InstanceType: ec2types.InstanceTypeT2Medium}
    createParams := ec2.CreateLaunchTemplateVersionInput{
        LaunchTemplateData: &template,
        LaunchTemplateName: aws.String("MyTemplate"),
        SourceVersion:      aws.String("1"),
    }
    outputCreate, err := ec2client.CreateLaunchTemplateVersion(context.Background(), &createParams)
    if err != nil {
        log.Fatal(err)
    }
    if outputCreate.Warning != nil {
        log.Fatalf("%v\n", outputCreate.Warning.Errors)
    }
    // 将新的启动模板版本设置为默认版本
    modifyParams := ec2.ModifyLaunchTemplateInput{
        DefaultVersion:     aws.String(strconv.FormatInt(*outputCreate.LaunchTemplateVersion.VersionNumber, 10)),
        LaunchTemplateName: outputCreate.LaunchTemplateVersion.LaunchTemplateName,
    }
    outputModify, err := ec2client.ModifyLaunchTemplate(context.Background(), &modifyParams)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("默认版本 %d\n", *outputModify.LaunchTemplate.DefaultVersionNumber)
}
英文:

EC2 launch template is immutable. You must create a new version if you need to modify the current launch template version.

Here is an example of creating a new version and then making it the default version using AWS SDK v2.

Install these two packages:

"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

Assuming you created the AWS config:

func createLaunchTemplateVersion(cfg aws.Config) {
	ec2client := ec2.NewFromConfig(cfg)
	template := ec2types.RequestLaunchTemplateData{
		InstanceType: ec2types.InstanceTypeT2Medium}
	createParams := ec2.CreateLaunchTemplateVersionInput{
		LaunchTemplateData: &template,
		LaunchTemplateName: aws.String("MyTemplate"),
		SourceVersion:      aws.String("1"),
	}
	outputCreate, err := ec2client.CreateLaunchTemplateVersion(context.Background(), &createParams)
	if err != nil {
		log.Fatal(err)
	}
	if outputCreate.Warning != nil {
		log.Fatalf("%v\n", outputCreate.Warning.Errors)
	}
	// set the new launch type version as the default version
	modifyParams := ec2.ModifyLaunchTemplateInput{
		DefaultVersion:     aws.String(strconv.FormatInt(*outputCreate.LaunchTemplateVersion.VersionNumber, 10)),
		LaunchTemplateName: outputCreate.LaunchTemplateVersion.LaunchTemplateName,
	}
	outputModify, err := ec2client.ModifyLaunchTemplate(context.Background(), &modifyParams)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("default version %d\n", *outputModify.LaunchTemplate.DefaultVersionNumber)
}

huangapple
  • 本文由 发表于 2022年12月2日 11:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74650569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定