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.
virtual Nullable<bool> AllowAllIpv6Outbound { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual Nullable<bool> AllowAllOutbound { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual Nullable<bool> AssociatePublicIpAddress { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual string AvailabilityZone { get; }
Property Value
System.String
Remarks
Default: - Random zone.
BlockDevices
Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes.
virtual IBlockDevice[] BlockDevices { get; }
Property Value
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).
virtual Nullable<CpuCredits> CreditSpecification { get; }
Property Value
System.Nullable<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.
virtual Nullable<bool> DetailedMonitoring { get; }
Property Value
System.Nullable<System.Boolean>
Remarks
Default: - false
DisableApiTermination
If true, the instance will not be able to be terminated using the Amazon EC2 console, CLI, or API.
virtual Nullable<bool> DisableApiTermination { get; }
Property Value
System.Nullable<System.Boolean>
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
EbsOptimized
Indicates whether the instance is optimized for Amazon EBS I/O.
virtual Nullable<bool> EbsOptimized { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual Nullable<bool> EnclaveEnabled { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual Nullable<bool> HibernationEnabled { get; }
Property Value
System.Nullable<System.Boolean>
Remarks
You can't set both enclaveEnabled
and hibernationEnabled
to true on the same instance.
Default: - false
Init
Apply the given CloudFormation Init configuration to the instance at startup.
virtual CloudFormationInit Init { get; }
Property Value
Remarks
Default: - no CloudFormation init
InitOptions
Use the given options for applying CloudFormation Init.
virtual 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).
virtual Nullable<InstanceInitiatedShutdownBehavior> InstanceInitiatedShutdownBehavior { get; }
Property Value
System.Nullable<InstanceInitiatedShutdownBehavior>
Remarks
Default: InstanceInitiatedShutdownBehavior.STOP
InstanceName
The name of the instance.
virtual string InstanceName { get; }
Property Value
System.String
Remarks
Default: - CDK generated name
InstanceProfile
The instance profile used to pass role information to EC2 instances.
virtual IInstanceProfile InstanceProfile { get; }
Property Value
Remarks
Note: You can provide an instanceProfile or a role, but not both.
Default: - No instance profile
InstanceType
Ipv6AddressCount
The number of IPv6 addresses to associate with the primary network interface.
virtual Nullable<double> Ipv6AddressCount { get; }
Property Value
System.Nullable<System.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.
virtual string KeyName { get; }
Property Value
System.String
Remarks
Default: - No SSH access will be possible.
Stability: Deprecated
KeyPair
The SSH keypair to grant access to the instance.
virtual IKeyPair KeyPair { get; }
Property Value
Remarks
Default: - No SSH access will be possible.
MachineImage
PlacementGroup
The placement group that you want to launch the instance into.
virtual IPlacementGroup PlacementGroup { get; }
Property Value
Remarks
Default: - no placement group will be used for this instance.
PrivateIpAddress
Defines a private IP address to associate with an instance.
virtual string PrivateIpAddress { get; }
Property Value
System.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.
virtual Nullable<bool> PropagateTagsToVolumeOnCreation { get; }
Property Value
System.Nullable<System.Boolean>
Remarks
Default: - false
RequireImdsv2
Whether IMDSv2 should be required on this instance.
virtual Nullable<bool> RequireImdsv2 { get; }
Property Value
System.Nullable<System.Boolean>
Remarks
Default: - false
ResourceSignalTimeout
The length of time to wait for the resourceSignalCount.
virtual Duration ResourceSignalTimeout { get; }
Property Value
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.
virtual IRole Role { get; }
Property Value
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.
virtual ISecurityGroup SecurityGroup { get; }
Property Value
Remarks
Default: - create new security group
SourceDestCheck
Specifies whether to enable an instance launched in a VPC to perform NAT.
virtual Nullable<bool> SourceDestCheck { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual Nullable<bool> SsmSessionPermissions { get; }
Property Value
System.Nullable<System.Boolean>
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.
virtual UserData UserData { get; }
Property Value
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.
virtual Nullable<bool> UserDataCausesReplacement { get; }
Property Value
System.Nullable<System.Boolean>
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
VpcSubnets
Where to place the instance within the VPC.
virtual ISubnetSelection VpcSubnets { get; }
Property Value
Remarks
Default: - Private subnets.