Skip to content

Data Source: awsInstance

Use this data source to get the ID of an Amazon EC2 Instance for use in other resources.

Example Usage

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.dataAwsInstance.DataAwsInstance(this, "foo", {
  filter: [
    {
      name: "image-id",
      values: ["ami-xxxxxxxx"],
    },
    {
      name: "tag:Name",
      values: ["instance-name-tag"],
    },
  ],
  instanceId: "i-instanceid",
});

Argument Reference

  • instanceId - (Optional) Specify the exact Instance ID with which to populate the data source.

  • instanceTags - (Optional) Map of tags, each pair of which must exactly match a pair on the desired Instance.

  • filter - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out describe-instances in the AWS CLI reference.

  • getPasswordData - (Optional) If true, wait for password data to become available and retrieve it. Useful for getting the administrator password for instances running Microsoft Windows. The password data is exported to the passwordData attribute. See GetPasswordData for more information.

  • getUserData - (Optional) Retrieve Base64 encoded User Data contents into the userDataBase64 attribute. A SHA-1 hash of the User Data contents will always be present in the userData attribute. Defaults to false.

\~> NOTE: At least one of filter, instanceTags, or instanceId must be specified.

\~> NOTE: If anything other than a single match is returned by the search, Terraform will fail. Ensure that your search is specific enough to return a single Instance ID only.

Attributes Reference

id is set to the ID of the found Instance. In addition, the following attributes are exported:

\~> NOTE: Some values are not always set and may not be available for interpolation.

  • ami - ID of the AMI used to launch the instance.
  • arn - ARN of the instance.
  • associatePublicIpAddress - Whether or not the Instance is associated with a public IP address or not (Boolean).
  • availabilityZone - Availability zone of the Instance.
  • creditSpecification - Credit specification of the Instance.
  • disableApiStop - Whether or not EC2 Instance Stop Protection](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html#Using_StopProtection) is enabled (Boolean).
  • disableApiTermination - Whether or not EC2 Instance Termination Protection is enabled (Boolean).
  • ebsBlockDevice - EBS block device mappings of the Instance.
  • deleteOnTermination - If the EBS volume will be deleted on termination.
  • deviceName - Physical name of the device.
  • encrypted - If the EBS volume is encrypted.
  • iops - 0 If the EBS volume is not a provisioned IOPS image, otherwise the supported IOPS count.
  • kmsKeyArn - ARN of KMS Key, if EBS volume is encrypted.
  • snapshotId - ID of the snapshot.
  • throughput - Throughput of the volume, in MiB/s.
  • volumeSize - Size of the volume, in GiB.
  • volumeType - Volume type.
  • ebsOptimized - Whether the Instance is EBS optimized or not (Boolean).
  • enclaveOptions - Enclave options of the instance.
  • enabled - Whether Nitro Enclaves are enabled.
  • ephemeralBlockDevice - Ephemeral block device mappings of the Instance.
  • deviceName - Physical name of the device.
  • noDevice - Whether the specified device included in the device mapping was suppressed or not (Boolean).
  • virtualName - Virtual device name.
  • hostId - ID of the dedicated host the instance will be assigned to.
  • hostResourceGroupArn - ARN of the host resource group the instance is associated with.
  • iamInstanceProfile - Name of the instance profile associated with the Instance.
  • instanceState - State of the instance. One of: pending, running, shuttingDown, terminated, stopping, stopped. See Instance Lifecycle for more information.
  • instanceType - Type of the Instance.
  • ipv6Addresses - IPv6 addresses associated to the Instance, if applicable. NOTE: Unlike the IPv4 address, this doesn't change if you attach an EIP to the instance.
  • keyName - Key name of the Instance.
  • maintenanceOptions - Maintenance and recovery options for the instance.
  • autoRecovery - Automatic recovery behavior of the instance.
  • metadataOptions - Metadata options of the Instance.
  • httpEndpoint - State of the metadata service: enabled, disabled.
  • httpTokens - If session tokens are required: optional, required.
  • httpPutResponseHopLimit - Desired HTTP PUT response hop limit for instance metadata requests.
  • instanceMetadataTags - If access to instance tags is allowed from the metadata service: enabled, disabled.
  • monitoring - Whether detailed monitoring is enabled or disabled for the Instance (Boolean).
  • networkInterfaceId - ID of the network interface that was created with the Instance.
  • outpostArn - ARN of the Outpost.
  • passwordData - Base-64 encoded encrypted password data for the instance. Useful for getting the administrator password for instances running Microsoft Windows. This attribute is only exported if getPasswordData is true. See GetPasswordData for more information.
  • placementGroup - Placement group of the Instance.
  • placementPartitionNumber - Number of the partition the instance is in.
  • privateDns - Private DNS name assigned to the Instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC.
  • privateDnsNameOptions - Options for the instance hostname.
  • enableResourceNameDnsAaaaRecord - Indicates whether to respond to DNS queries for instance hostnames with DNS AAAA records.
  • enableResourceNameDnsARecord - Indicates whether to respond to DNS queries for instance hostnames with DNS A records.
  • hostnameType - Type of hostname for EC2 instances.
  • privateIp - Private IP address assigned to the Instance.
  • publicDns - Public DNS name assigned to the Instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC.
  • publicIp - Public IP address assigned to the Instance, if applicable. NOTE: If you are using an awsEip with your instance, you should refer to the EIP's address directly and not use publicIp, as this field will change after the EIP is attached.
  • rootBlockDevice - Root block device mappings of the Instance
  • deviceName - Physical name of the device.
  • deleteOnTermination - If the root block device will be deleted on termination.
  • encrypted - If the EBS volume is encrypted.
  • iops - 0 If the volume is not a provisioned IOPS image, otherwise the supported IOPS count.
  • kmsKeyArn - ARN of KMS Key, if EBS volume is encrypted.
  • throughput - Throughput of the volume, in MiB/s.
  • volumeSize - Size of the volume, in GiB.
  • volumeType - Type of the volume.
  • secondaryPrivateIps - Secondary private IPv4 addresses assigned to the instance's primary network interface (eth0) in a VPC.
  • securityGroups - Associated security groups.
  • sourceDestCheck - Whether the network interface performs source/destination checking (Boolean).
  • subnetId - VPC subnet ID.
  • tags - Map of tags assigned to the Instance.
  • tenancy - Tenancy of the instance: dedicated, default, host.
  • userData - SHA-1 hash of User Data supplied to the Instance.
  • userDataBase64 - Base64 encoded contents of User Data supplied to the Instance. Valid UTF-8 contents can be decoded with the base64Decode function. This attribute is only exported if getUserData is true.
  • vpcSecurityGroupIds - Associated security groups in a non-default VPC.

Timeouts

Configuration options:

  • read - (Default 20M)