Class: AWSCDK::EC2::Instance

Inherits:
Resource
  • Object
show all
Includes:
IInstance
Defined in:
ec2/instance.rb

Overview

This represents a single EC2 instance.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, id, props) ⇒ Instance

Returns a new instance of Instance.

Parameters:



12
13
14
15
16
17
18
# File 'ec2/instance.rb', line 12

def initialize(scope, id, props)
  props = props.is_a?(Hash) ? ::AWSCDK::EC2::InstanceProps.new(**props.transform_keys(&:to_sym)) : props
  Jsii::Type.check_type(scope, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJjb25zdHJ1Y3RzLkNvbnN0cnVjdCJ9")), "scope")
  Jsii::Type.check_type(id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "id")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfZWMyLkluc3RhbmNlUHJvcHMifQ==")), "props")
  Jsii::Object.instance_method(:initialize).bind(self).call(scope, id, props)
end

Class Method Details

.jsii_overridable_methodsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ec2/instance.rb', line 20

def self.jsii_overridable_methods
  {
    :node => { kind: :property, name: "node", is_optional: false },
    :env => { kind: :property, name: "env", is_optional: false },
    :physical_name => { kind: :property, name: "physicalName", is_optional: false },
    :stack => { kind: :property, name: "stack", is_optional: false },
    :connections => { kind: :property, name: "connections", is_optional: false },
    :grant_principal => { kind: :property, name: "grantPrincipal", is_optional: false },
    :instance => { kind: :property, name: "instance", is_optional: false },
    :instance_availability_zone => { kind: :property, name: "instanceAvailabilityZone", is_optional: false },
    :instance_id => { kind: :property, name: "instanceId", is_optional: false },
    :instance_private_dns_name => { kind: :property, name: "instancePrivateDnsName", is_optional: false },
    :instance_private_ip => { kind: :property, name: "instancePrivateIp", is_optional: false },
    :instance_public_dns_name => { kind: :property, name: "instancePublicDnsName", is_optional: false },
    :instance_public_ip => { kind: :property, name: "instancePublicIp", is_optional: false },
    :instance_ref => { kind: :property, name: "instanceRef", is_optional: false },
    :os_type => { kind: :property, name: "osType", is_optional: false },
    :role => { kind: :property, name: "role", is_optional: false },
    :user_data => { kind: :property, name: "userData", is_optional: false },
    :to_string => { kind: :method, name: "toString", is_optional: false },
    :with => { kind: :method, name: "with", is_optional: false },
    :apply_cross_stack_reference_strength => { kind: :method, name: "applyCrossStackReferenceStrength", is_optional: false },
    :apply_removal_policy => { kind: :method, name: "applyRemovalPolicy", is_optional: false },
    :generate_physical_name => { kind: :method, name: "generatePhysicalName", is_optional: false },
    :get_resource_arn_attribute => { kind: :method, name: "getResourceArnAttribute", is_optional: false },
    :get_resource_name_attribute => { kind: :method, name: "getResourceNameAttribute", is_optional: false },
    :add_security_group => { kind: :method, name: "addSecurityGroup", is_optional: false },
    :add_to_role_policy => { kind: :method, name: "addToRolePolicy", is_optional: false },
    :add_user_data => { kind: :method, name: "addUserData", is_optional: false },
    :apply_cloud_formation_init => { kind: :method, name: "applyCloudFormationInit", is_optional: false },
  }
end

.PROPERTY_INJECTION_IDString

Uniquely identifies this class.

Returns:

  • (String)


99
100
101
# File 'ec2/instance.rb', line 99

def self.PROPERTY_INJECTION_ID()
  Jsii::Kernel.instance.get_static("aws-cdk-lib.aws_ec2.Instance", "PROPERTY_INJECTION_ID")
end

Instance Method Details

#add_security_group(security_group) ⇒ void

This method returns an undefined value.

Add the security group to the instance.

Parameters:



292
293
294
295
# File 'ec2/instance.rb', line 292

def add_security_group(security_group)
  Jsii::Type.check_type(security_group, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfZWMyLklTZWN1cml0eUdyb3VwIn0=")), "securityGroup")
  jsii_call_method("addSecurityGroup", [security_group])
end

#add_to_role_policy(statement) ⇒ void

This method returns an undefined value.

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

Parameters:



301
302
303
304
# File 'ec2/instance.rb', line 301

def add_to_role_policy(statement)
  Jsii::Type.check_type(statement, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfaWFtLlBvbGljeVN0YXRlbWVudCJ9")), "statement")
  jsii_call_method("addToRolePolicy", [statement])
end

#add_user_data(*commands) ⇒ void

This method returns an undefined value.

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 (Array<String>)


312
313
314
315
316
317
# File 'ec2/instance.rb', line 312

def add_user_data(*commands)
  commands.each_with_index do |item, index|
    Jsii::Type.check_type(item, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "commands[#{index}]")
  end
  jsii_call_method("addUserData", [*commands])
end

#apply_cloud_formation_init(init, options = nil) ⇒ void

This method returns an undefined value.

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-init and cfn-signal.
  • Update the instance's CreationPolicy to wait for the cfn-signal commands.

Parameters:



330
331
332
333
334
335
# File 'ec2/instance.rb', line 330

def apply_cloud_formation_init(init, options = nil)
  Jsii::Type.check_type(init, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfZWMyLkNsb3VkRm9ybWF0aW9uSW5pdCJ9")), "init")
  options = options.is_a?(Hash) ? ::AWSCDK::EC2::ApplyCloudFormationInitOptions.new(**options.transform_keys(&:to_sym)) : options
  Jsii::Type.check_type(options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfZWMyLkFwcGx5Q2xvdWRGb3JtYXRpb25Jbml0T3B0aW9ucyJ9")), "options") unless options.nil?
  jsii_call_method("applyCloudFormationInit", [init, options])
end

#apply_cross_stack_reference_strength(strength) ⇒ void

This method returns an undefined value.

Override the cross-stack reference strength for this resource.

When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.

Parameters:



231
232
233
234
# File 'ec2/instance.rb', line 231

def apply_cross_stack_reference_strength(strength)
  Jsii::Type.check_type(strength, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5SZWZlcmVuY2VTdHJlbmd0aCJ9")), "strength")
  jsii_call_method("applyCrossStackReferenceStrength", [strength])
end

#apply_removal_policy(policy) ⇒ void

This method returns an undefined value.

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:



248
249
250
251
# File 'ec2/instance.rb', line 248

def apply_removal_policy(policy)
  Jsii::Type.check_type(policy, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5SZW1vdmFsUG9saWN5In0=")), "policy")
  jsii_call_method("applyRemovalPolicy", [policy])
end

#connectionsAWSCDK::EC2::Connections

Allows specify security group connections for the instance.



106
107
108
# File 'ec2/instance.rb', line 106

def connections()
  jsii_get_property("connections")
end

#envAWSCDK::Interfaces::ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.

For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.



71
72
73
# File 'ec2/instance.rb', line 71

def env()
  jsii_get_property("env")
end

#generate_physical_nameString

Returns:

  • (String)


254
255
256
# File 'ec2/instance.rb', line 254

def generate_physical_name()
  jsii_call_method("generatePhysicalName", [])
end

#get_resource_arn_attribute(arn_attr, arn_components) ⇒ String

Returns an environment-sensitive token that should be used for the resource's "ARN" attribute (e.g. bucket.bucketArn).

Normally, this token will resolve to arn_attr, but if the resource is referenced across environments, arn_components will be used to synthesize a concrete ARN with the resource's physical name. Make sure to reference this.physicalName in arn_components.

Parameters:

  • arn_attr (String)

    The CFN attribute which resolves to the ARN of the resource.

  • arn_components (AWSCDK::ARNComponents)

    The format of the ARN of this resource.

Returns:

  • (String)


268
269
270
271
272
273
# File 'ec2/instance.rb', line 268

def get_resource_arn_attribute(arn_attr, arn_components)
  Jsii::Type.check_type(arn_attr, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "arnAttr")
  arn_components = arn_components.is_a?(Hash) ? ::AWSCDK::ARNComponents.new(**arn_components.transform_keys(&:to_sym)) : arn_components
  Jsii::Type.check_type(arn_components, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5Bcm5Db21wb25lbnRzIn0=")), "arnComponents")
  jsii_call_method("getResourceArnAttribute", [arn_attr, arn_components])
end

#get_resource_name_attribute(name_attr) ⇒ String

Returns an environment-sensitive token that should be used for the resource's "name" attribute (e.g. bucket.bucketName).

Normally, this token will resolve to name_attr, but if the resource is referenced across environments, it will be resolved to this.physicalName, which will be a concrete name.

Parameters:

  • name_attr (String)

    The CFN attribute which resolves to the resource's name.

Returns:

  • (String)


283
284
285
286
# File 'ec2/instance.rb', line 283

def get_resource_name_attribute(name_attr)
  Jsii::Type.check_type(name_attr, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "nameAttr")
  jsii_call_method("getResourceNameAttribute", [name_attr])
end

#grant_principalAWSCDK::IAM::IPrincipal

The principal to grant permissions to.



113
114
115
# File 'ec2/instance.rb', line 113

def grant_principal()
  jsii_get_property("grantPrincipal")
end

#instanceAWSCDK::EC2::CfnInstance

the underlying instance resource.



120
121
122
# File 'ec2/instance.rb', line 120

def instance()
  jsii_get_property("instance")
end

#instance_availability_zoneString

The availability zone the instance was launched in.

Returns:

  • (String)


127
128
129
# File 'ec2/instance.rb', line 127

def instance_availability_zone()
  jsii_get_property("instanceAvailabilityZone")
end

#instance_idString

The instance's ID.

Returns:

  • (String)


134
135
136
# File 'ec2/instance.rb', line 134

def instance_id()
  jsii_get_property("instanceId")
end

#instance_private_dns_nameString

Private DNS name for this instance.

Returns:

  • (String)


141
142
143
# File 'ec2/instance.rb', line 141

def instance_private_dns_name()
  jsii_get_property("instancePrivateDnsName")
end

#instance_private_ipString

Private IP for this instance.

Returns:

  • (String)


148
149
150
# File 'ec2/instance.rb', line 148

def instance_private_ip()
  jsii_get_property("instancePrivateIp")
end

#instance_public_dns_nameString

Publicly-routable DNS name for this instance.

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

Returns:

  • (String)


157
158
159
# File 'ec2/instance.rb', line 157

def instance_public_dns_name()
  jsii_get_property("instancePublicDnsName")
end

#instance_public_ipString

Publicly-routable IP address for this instance.

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

Returns:

  • (String)


166
167
168
# File 'ec2/instance.rb', line 166

def instance_public_ip()
  jsii_get_property("instancePublicIp")
end

#instance_refAWSCDK::Interfaces::AWSEC2::InstanceReference

A reference to a Instance resource.



173
174
175
# File 'ec2/instance.rb', line 173

def instance_ref()
  jsii_get_property("instanceRef")
end

#nodeConstructs::Node

The tree node.

Returns:

  • (Constructs::Node)


56
57
58
# File 'ec2/instance.rb', line 56

def node()
  jsii_get_property("node")
end

#os_typeAWSCDK::EC2::OperatingSystemType

The type of OS the instance is running.



180
181
182
# File 'ec2/instance.rb', line 180

def os_type()
  jsii_get_property("osType")
end

#physical_nameString

Returns a string-encoded token that resolves to the physical name that should be passed to the CloudFormation resource.

This value will resolve to one of the following:

  • a concrete value (e.g. "my-awesome-bucket")
  • undefined, when a name should be generated by CloudFormation
  • a concrete name generated automatically during synthesis, in cross-environment scenarios.

Returns:

  • (String)


85
86
87
# File 'ec2/instance.rb', line 85

def physical_name()
  jsii_get_property("physicalName")
end

#roleAWSCDK::IAM::IRole

The IAM role assumed by the instance.

Returns:



187
188
189
# File 'ec2/instance.rb', line 187

def role()
  jsii_get_property("role")
end

#stackAWSCDK::Stack

The stack in which this resource is defined.

Returns:



92
93
94
# File 'ec2/instance.rb', line 92

def stack()
  jsii_get_property("stack")
end

#to_stringString

Returns a string representation of this construct.

Returns:

  • (String)


201
202
203
# File 'ec2/instance.rb', line 201

def to_string()
  jsii_call_method("toString", [])
end

#user_dataAWSCDK::EC2::UserData

UserData for the instance.



194
195
196
# File 'ec2/instance.rb', line 194

def user_data()
  jsii_get_property("userData")
end

#with(*mixins) ⇒ Constructs::IConstruct

Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.

Parameters:

  • mixins (Array<Constructs::IMixin>)

Returns:

  • (Constructs::IConstruct)


214
215
216
217
218
219
# File 'ec2/instance.rb', line 214

def with(*mixins)
  mixins.each_with_index do |item, index|
    Jsii::Type.check_type(item, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJjb25zdHJ1Y3RzLklNaXhpbiJ9")), "mixins[#{index}]")
  end
  jsii_call_method("with", [*mixins])
end