Show / Hide Table of Contents

Interface IInstanceProps

Properties of an EC2 Instance.

Namespace: Amazon.CDK.AWS.EC2
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public interface IInstanceProps
Syntax (vb)
Public Interface IInstanceProps
Remarks

ExampleMetadata: infused

Examples
// Creates a distribution from an EC2 instance
            Vpc vpc;

            // Create an EC2 instance in a VPC. 'subnetType' can be private.
            var instance = new Instance(this, "Instance", new InstanceProps {
                Vpc = vpc,
                InstanceType = InstanceType.Of(InstanceClass.BURSTABLE3, InstanceSize.MICRO),
                MachineImage = MachineImage.LatestAmazonLinux2023(),
                VpcSubnets = new SubnetSelection { SubnetType = SubnetType.PRIVATE_WITH_EGRESS }
            });
            new Distribution(this, "myDist", new DistributionProps {
                DefaultBehavior = new BehaviorOptions { Origin = VpcOrigin.WithEc2Instance(instance) }
            });

Synopsis

Properties

AllowAllIpv6Outbound

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

AllowAllOutbound

Whether the instance could initiate connections to anywhere by default.

AssociatePublicIpAddress

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

AvailabilityZone

In which AZ to place the instance within the VPC.

BlockDevices

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

CreditSpecification

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

DetailedMonitoring

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

DisableApiTermination

If true, the instance will not be able to be terminated using the Amazon EC2 console, CLI, or API.

EbsOptimized

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

EnclaveEnabled

Whether the instance is enabled for AWS Nitro Enclaves.

HibernationEnabled

Whether the instance is enabled for hibernation.

Init

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

InitOptions

Use the given options for applying CloudFormation Init.

InstanceInitiatedShutdownBehavior

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

InstanceName

The name of the instance.

InstanceProfile

The instance profile used to pass role information to EC2 instances.

InstanceType

Type of instance to launch.

Ipv6AddressCount

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

KeyName

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

KeyPair

The SSH keypair to grant access to the instance.

MachineImage

AMI to launch.

PlacementGroup

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

PrivateIpAddress

Defines a private IP address to associate with an instance.

PropagateTagsToVolumeOnCreation

Propagate the EC2 instance tags to the EBS volumes.

RequireImdsv2

Whether IMDSv2 should be required on this instance.

ResourceSignalTimeout

The length of time to wait for the resourceSignalCount.

Role

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

SecurityGroup

Security Group to assign to this instance.

SourceDestCheck

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

SsmSessionPermissions

Add SSM session permissions to the instance role.

UserData

Specific UserData to use.

UserDataCausesReplacement

Changes to the UserData force replacement.

Vpc

VPC to launch the instance in.

VpcSubnets

Where to place the instance within the VPC.

Properties

AllowAllIpv6Outbound

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

bool? AllowAllIpv6Outbound { get; }
Property Value

bool?

Remarks

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

Default: false

AllowAllOutbound

Whether the instance could initiate connections to anywhere by default.

bool? AllowAllOutbound { get; }
Property Value

bool?

Remarks

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

Default: true

AssociatePublicIpAddress

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

bool? AssociatePublicIpAddress { get; }
Property Value

bool?

Remarks

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

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

AvailabilityZone

In which AZ to place the instance within the VPC.

string? AvailabilityZone { get; }
Property Value

string

Remarks

Default: - Random zone.

BlockDevices

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

IBlockDevice[]? BlockDevices { get; }
Property Value

IBlockDevice[]

Remarks

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

CreditSpecification

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

CpuCredits? CreditSpecification { get; }
Property Value

CpuCredits?

Remarks

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.

DetailedMonitoring

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

bool? DetailedMonitoring { get; }
Property Value

bool?

Remarks

Default: - false

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

DisableApiTermination

If true, the instance will not be able to be terminated using the Amazon EC2 console, CLI, or API.

bool? DisableApiTermination { get; }
Property Value

bool?

Remarks

To change this attribute after launch, use ModifyInstanceAttribute. Alternatively, if you set InstanceInitiatedShutdownBehavior to terminate, you can terminate the instance by running the shutdown command from the instance.

Default: false

See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-instance.html#cfn-ec2-instance-disableapitermination

EbsOptimized

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

bool? EbsOptimized { get; }
Property Value

bool?

Remarks

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

EnclaveEnabled

Whether the instance is enabled for AWS Nitro Enclaves.

bool? EnclaveEnabled { get; }
Property Value

bool?

Remarks

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

HibernationEnabled

Whether the instance is enabled for hibernation.

bool? HibernationEnabled { get; }
Property Value

bool?

Remarks

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.

CloudFormationInit? Init { get; }
Property Value

CloudFormationInit

Remarks

Default: - no CloudFormation init

InitOptions

Use the given options for applying CloudFormation Init.

IApplyCloudFormationInitOptions? InitOptions { get; }
Property Value

IApplyCloudFormationInitOptions

Remarks

Describes the configsets to use and the timeout to wait

Default: - default options

InstanceInitiatedShutdownBehavior

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

InstanceInitiatedShutdownBehavior? InstanceInitiatedShutdownBehavior { get; }
Property Value

InstanceInitiatedShutdownBehavior?

Remarks

Default: InstanceInitiatedShutdownBehavior.STOP

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

InstanceName

The name of the instance.

string? InstanceName { get; }
Property Value

string

Remarks

Default: - CDK generated name

InstanceProfile

The instance profile used to pass role information to EC2 instances.

IInstanceProfile? InstanceProfile { get; }
Property Value

IInstanceProfile

Remarks

Note: You can provide an instanceProfile or a role, but not both.

Default: - No instance profile

InstanceType

Type of instance to launch.

InstanceType InstanceType { get; }
Property Value

InstanceType

Remarks

ExampleMetadata: infused

Ipv6AddressCount

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

double? Ipv6AddressCount { get; }
Property Value

double?

Remarks

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.

KeyName

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

[Obsolete("- Use `keyPair` instead - https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2-readme.html#using-an-existing-ec2-key-pair")]
string? KeyName { get; }
Property Value

string

Remarks

Default: - No SSH access will be possible.

Stability: Deprecated

KeyPair

The SSH keypair to grant access to the instance.

IKeyPair? KeyPair { get; }
Property Value

IKeyPair

Remarks

Default: - No SSH access will be possible.

MachineImage

AMI to launch.

IMachineImage MachineImage { get; }
Property Value

IMachineImage

Remarks

ExampleMetadata: infused

PlacementGroup

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

IPlacementGroupRef? PlacementGroup { get; }
Property Value

IPlacementGroupRef

Remarks

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

PrivateIpAddress

Defines a private IP address to associate with an instance.

string? PrivateIpAddress { get; }
Property Value

string

Remarks

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

Default: - no association

PropagateTagsToVolumeOnCreation

Propagate the EC2 instance tags to the EBS volumes.

bool? PropagateTagsToVolumeOnCreation { get; }
Property Value

bool?

Remarks

Default: - false

RequireImdsv2

Whether IMDSv2 should be required on this instance.

bool? RequireImdsv2 { get; }
Property Value

bool?

Remarks

Default: - false

ResourceSignalTimeout

The length of time to wait for the resourceSignalCount.

Duration? ResourceSignalTimeout { get; }
Property Value

Duration

Remarks

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.

IRole? Role { get; }
Property Value

IRole

Remarks

The role must be assumable by the service principal ec2.amazonaws.com: Note: You can provide an instanceProfile or a role, but not both.

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

Examples
var role = new Role(this, "MyRole", new RoleProps {
                 AssumedBy = new ServicePrincipal("ec2.amazonaws.com")
             });

SecurityGroup

Security Group to assign to this instance.

ISecurityGroup? SecurityGroup { get; }
Property Value

ISecurityGroup

Remarks

Default: - create new security group

SourceDestCheck

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

bool? SourceDestCheck { get; }
Property Value

bool?

Remarks

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

SsmSessionPermissions

Add SSM session permissions to the instance role.

bool? SsmSessionPermissions { get; }
Property Value

bool?

Remarks

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

UserData

Specific UserData to use.

UserData? UserData { get; }
Property Value

UserData

Remarks

The UserData may still be mutated after creation.

Default: - A UserData object appropriate for the MachineImage's Operating System is created.

UserDataCausesReplacement

Changes to the UserData force replacement.

bool? UserDataCausesReplacement { get; }
Property Value

bool?

Remarks

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

    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.

    IVpc Vpc { get; }
    Property Value

    IVpc

    Remarks

    ExampleMetadata: infused

    VpcSubnets

    Where to place the instance within the VPC.

    ISubnetSelection? VpcSubnets { get; }
    Property Value

    ISubnetSelection

    Remarks

    Default: - Private subnets.

    Back to top Generated by DocFX