Instance

class aws_cdk.aws_ec2.Instance(scope, id, *, instance_type, machine_image, vpc, allow_all_outbound=None, availability_zone=None, instance_name=None, key_name=None, resource_signal_timeout=None, role=None, security_group=None, source_dest_check=None, user_data=None, vpc_subnets=None)

Bases: aws_cdk.core.Resource

This represents a single EC2 instance.

__init__(scope, id, *, instance_type, machine_image, vpc, allow_all_outbound=None, availability_zone=None, instance_name=None, key_name=None, resource_signal_timeout=None, role=None, security_group=None, source_dest_check=None, user_data=None, vpc_subnets=None)
Parameters
  • scope (Construct) –

  • id (str) –

  • props

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

  • machine_image (IMachineImage) – AMI to launch.

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

  • 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

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

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

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

  • 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

  • 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.

  • vpc_subnets (Optional[SubnetSelection]) – Where to place the instance within the VPC. Default: - Private subnets.

Return type

None

Methods

add_security_group(security_group)

Add the security group to the instance.

Parameters

security_group (ISecurityGroup) – : The security group to add.

Return type

None

add_to_role_policy(statement)

Adds a statement to the IAM role assumed by the instance.

Parameters

statement (PolicyStatement) –

Return type

None

add_user_data(*commands)

Add command to the startup script of the instance. The command must be in the scripting language supported by the instance’s OS (i.e. Linux/Windows).

Parameters

commands (str) –

Return type

None

to_string()

Returns a string representation of this construct.

Return type

str

Attributes

connections

Allows specify security group connections for the instance.

Return type

Connections

grant_principal

The principal to grant permissions to.

Return type

IPrincipal

instance

the underlying instance resource.

Return type

CfnInstance

instance_availability_zone

The availability zone the instance was launched in.

attribute: :attribute:: true

Return type

str

instance_id

The instance’s ID.

attribute: :attribute:: true

Return type

str

instance_private_dns_name

Private DNS name for this instance.

attribute: :attribute:: true

Return type

str

instance_private_ip

Private IP for this instance.

attribute: :attribute:: true

Return type

str

instance_public_dns_name

Publicly-routable DNS name for this instance.

(May be an empty string if the instance does not have a public name).

attribute: :attribute:: true

Return type

str

instance_public_ip

Publicly-routable IP address for this instance.

(May be an empty string if the instance does not have a public IP).

attribute: :attribute:: true

Return type

str

node

Construct tree node which offers APIs for interacting with the construct tree.

Return type

ConstructNode

os_type

The type of OS the instance is running.

Return type

OperatingSystemType

role

The IAM role assumed by the instance.

Return type

IRole

stack

The stack in which this resource is defined.

Return type

Stack

user_data

UserData for the instance.

Return type

UserData

Static Methods

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters

x (Any) –

Return type

bool