1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package ec2
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/ec2/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Launches the specified number of instances using an AMI for which you have
16// permissions. You can specify a number of options, or leave the default options.
17// The following rules apply:
18//
19// * [EC2-VPC] If you don't specify a subnet ID, we
20// choose a default subnet from your default VPC for you. If you don't have a
21// default VPC, you must specify a subnet ID in the request.
22//
23// * [EC2-Classic] If
24// don't specify an Availability Zone, we choose one for you.
25//
26// * Some instance
27// types must be launched into a VPC. If you do not have a default VPC, or if you
28// do not specify a subnet ID, the request fails. For more information, see
29// Instance types available only in a VPC
30// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-vpc.html#vpc-only-instance-types).
31//
32// *
33// [EC2-VPC] All instances have a network interface with a primary private IPv4
34// address. If you don't specify this address, we choose one from the IPv4 range of
35// your subnet.
36//
37// * Not all instance types support IPv6 addresses. For more
38// information, see Instance types
39// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html).
40//
41// * If
42// you don't specify a security group ID, we use the default security group. For
43// more information, see Security groups
44// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html).
45//
46// *
47// If any of the AMIs have a product code attached for which the user has not
48// subscribed, the request fails.
49//
50// You can create a launch template
51// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html),
52// which is a resource that contains the parameters to launch an instance. When you
53// launch an instance using RunInstances, you can specify the launch template
54// instead of specifying the launch parameters. To ensure faster instance launches,
55// break up large requests into smaller batches. For example, create five separate
56// launch requests for 100 instances each instead of one launch request for 500
57// instances. An instance is ready for you to use when it's in the running state.
58// You can check the state of your instance using DescribeInstances. You can tag
59// instances and EBS volumes during launch, after launch, or both. For more
60// information, see CreateTags and Tagging your Amazon EC2 resources
61// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html). Linux
62// instances have access to the public key of the key pair at boot. You can use
63// this key to provide secure access to the instance. Amazon EC2 public images use
64// this feature to provide secure access without passwords. For more information,
65// see Key pairs
66// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html). For
67// troubleshooting, see What to do if an instance immediately terminates
68// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_InstanceStraightToTerminated.html),
69// and Troubleshooting connecting to your instance
70// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html).
71func (c *Client) RunInstances(ctx context.Context, params *RunInstancesInput, optFns ...func(*Options)) (*RunInstancesOutput, error) {
72	if params == nil {
73		params = &RunInstancesInput{}
74	}
75
76	result, metadata, err := c.invokeOperation(ctx, "RunInstances", params, optFns, addOperationRunInstancesMiddlewares)
77	if err != nil {
78		return nil, err
79	}
80
81	out := result.(*RunInstancesOutput)
82	out.ResultMetadata = metadata
83	return out, nil
84}
85
86type RunInstancesInput struct {
87
88	// The maximum number of instances to launch. If you specify more instances than
89	// Amazon EC2 can launch in the target Availability Zone, Amazon EC2 launches the
90	// largest possible number of instances above MinCount. Constraints: Between 1 and
91	// the maximum number you're allowed for the specified instance type. For more
92	// information about the default limits, and how to request an increase, see How
93	// many instances can I run in Amazon EC2
94	// (http://aws.amazon.com/ec2/faqs/#How_many_instances_can_I_run_in_Amazon_EC2) in
95	// the Amazon EC2 FAQ.
96	//
97	// This member is required.
98	MaxCount int32
99
100	// The minimum number of instances to launch. If you specify a minimum that is more
101	// instances than Amazon EC2 can launch in the target Availability Zone, Amazon EC2
102	// launches no instances. Constraints: Between 1 and the maximum number you're
103	// allowed for the specified instance type. For more information about the default
104	// limits, and how to request an increase, see How many instances can I run in
105	// Amazon EC2
106	// (http://aws.amazon.com/ec2/faqs/#How_many_instances_can_I_run_in_Amazon_EC2) in
107	// the Amazon EC2 General FAQ.
108	//
109	// This member is required.
110	MinCount int32
111
112	// Reserved.
113	AdditionalInfo *string
114
115	// The block device mapping entries.
116	BlockDeviceMappings []types.BlockDeviceMapping
117
118	// Information about the Capacity Reservation targeting option. If you do not
119	// specify this parameter, the instance's Capacity Reservation preference defaults
120	// to open, which enables it to run in any open Capacity Reservation that has
121	// matching attributes (instance type, platform, Availability Zone).
122	CapacityReservationSpecification *types.CapacityReservationSpecification
123
124	// Unique, case-sensitive identifier you provide to ensure the idempotency of the
125	// request. If you do not specify a client token, a randomly generated token is
126	// used for the request to ensure idempotency. For more information, see Ensuring
127	// Idempotency
128	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html).
129	// Constraints: Maximum 64 ASCII characters
130	ClientToken *string
131
132	// The CPU options for the instance. For more information, see Optimizing CPU
133	// options
134	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html)
135	// in the Amazon EC2 User Guide.
136	CpuOptions *types.CpuOptionsRequest
137
138	// The credit option for CPU usage of the burstable performance instance. Valid
139	// values are standard and unlimited. To change this attribute after launch, use
140	// ModifyInstanceCreditSpecification
141	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifyInstanceCreditSpecification.html).
142	// For more information, see Burstable performance instances
143	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/burstable-performance-instances.html)
144	// in the Amazon EC2 User Guide. Default: standard (T2 instances) or unlimited
145	// (T3/T3a instances)
146	CreditSpecification *types.CreditSpecificationRequest
147
148	// If you set this parameter to true, you can't terminate the instance using the
149	// Amazon EC2 console, CLI, or API; otherwise, you can. To change this attribute
150	// after launch, use ModifyInstanceAttribute
151	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifyInstanceAttribute.html).
152	// Alternatively, if you set InstanceInitiatedShutdownBehavior to terminate, you
153	// can terminate the instance by running the shutdown command from the instance.
154	// Default: false
155	DisableApiTermination bool
156
157	// Checks whether you have the required permissions for the action, without
158	// actually making the request, and provides an error response. If you have the
159	// required permissions, the error response is DryRunOperation. Otherwise, it is
160	// UnauthorizedOperation.
161	DryRun bool
162
163	// Indicates whether the instance is optimized for Amazon EBS I/O. This
164	// optimization provides dedicated throughput to Amazon EBS and an optimized
165	// configuration stack to provide optimal Amazon EBS I/O performance. This
166	// optimization isn't available with all instance types. Additional usage charges
167	// apply when using an EBS-optimized instance. Default: false
168	EbsOptimized bool
169
170	// An elastic GPU to associate with the instance. An Elastic GPU is a GPU resource
171	// that you can attach to your Windows instance to accelerate the graphics
172	// performance of your applications. For more information, see Amazon EC2 Elastic
173	// GPUs
174	// (https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/elastic-graphics.html)
175	// in the Amazon EC2 User Guide.
176	ElasticGpuSpecification []types.ElasticGpuSpecification
177
178	// An elastic inference accelerator to associate with the instance. Elastic
179	// inference accelerators are a resource you can attach to your Amazon EC2
180	// instances to accelerate your Deep Learning (DL) inference workloads. You cannot
181	// specify accelerators from different generations in the same request.
182	ElasticInferenceAccelerators []types.ElasticInferenceAccelerator
183
184	// Indicates whether the instance is enabled for AWS Nitro Enclaves. For more
185	// information, see  What is AWS Nitro Enclaves?
186	// (https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html) in the AWS
187	// Nitro Enclaves User Guide. You can't enable AWS Nitro Enclaves and hibernation
188	// on the same instance.
189	EnclaveOptions *types.EnclaveOptionsRequest
190
191	// Indicates whether an instance is enabled for hibernation. For more information,
192	// see Hibernate your instance
193	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Hibernate.html) in the
194	// Amazon EC2 User Guide. You can't enable hibernation and AWS Nitro Enclaves on
195	// the same instance.
196	HibernationOptions *types.HibernationOptionsRequest
197
198	// The IAM instance profile.
199	IamInstanceProfile *types.IamInstanceProfileSpecification
200
201	// The ID of the AMI. An AMI ID is required to launch an instance and must be
202	// specified here or in a launch template.
203	ImageId *string
204
205	// Indicates whether an instance stops or terminates when you initiate shutdown
206	// from the instance (using the operating system command for system shutdown).
207	// Default: stop
208	InstanceInitiatedShutdownBehavior types.ShutdownBehavior
209
210	// The market (purchasing) option for the instances. For RunInstances, persistent
211	// Spot Instance requests are only supported when InstanceInterruptionBehavior is
212	// set to either hibernate or stop.
213	InstanceMarketOptions *types.InstanceMarketOptionsRequest
214
215	// The instance type. For more information, see Instance types
216	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html) in the
217	// Amazon EC2 User Guide. Default: m1.small
218	InstanceType types.InstanceType
219
220	// [EC2-VPC] The number of IPv6 addresses to associate with the primary network
221	// interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet.
222	// You cannot specify this option and the option to assign specific IPv6 addresses
223	// in the same request. You can specify this option if you've specified a minimum
224	// number of instances to launch. You cannot specify this option and the network
225	// interfaces option in the same request.
226	Ipv6AddressCount int32
227
228	// [EC2-VPC] The IPv6 addresses from the range of the subnet to associate with the
229	// primary network interface. You cannot specify this option and the option to
230	// assign a number of IPv6 addresses in the same request. You cannot specify this
231	// option if you've specified a minimum number of instances to launch. You cannot
232	// specify this option and the network interfaces option in the same request.
233	Ipv6Addresses []types.InstanceIpv6Address
234
235	// The ID of the kernel. We recommend that you use PV-GRUB instead of kernels and
236	// RAM disks. For more information, see  PV-GRUB
237	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html)
238	// in the Amazon EC2 User Guide.
239	KernelId *string
240
241	// The name of the key pair. You can create a key pair using CreateKeyPair
242	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateKeyPair.html)
243	// or ImportKeyPair
244	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ImportKeyPair.html).
245	// If you do not specify a key pair, you can't connect to the instance unless you
246	// choose an AMI that is configured to allow users another way to log in.
247	KeyName *string
248
249	// The launch template to use to launch the instances. Any parameters that you
250	// specify in RunInstances override the same parameters in the launch template. You
251	// can specify either the name or ID of a launch template, but not both.
252	LaunchTemplate *types.LaunchTemplateSpecification
253
254	// The license configurations.
255	LicenseSpecifications []types.LicenseConfigurationRequest
256
257	// The metadata options for the instance. For more information, see Instance
258	// metadata and user data
259	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html).
260	MetadataOptions *types.InstanceMetadataOptionsRequest
261
262	// Specifies whether detailed monitoring is enabled for the instance.
263	Monitoring *types.RunInstancesMonitoringEnabled
264
265	// The network interfaces to associate with the instance. If you specify a network
266	// interface, you must specify any security groups and subnets as part of the
267	// network interface.
268	NetworkInterfaces []types.InstanceNetworkInterfaceSpecification
269
270	// The placement for the instance.
271	Placement *types.Placement
272
273	// [EC2-VPC] The primary IPv4 address. You must specify a value from the IPv4
274	// address range of the subnet. Only one private IP address can be designated as
275	// primary. You can't specify this option if you've specified the option to
276	// designate a private IP address as the primary IP address in a network interface
277	// specification. You cannot specify this option if you're launching more than one
278	// instance in the request. You cannot specify this option and the network
279	// interfaces option in the same request.
280	PrivateIpAddress *string
281
282	// The ID of the RAM disk to select. Some kernels require additional drivers at
283	// launch. Check the kernel requirements for information about whether you need to
284	// specify a RAM disk. To find kernel requirements, go to the AWS Resource Center
285	// and search for the kernel ID. We recommend that you use PV-GRUB instead of
286	// kernels and RAM disks. For more information, see  PV-GRUB
287	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html)
288	// in the Amazon EC2 User Guide.
289	RamdiskId *string
290
291	// The IDs of the security groups. You can create a security group using
292	// CreateSecurityGroup
293	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateSecurityGroup.html).
294	// If you specify a network interface, you must specify any security groups as part
295	// of the network interface.
296	SecurityGroupIds []string
297
298	// [EC2-Classic, default VPC] The names of the security groups. For a nondefault
299	// VPC, you must use security group IDs instead. If you specify a network
300	// interface, you must specify any security groups as part of the network
301	// interface. Default: Amazon EC2 uses the default security group.
302	SecurityGroups []string
303
304	// [EC2-VPC] The ID of the subnet to launch the instance into. If you specify a
305	// network interface, you must specify any subnets as part of the network
306	// interface.
307	SubnetId *string
308
309	// The tags to apply to the resources during launch. You can only tag instances and
310	// volumes on launch. The specified tags are applied to all instances or volumes
311	// that are created during launch. To tag a resource after it has been created, see
312	// CreateTags
313	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateTags.html).
314	TagSpecifications []types.TagSpecification
315
316	// The user data to make available to the instance. For more information, see
317	// Running commands on your Linux instance at launch
318	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html) (Linux) and
319	// Adding User Data
320	// (https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-add-user-data)
321	// (Windows). If you are using a command line tool, base64-encoding is performed
322	// for you, and you can load the text from a file. Otherwise, you must provide
323	// base64-encoded text. User data is limited to 16 KB.
324	UserData *string
325}
326
327// Describes a launch request for one or more instances, and includes owner,
328// requester, and security group information that applies to all instances in the
329// launch request.
330type RunInstancesOutput struct {
331
332	// [EC2-Classic only] The security groups.
333	Groups []types.GroupIdentifier
334
335	// The instances.
336	Instances []types.Instance
337
338	// The ID of the AWS account that owns the reservation.
339	OwnerId *string
340
341	// The ID of the requester that launched the instances on your behalf (for example,
342	// AWS Management Console or Auto Scaling).
343	RequesterId *string
344
345	// The ID of the reservation.
346	ReservationId *string
347
348	// Metadata pertaining to the operation's result.
349	ResultMetadata middleware.Metadata
350}
351
352func addOperationRunInstancesMiddlewares(stack *middleware.Stack, options Options) (err error) {
353	err = stack.Serialize.Add(&awsEc2query_serializeOpRunInstances{}, middleware.After)
354	if err != nil {
355		return err
356	}
357	err = stack.Deserialize.Add(&awsEc2query_deserializeOpRunInstances{}, middleware.After)
358	if err != nil {
359		return err
360	}
361	if err = addSetLoggerMiddleware(stack, options); err != nil {
362		return err
363	}
364	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
365		return err
366	}
367	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
368		return err
369	}
370	if err = addResolveEndpointMiddleware(stack, options); err != nil {
371		return err
372	}
373	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
374		return err
375	}
376	if err = addRetryMiddlewares(stack, options); err != nil {
377		return err
378	}
379	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
380		return err
381	}
382	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
383		return err
384	}
385	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
386		return err
387	}
388	if err = addClientUserAgent(stack); err != nil {
389		return err
390	}
391	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
392		return err
393	}
394	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
395		return err
396	}
397	if err = addIdempotencyToken_opRunInstancesMiddleware(stack, options); err != nil {
398		return err
399	}
400	if err = addOpRunInstancesValidationMiddleware(stack); err != nil {
401		return err
402	}
403	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opRunInstances(options.Region), middleware.Before); err != nil {
404		return err
405	}
406	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
407		return err
408	}
409	if err = addResponseErrorMiddleware(stack); err != nil {
410		return err
411	}
412	if err = addRequestResponseLogging(stack, options); err != nil {
413		return err
414	}
415	return nil
416}
417
418type idempotencyToken_initializeOpRunInstances struct {
419	tokenProvider IdempotencyTokenProvider
420}
421
422func (*idempotencyToken_initializeOpRunInstances) ID() string {
423	return "OperationIdempotencyTokenAutoFill"
424}
425
426func (m *idempotencyToken_initializeOpRunInstances) HandleInitialize(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
427	out middleware.InitializeOutput, metadata middleware.Metadata, err error,
428) {
429	if m.tokenProvider == nil {
430		return next.HandleInitialize(ctx, in)
431	}
432
433	input, ok := in.Parameters.(*RunInstancesInput)
434	if !ok {
435		return out, metadata, fmt.Errorf("expected middleware input to be of type *RunInstancesInput ")
436	}
437
438	if input.ClientToken == nil {
439		t, err := m.tokenProvider.GetIdempotencyToken()
440		if err != nil {
441			return out, metadata, err
442		}
443		input.ClientToken = &t
444	}
445	return next.HandleInitialize(ctx, in)
446}
447func addIdempotencyToken_opRunInstancesMiddleware(stack *middleware.Stack, cfg Options) error {
448	return stack.Initialize.Add(&idempotencyToken_initializeOpRunInstances{tokenProvider: cfg.IdempotencyTokenProvider}, middleware.Before)
449}
450
451func newServiceMetadataMiddleware_opRunInstances(region string) *awsmiddleware.RegisterServiceMetadata {
452	return &awsmiddleware.RegisterServiceMetadata{
453		Region:        region,
454		ServiceID:     ServiceID,
455		SigningName:   "ec2",
456		OperationName: "RunInstances",
457	}
458}
459