InstanceProps

class aws_cdk.aws_ec2.InstanceProps(*, instance_type, machine_image, vpc, allow_all_ipv6_outbound=None, allow_all_outbound=None, associate_public_ip_address=None, availability_zone=None, block_devices=None, credit_specification=None, detailed_monitoring=None, ebs_optimized=None, enclave_enabled=None, hibernation_enabled=None, init=None, init_options=None, instance_initiated_shutdown_behavior=None, instance_name=None, ipv6_address_count=None, key_name=None, key_pair=None, placement_group=None, private_ip_address=None, propagate_tags_to_volume_on_creation=None, require_imdsv2=None, resource_signal_timeout=None, role=None, security_group=None, source_dest_check=None, ssm_session_permissions=None, user_data=None, user_data_causes_replacement=None, vpc_subnets=None)

Bases: object

Properties of an EC2 Instance.

Parameters:
  • instance_type (InstanceType) – Type of instance to launch.

  • machine_image (IMachineImage) – AMI to launch.

  • vpc (IVpc) – VPC to launch the instance in.

  • allow_all_ipv6_outbound (Optional[bool]) – Whether the instance could initiate IPv6 connections to anywhere by default. This property is only used when you do not provide a security group. Default: false

  • allow_all_outbound (Optional[bool]) – Whether the instance could initiate connections to anywhere by default. This property is only used when you do not provide a security group. Default: true

  • associate_public_ip_address (Optional[bool]) – Whether to associate a public IP address to the primary network interface attached to this instance. You cannot specify this property and ipv6AddressCount at the same time. Default: - public IP address is automatically assigned based on default behavior

  • availability_zone (Optional[str]) – In which AZ to place the instance within the VPC. Default: - Random zone.

  • block_devices (Optional[Sequence[Union[BlockDevice, Dict[str, Any]]]]) – Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes. Each instance that is launched has an associated root device volume, either an Amazon EBS volume or an instance store volume. You can use block device mappings to specify additional EBS volumes or instance store volumes to attach to an instance when it is launched. Default: - Uses the block device mapping of the AMI

  • credit_specification (Optional[CpuCredits]) – Specifying the CPU credit type for burstable EC2 instance types (T2, T3, T3a, etc). The unlimited CPU credit option is not supported for T3 instances with a dedicated host. Default: - T2 instances are standard, while T3, T4g, and T3a instances are unlimited.

  • detailed_monitoring (Optional[bool]) – Whether “Detailed Monitoring” is enabled for this instance Keep in mind that Detailed Monitoring results in extra charges. Default: - false

  • ebs_optimized (Optional[bool]) – Indicates whether the instance is optimized for Amazon EBS I/O. This optimization provides dedicated throughput to Amazon EBS and an optimized configuration stack to provide optimal Amazon EBS I/O performance. This optimization isn’t available with all instance types. Additional usage charges apply when using an EBS-optimized instance. Default: false

  • enclave_enabled (Optional[bool]) – Whether the instance is enabled for AWS Nitro Enclaves. Nitro Enclaves requires a Nitro-based virtualized parent instance with specific Intel/AMD with at least 4 vCPUs or Graviton with at least 2 vCPUs instance types and Linux/Windows host OS, while the enclave itself supports only Linux OS. You can’t set both enclaveEnabled and hibernationEnabled to true on the same instance. Default: - false

  • hibernation_enabled (Optional[bool]) – Whether the instance is enabled for hibernation. You can’t set both enclaveEnabled and hibernationEnabled to true on the same instance. Default: - false

  • init (Optional[CloudFormationInit]) – Apply the given CloudFormation Init configuration to the instance at startup. Default: - no CloudFormation init

  • init_options (Union[ApplyCloudFormationInitOptions, Dict[str, Any], None]) – Use the given options for applying CloudFormation Init. Describes the configsets to use and the timeout to wait Default: - default options

  • instance_initiated_shutdown_behavior (Optional[InstanceInitiatedShutdownBehavior]) – Indicates whether an instance stops or terminates when you initiate shutdown from the instance (using the operating system command for system shutdown). Default: InstanceInitiatedShutdownBehavior.STOP

  • instance_name (Optional[str]) – The name of the instance. Default: - CDK generated name

  • ipv6_address_count (Union[int, float, None]) – The number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. You cannot specify this property and associatePublicIpAddress at the same time. Default: - For instances associated with an IPv6 subnet, use 1; otherwise, use 0.

  • key_name (Optional[str]) – (deprecated) Name of SSH keypair to grant access to instance. Default: - No SSH access will be possible.

  • key_pair (Optional[IKeyPair]) – The SSH keypair to grant access to the instance. Default: - No SSH access will be possible.

  • placement_group (Optional[IPlacementGroup]) – The placement group that you want to launch the instance into. Default: - no placement group will be used for this instance.

  • private_ip_address (Optional[str]) – Defines a private IP address to associate with an instance. Private IP should be available within the VPC that the instance is build within. Default: - no association

  • propagate_tags_to_volume_on_creation (Optional[bool]) – Propagate the EC2 instance tags to the EBS volumes. Default: - false

  • require_imdsv2 (Optional[bool]) – Whether IMDSv2 should be required on this instance. Default: - false

  • resource_signal_timeout (Optional[Duration]) – The length of time to wait for the resourceSignalCount. The maximum value is 43200 (12 hours). Default: Duration.minutes(5)

  • role (Optional[IRole]) – An IAM role to associate with the instance profile assigned to this Auto Scaling Group. The role must be assumable by the service principal ec2.amazonaws.com: Default: - A role will automatically be created, it can be accessed via the role property

  • security_group (Optional[ISecurityGroup]) – Security Group to assign to this instance. Default: - create new security group

  • source_dest_check (Optional[bool]) – Specifies whether to enable an instance launched in a VPC to perform NAT. This controls whether source/destination checking is enabled on the instance. A value of true means that checking is enabled, and false means that checking is disabled. The value must be false for the instance to perform NAT. Default: true

  • ssm_session_permissions (Optional[bool]) – Add SSM session permissions to the instance role. Setting this to true adds the necessary permissions to connect to the instance using SSM Session Manager. You can do this from the AWS Console. NOTE: Setting this flag to true may not be enough by itself. You must also use an AMI that comes with the SSM Agent, or install the SSM Agent yourself. See Working with SSM Agent in the SSM Developer Guide. Default: false

  • user_data (Optional[UserData]) – Specific UserData to use. The UserData may still be mutated after creation. Default: - A UserData object appropriate for the MachineImage’s Operating System is created.

  • user_data_causes_replacement (Optional[bool]) – Changes to the UserData force replacement. Depending the EC2 instance type, changing UserData either restarts the instance or replaces the instance. - Instance store-backed instances are replaced. - EBS-backed instances are restarted. By default, restarting does not execute the new UserData so you will need a different mechanism to ensure the instance is restarted. Setting this to true will make the instance’s Logical ID depend on the UserData, which will cause CloudFormation to replace it if the UserData changes. Default: - true if initOptions is specified, false otherwise.

  • vpc_subnets (Union[SubnetSelection, Dict[str, Any], None]) – Where to place the instance within the VPC. Default: - Private subnets.

ExampleMetadata:

infused

Example:

# vpc: ec2.IVpc

lb = elb.LoadBalancer(self, "LB",
    vpc=vpc,
    internet_facing=True
)

# instance to add as the target for load balancer.
instance = ec2.Instance(self, "targetInstance",
    vpc=vpc,
    instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
    machine_image=ec2.AmazonLinuxImage(generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2)
)
lb.add_target(elb.InstanceTarget(instance))

Attributes

allow_all_ipv6_outbound

Whether the instance could initiate IPv6 connections to anywhere by default.

This property is only used when you do not provide a security group.

Default:

false

allow_all_outbound

Whether the instance could initiate connections to anywhere by default.

This property is only used when you do not provide a security group.

Default:

true

associate_public_ip_address

Whether to associate a public IP address to the primary network interface attached to this instance.

You cannot specify this property and ipv6AddressCount at the same time.

Default:
  • public IP address is automatically assigned based on default behavior

availability_zone

In which AZ to place the instance within the VPC.

Default:
  • Random zone.

block_devices

Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes.

Each instance that is launched has an associated root device volume, either an Amazon EBS volume or an instance store volume. You can use block device mappings to specify additional EBS volumes or instance store volumes to attach to an instance when it is launched.

Default:
  • Uses the block device mapping of the AMI

See:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html

credit_specification

Specifying the CPU credit type for burstable EC2 instance types (T2, T3, T3a, etc).

The unlimited CPU credit option is not supported for T3 instances with a dedicated host.

Default:
  • T2 instances are standard, while T3, T4g, and T3a instances are unlimited.

detailed_monitoring

Whether “Detailed Monitoring” is enabled for this instance Keep in mind that Detailed Monitoring results in extra charges.

Default:
  • false

See:

http://aws.amazon.com/cloudwatch/pricing/

ebs_optimized

Indicates whether the instance is optimized for Amazon EBS I/O.

This optimization provides dedicated throughput to Amazon EBS and an optimized configuration stack to provide optimal Amazon EBS I/O performance. This optimization isn’t available with all instance types. Additional usage charges apply when using an EBS-optimized instance.

Default:

false

enclave_enabled

Whether the instance is enabled for AWS Nitro Enclaves.

Nitro Enclaves requires a Nitro-based virtualized parent instance with specific Intel/AMD with at least 4 vCPUs or Graviton with at least 2 vCPUs instance types and Linux/Windows host OS, while the enclave itself supports only Linux OS.

You can’t set both enclaveEnabled and hibernationEnabled to true on the same instance.

Default:
  • false

See:

https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html#nitro-enclave-reqs

hibernation_enabled

Whether the instance is enabled for hibernation.

You can’t set both enclaveEnabled and hibernationEnabled to true on the same instance.

Default:
  • false

See:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance-hibernationoptions.html

init

Apply the given CloudFormation Init configuration to the instance at startup.

Default:
  • no CloudFormation init

init_options

Use the given options for applying CloudFormation Init.

Describes the configsets to use and the timeout to wait

Default:
  • default options

instance_initiated_shutdown_behavior

Indicates whether an instance stops or terminates when you initiate shutdown from the instance (using the operating system command for system shutdown).

Default:

InstanceInitiatedShutdownBehavior.STOP

See:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior

instance_name

The name of the instance.

Default:
  • CDK generated name

instance_type

Type of instance to launch.

ipv6_address_count

The number of IPv6 addresses to associate with the primary network interface.

Amazon EC2 chooses the IPv6 addresses from the range of your subnet.

You cannot specify this property and associatePublicIpAddress at the same time.

Default:
  • For instances associated with an IPv6 subnet, use 1; otherwise, use 0.

key_name

(deprecated) Name of SSH keypair to grant access to instance.

Default:
  • No SSH access will be possible.

Deprecated:
Stability:

deprecated

key_pair

The SSH keypair to grant access to the instance.

Default:
  • No SSH access will be possible.

machine_image

AMI to launch.

placement_group

The placement group that you want to launch the instance into.

Default:
  • no placement group will be used for this instance.

private_ip_address

Defines a private IP address to associate with an instance.

Private IP should be available within the VPC that the instance is build within.

Default:
  • no association

propagate_tags_to_volume_on_creation

Propagate the EC2 instance tags to the EBS volumes.

Default:
  • false

require_imdsv2

Whether IMDSv2 should be required on this instance.

Default:
  • false

resource_signal_timeout

The length of time to wait for the resourceSignalCount.

The maximum value is 43200 (12 hours).

Default:

Duration.minutes(5)

role

An IAM role to associate with the instance profile assigned to this Auto Scaling Group.

The role must be assumable by the service principal ec2.amazonaws.com:

Default:
  • A role will automatically be created, it can be accessed via the role property

Example:

role = iam.Role(self, "MyRole",
    assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")
)
security_group

Security Group to assign to this instance.

Default:
  • create new security group

source_dest_check

Specifies whether to enable an instance launched in a VPC to perform NAT.

This controls whether source/destination checking is enabled on the instance. A value of true means that checking is enabled, and false means that checking is disabled. The value must be false for the instance to perform NAT.

Default:

true

ssm_session_permissions

Add SSM session permissions to the instance role.

Setting this to true adds the necessary permissions to connect to the instance using SSM Session Manager. You can do this from the AWS Console.

NOTE: Setting this flag to true may not be enough by itself. You must also use an AMI that comes with the SSM Agent, or install the SSM Agent yourself. See Working with SSM Agent in the SSM Developer Guide.

Default:

false

user_data

Specific UserData to use.

The UserData may still be mutated after creation.

Default:

  • A UserData object appropriate for the MachineImage’s

Operating System is created.

user_data_causes_replacement

Changes to the UserData force replacement.

Depending the EC2 instance type, changing UserData either restarts the instance or replaces the instance.

  • Instance store-backed instances are replaced.

  • EBS-backed instances are restarted.

By default, restarting does not execute the new UserData so you will need a different mechanism to ensure the instance is restarted.

Setting this to true will make the instance’s Logical ID depend on the UserData, which will cause CloudFormation to replace it if the UserData changes.

Default:
  • true if initOptions is specified, false otherwise.

vpc

VPC to launch the instance in.

vpc_subnets

Where to place the instance within the VPC.

Default:
  • Private subnets.