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

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

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创建新版本并将其设置为默认版本的示例。

安装以下两个包:

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

假设您已创建了AWS配置:

  1. func createLaunchTemplateVersion(cfg aws.Config) {
  2. ec2client := ec2.NewFromConfig(cfg)
  3. template := ec2types.RequestLaunchTemplateData{
  4. InstanceType: ec2types.InstanceTypeT2Medium}
  5. createParams := ec2.CreateLaunchTemplateVersionInput{
  6. LaunchTemplateData: &template,
  7. LaunchTemplateName: aws.String("MyTemplate"),
  8. SourceVersion: aws.String("1"),
  9. }
  10. outputCreate, err := ec2client.CreateLaunchTemplateVersion(context.Background(), &createParams)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. if outputCreate.Warning != nil {
  15. log.Fatalf("%v\n", outputCreate.Warning.Errors)
  16. }
  17. // 将新的启动模板版本设置为默认版本
  18. modifyParams := ec2.ModifyLaunchTemplateInput{
  19. DefaultVersion: aws.String(strconv.FormatInt(*outputCreate.LaunchTemplateVersion.VersionNumber, 10)),
  20. LaunchTemplateName: outputCreate.LaunchTemplateVersion.LaunchTemplateName,
  21. }
  22. outputModify, err := ec2client.ModifyLaunchTemplate(context.Background(), &modifyParams)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Printf("默认版本 %d\n", *outputModify.LaunchTemplate.DefaultVersionNumber)
  27. }
英文:

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:

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

Assuming you created the AWS config:

  1. func createLaunchTemplateVersion(cfg aws.Config) {
  2. ec2client := ec2.NewFromConfig(cfg)
  3. template := ec2types.RequestLaunchTemplateData{
  4. InstanceType: ec2types.InstanceTypeT2Medium}
  5. createParams := ec2.CreateLaunchTemplateVersionInput{
  6. LaunchTemplateData: &template,
  7. LaunchTemplateName: aws.String("MyTemplate"),
  8. SourceVersion: aws.String("1"),
  9. }
  10. outputCreate, err := ec2client.CreateLaunchTemplateVersion(context.Background(), &createParams)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. if outputCreate.Warning != nil {
  15. log.Fatalf("%v\n", outputCreate.Warning.Errors)
  16. }
  17. // set the new launch type version as the default version
  18. modifyParams := ec2.ModifyLaunchTemplateInput{
  19. DefaultVersion: aws.String(strconv.FormatInt(*outputCreate.LaunchTemplateVersion.VersionNumber, 10)),
  20. LaunchTemplateName: outputCreate.LaunchTemplateVersion.LaunchTemplateName,
  21. }
  22. outputModify, err := ec2client.ModifyLaunchTemplate(context.Background(), &modifyParams)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Printf("default version %d\n", *outputModify.LaunchTemplate.DefaultVersionNumber)
  27. }

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:

确定