Class Instance
java.lang.Object
software.amazon.jsii.JsiiObject
software.constructs.Construct
software.amazon.awscdk.Resource
software.amazon.awscdk.services.ec2.Instance
- All Implemented Interfaces:
IInstanceRef,IEnvironmentAware,IResource,IConnectable,IInstance,IGrantable,software.amazon.jsii.JsiiSerializable,software.constructs.IConstruct,software.constructs.IDependable
@Generated(value="jsii-pacmak/1.119.0 (build 1634eac)",
date="2025-11-13T16:10:03.452Z")
@Stability(Stable)
public class Instance
extends Resource
implements IInstance
This represents a single EC2 instance.
Example:
// Creates a distribution from an EC2 instance
Vpc vpc;
// Create an EC2 instance in a VPC. 'subnetType' can be private.
Instance instance = Instance.Builder.create(this, "Instance")
.vpc(vpc)
.instanceType(InstanceType.of(InstanceClass.BURSTABLE3, InstanceSize.MICRO))
.machineImage(MachineImage.latestAmazonLinux2023())
.vpcSubnets(SubnetSelection.builder().subnetType(SubnetType.PRIVATE_WITH_EGRESS).build())
.build();
Distribution.Builder.create(this, "myDist")
.defaultBehavior(BehaviorOptions.builder().origin(VpcOrigin.withEc2Instance(instance)).build())
.build();
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class software.amazon.jsii.JsiiObject
software.amazon.jsii.JsiiObject.InitializationModeNested classes/interfaces inherited from interface software.constructs.IConstruct
software.constructs.IConstruct.Jsii$DefaultNested classes/interfaces inherited from interface software.amazon.awscdk.services.ec2.IInstance
IInstance.Jsii$Default, IInstance.Jsii$ProxyNested classes/interfaces inherited from interface software.amazon.awscdk.IResource
IResource.Jsii$Default -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstance(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) protectedInstance(software.amazon.jsii.JsiiObjectRef objRef) Instance(software.constructs.Construct scope, String id, InstanceProps props) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSecurityGroup(ISecurityGroup securityGroup) Add the security group to the instance.voidaddToRolePolicy(PolicyStatement statement) Adds a statement to the IAM role assumed by the instance.voidaddUserData(String... commands) Add command to the startup script of the instance.voidUse a CloudFormation Init configuration at instance startup.voidUse a CloudFormation Init configuration at instance startup.Allows specify security group connections for the instance.The principal to grant permissions to.the underlying instance resource.The availability zone the instance was launched in.The instance's ID.Private DNS name for this instance.Private IP for this instance.Publicly-routable DNS name for this instance.Publicly-routable IP address for this instance.A reference to a Instance resource.The type of OS the instance is running.getRole()The IAM role assumed by the instance.UserData for the instance.Methods inherited from class software.amazon.awscdk.Resource
applyRemovalPolicy, generatePhysicalName, getEnv, getPhysicalName, getResourceArnAttribute, getResourceNameAttribute, getStack, isOwnedResource, isResourceMethods inherited from class software.constructs.Construct
getNode, isConstruct, toStringMethods inherited from class software.amazon.jsii.JsiiObject
jsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSetMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface software.amazon.awscdk.IResource
applyRemovalPolicy, getStackMethods inherited from interface software.amazon.jsii.JsiiSerializable
$jsii$toJson
-
Field Details
-
PROPERTY_INJECTION_ID
Uniquely identifies this class.
-
-
Constructor Details
-
Instance
protected Instance(software.amazon.jsii.JsiiObjectRef objRef) -
Instance
protected Instance(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) -
Instance
@Stability(Stable) public Instance(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull InstanceProps props) - Parameters:
scope- This parameter is required.id- This parameter is required.props- This parameter is required.
-
-
Method Details
-
addSecurityGroup
Add the security group to the instance.- Parameters:
securityGroup- : The security group to add. This parameter is required.
-
addToRolePolicy
Adds a statement to the IAM role assumed by the instance.- Parameters:
statement- This parameter is required.
-
addUserData
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- This parameter is required.
-
applyCloudFormationInit
@Stability(Stable) public void applyCloudFormationInit(@NotNull CloudFormationInit init, @Nullable ApplyCloudFormationInitOptions options) Use a CloudFormation Init configuration at instance startup.This does the following:
- Attaches the CloudFormation Init metadata to the Instance resource.
- Add commands to the instance UserData to run
cfn-initandcfn-signal. - Update the instance's CreationPolicy to wait for the
cfn-signalcommands.
- Parameters:
init- This parameter is required.options-
-
applyCloudFormationInit
Use a CloudFormation Init configuration at instance startup.This does the following:
- Attaches the CloudFormation Init metadata to the Instance resource.
- Add commands to the instance UserData to run
cfn-initandcfn-signal. - Update the instance's CreationPolicy to wait for the
cfn-signalcommands.
- Parameters:
init- This parameter is required.
-
getConnections
Allows specify security group connections for the instance.- Specified by:
getConnectionsin interfaceIConnectable
-
getGrantPrincipal
The principal to grant permissions to.- Specified by:
getGrantPrincipalin interfaceIGrantable
-
getInstance
the underlying instance resource. -
getInstanceAvailabilityZone
The availability zone the instance was launched in.- Specified by:
getInstanceAvailabilityZonein interfaceIInstance
-
getInstanceId
The instance's ID.- Specified by:
getInstanceIdin interfaceIInstance
-
getInstancePrivateDnsName
Private DNS name for this instance.- Specified by:
getInstancePrivateDnsNamein interfaceIInstance
-
getInstancePrivateIp
Private IP for this instance.- Specified by:
getInstancePrivateIpin interfaceIInstance
-
getInstancePublicDnsName
Publicly-routable DNS name for this instance.(May be an empty string if the instance does not have a public name).
- Specified by:
getInstancePublicDnsNamein interfaceIInstance
-
getInstancePublicIp
Publicly-routable IP address for this instance.(May be an empty string if the instance does not have a public IP).
- Specified by:
getInstancePublicIpin interfaceIInstance
-
getInstanceRef
A reference to a Instance resource.- Specified by:
getInstanceRefin interfaceIInstanceRef
-
getOsType
The type of OS the instance is running. -
getRole
The IAM role assumed by the instance. -
getUserData
UserData for the instance.
-