Home >Backend Development >Golang >How do I change the instance type in an EC2 launch template using the AWS SDK?
I want to change something in the launch template, such as the instance type. This means creating a new version while doing so.
I have browsed the SDK documentation for Go and Python. Neither seems to have parameters that allow me to achieve the same goal.
I am referring to these: Go functions, Python functions
Please help me...
ec2 Launch templates are immutable. If you need to modify the current launch template version, you must create a new version.
The following is an example of using AWS SDK v2 to create a new version and set it as the default version.
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 an aws configuration:
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) }
The above is the detailed content of How do I change the instance type in an EC2 launch template using the AWS SDK?. For more information, please follow other related articles on the PHP Chinese website!