AWS v6.71.0 published on Friday, Mar 7, 2025 by Pulumi
aws.ec2.getKeyPair
Explore with Pulumi AI
Use this data source to get information about a specific EC2 Key Pair.
Example Usage
The following example shows how to get a EC2 Key Pair including the public key material from its name.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const example = aws.ec2.getKeyPair({
    keyName: "test",
    includePublicKey: true,
    filters: [{
        name: "tag:Component",
        values: ["web"],
    }],
});
export const fingerprint = example.then(example => example.fingerprint);
export const name = example.then(example => example.keyName);
export const id = example.then(example => example.id);
import pulumi
import pulumi_aws as aws
example = aws.ec2.get_key_pair(key_name="test",
    include_public_key=True,
    filters=[{
        "name": "tag:Component",
        "values": ["web"],
    }])
pulumi.export("fingerprint", example.fingerprint)
pulumi.export("name", example.key_name)
pulumi.export("id", example.id)
package main
import (
	"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		example, err := ec2.LookupKeyPair(ctx, &ec2.LookupKeyPairArgs{
			KeyName:          pulumi.StringRef("test"),
			IncludePublicKey: pulumi.BoolRef(true),
			Filters: []ec2.GetKeyPairFilter{
				{
					Name: "tag:Component",
					Values: []string{
						"web",
					},
				},
			},
		}, nil)
		if err != nil {
			return err
		}
		ctx.Export("fingerprint", example.Fingerprint)
		ctx.Export("name", example.KeyName)
		ctx.Export("id", example.Id)
		return nil
	})
}
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() => 
{
    var example = Aws.Ec2.GetKeyPair.Invoke(new()
    {
        KeyName = "test",
        IncludePublicKey = true,
        Filters = new[]
        {
            new Aws.Ec2.Inputs.GetKeyPairFilterInputArgs
            {
                Name = "tag:Component",
                Values = new[]
                {
                    "web",
                },
            },
        },
    });
    return new Dictionary<string, object?>
    {
        ["fingerprint"] = example.Apply(getKeyPairResult => getKeyPairResult.Fingerprint),
        ["name"] = example.Apply(getKeyPairResult => getKeyPairResult.KeyName),
        ["id"] = example.Apply(getKeyPairResult => getKeyPairResult.Id),
    };
});
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.ec2.Ec2Functions;
import com.pulumi.aws.ec2.inputs.GetKeyPairArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }
    public static void stack(Context ctx) {
        final var example = Ec2Functions.getKeyPair(GetKeyPairArgs.builder()
            .keyName("test")
            .includePublicKey(true)
            .filters(GetKeyPairFilterArgs.builder()
                .name("tag:Component")
                .values("web")
                .build())
            .build());
        ctx.export("fingerprint", example.applyValue(getKeyPairResult -> getKeyPairResult.fingerprint()));
        ctx.export("name", example.applyValue(getKeyPairResult -> getKeyPairResult.keyName()));
        ctx.export("id", example.applyValue(getKeyPairResult -> getKeyPairResult.id()));
    }
}
variables:
  example:
    fn::invoke:
      function: aws:ec2:getKeyPair
      arguments:
        keyName: test
        includePublicKey: true
        filters:
          - name: tag:Component
            values:
              - web
outputs:
  fingerprint: ${example.fingerprint}
  name: ${example.keyName}
  id: ${example.id}
Using getKeyPair
Two invocation forms are available. The direct form accepts plain arguments and either blocks until the result value is available, or returns a Promise-wrapped result. The output form accepts Input-wrapped arguments and returns an Output-wrapped result.
function getKeyPair(args: GetKeyPairArgs, opts?: InvokeOptions): Promise<GetKeyPairResult>
function getKeyPairOutput(args: GetKeyPairOutputArgs, opts?: InvokeOptions): Output<GetKeyPairResult>def get_key_pair(filters: Optional[Sequence[GetKeyPairFilter]] = None,
                 include_public_key: Optional[bool] = None,
                 key_name: Optional[str] = None,
                 key_pair_id: Optional[str] = None,
                 tags: Optional[Mapping[str, str]] = None,
                 opts: Optional[InvokeOptions] = None) -> GetKeyPairResult
def get_key_pair_output(filters: Optional[pulumi.Input[Sequence[pulumi.Input[GetKeyPairFilterArgs]]]] = None,
                 include_public_key: Optional[pulumi.Input[bool]] = None,
                 key_name: Optional[pulumi.Input[str]] = None,
                 key_pair_id: Optional[pulumi.Input[str]] = None,
                 tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None,
                 opts: Optional[InvokeOptions] = None) -> Output[GetKeyPairResult]func LookupKeyPair(ctx *Context, args *LookupKeyPairArgs, opts ...InvokeOption) (*LookupKeyPairResult, error)
func LookupKeyPairOutput(ctx *Context, args *LookupKeyPairOutputArgs, opts ...InvokeOption) LookupKeyPairResultOutput> Note: This function is named LookupKeyPair in the Go SDK.
public static class GetKeyPair 
{
    public static Task<GetKeyPairResult> InvokeAsync(GetKeyPairArgs args, InvokeOptions? opts = null)
    public static Output<GetKeyPairResult> Invoke(GetKeyPairInvokeArgs args, InvokeOptions? opts = null)
}public static CompletableFuture<GetKeyPairResult> getKeyPair(GetKeyPairArgs args, InvokeOptions options)
public static Output<GetKeyPairResult> getKeyPair(GetKeyPairArgs args, InvokeOptions options)
fn::invoke:
  function: aws:ec2/getKeyPair:getKeyPair
  arguments:
    # arguments dictionaryThe following arguments are supported:
- Filters
List<GetKey Pair Filter> 
- Custom filter block as described below.
- IncludePublic boolKey 
- Whether to include the public key material in the response.
- KeyName string
- Key Pair name.
- KeyPair stringId 
- Key Pair ID.
- Dictionary<string, string>
- Any tags assigned to the Key Pair.
- Filters
[]GetKey Pair Filter 
- Custom filter block as described below.
- IncludePublic boolKey 
- Whether to include the public key material in the response.
- KeyName string
- Key Pair name.
- KeyPair stringId 
- Key Pair ID.
- map[string]string
- Any tags assigned to the Key Pair.
- filters
List<GetKey Pair Filter> 
- Custom filter block as described below.
- includePublic BooleanKey 
- Whether to include the public key material in the response.
- keyName String
- Key Pair name.
- keyPair StringId 
- Key Pair ID.
- Map<String,String>
- Any tags assigned to the Key Pair.
- filters
GetKey Pair Filter[] 
- Custom filter block as described below.
- includePublic booleanKey 
- Whether to include the public key material in the response.
- keyName string
- Key Pair name.
- keyPair stringId 
- Key Pair ID.
- {[key: string]: string}
- Any tags assigned to the Key Pair.
- filters
Sequence[GetKey Pair Filter] 
- Custom filter block as described below.
- include_public_ boolkey 
- Whether to include the public key material in the response.
- key_name str
- Key Pair name.
- key_pair_ strid 
- Key Pair ID.
- Mapping[str, str]
- Any tags assigned to the Key Pair.
- filters List<Property Map>
- Custom filter block as described below.
- includePublic BooleanKey 
- Whether to include the public key material in the response.
- keyName String
- Key Pair name.
- keyPair StringId 
- Key Pair ID.
- Map<String>
- Any tags assigned to the Key Pair.
getKeyPair Result
The following output properties are available:
- Arn string
- ARN of the Key Pair.
- CreateTime string
- Timestamp for when the key pair was created in ISO 8601 format.
- Fingerprint string
- SHA-1 digest of the DER encoded private key.
- Id string
- The provider-assigned unique ID for this managed resource.
- KeyType string
- Type of key pair.
- PublicKey string
- Public key material.
- Dictionary<string, string>
- Any tags assigned to the Key Pair.
- Filters
List<GetKey Pair Filter> 
- IncludePublic boolKey 
- KeyName string
- KeyPair stringId 
- Arn string
- ARN of the Key Pair.
- CreateTime string
- Timestamp for when the key pair was created in ISO 8601 format.
- Fingerprint string
- SHA-1 digest of the DER encoded private key.
- Id string
- The provider-assigned unique ID for this managed resource.
- KeyType string
- Type of key pair.
- PublicKey string
- Public key material.
- map[string]string
- Any tags assigned to the Key Pair.
- Filters
[]GetKey Pair Filter 
- IncludePublic boolKey 
- KeyName string
- KeyPair stringId 
- arn String
- ARN of the Key Pair.
- createTime String
- Timestamp for when the key pair was created in ISO 8601 format.
- fingerprint String
- SHA-1 digest of the DER encoded private key.
- id String
- The provider-assigned unique ID for this managed resource.
- keyType String
- Type of key pair.
- publicKey String
- Public key material.
- Map<String,String>
- Any tags assigned to the Key Pair.
- filters
List<GetKey Pair Filter> 
- includePublic BooleanKey 
- keyName String
- keyPair StringId 
- arn string
- ARN of the Key Pair.
- createTime string
- Timestamp for when the key pair was created in ISO 8601 format.
- fingerprint string
- SHA-1 digest of the DER encoded private key.
- id string
- The provider-assigned unique ID for this managed resource.
- keyType string
- Type of key pair.
- publicKey string
- Public key material.
- {[key: string]: string}
- Any tags assigned to the Key Pair.
- filters
GetKey Pair Filter[] 
- includePublic booleanKey 
- keyName string
- keyPair stringId 
- arn str
- ARN of the Key Pair.
- create_time str
- Timestamp for when the key pair was created in ISO 8601 format.
- fingerprint str
- SHA-1 digest of the DER encoded private key.
- id str
- The provider-assigned unique ID for this managed resource.
- key_type str
- Type of key pair.
- public_key str
- Public key material.
- Mapping[str, str]
- Any tags assigned to the Key Pair.
- filters
Sequence[GetKey Pair Filter] 
- include_public_ boolkey 
- key_name str
- key_pair_ strid 
- arn String
- ARN of the Key Pair.
- createTime String
- Timestamp for when the key pair was created in ISO 8601 format.
- fingerprint String
- SHA-1 digest of the DER encoded private key.
- id String
- The provider-assigned unique ID for this managed resource.
- keyType String
- Type of key pair.
- publicKey String
- Public key material.
- Map<String>
- Any tags assigned to the Key Pair.
- filters List<Property Map>
- includePublic BooleanKey 
- keyName String
- keyPair StringId 
Supporting Types
GetKeyPairFilter   
- Name string
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- Values List<string>
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
- Name string
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- Values []string
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
- name String
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- values List<String>
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
- name string
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- values string[]
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
- name str
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- values Sequence[str]
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
- name String
- Name of the filter field. Valid values can be found in the EC2 DescribeKeyPairs API Reference.
- values List<String>
- Set of values that are accepted for the given filter field. Results will be selected if any given value matches.
Package Details
- Repository
- AWS Classic pulumi/pulumi-aws
- License
- Apache-2.0
- Notes
- This Pulumi package is based on the awsTerraform Provider.