I just started learning pulumi and I am having tro...
# aws
a
I just started learning pulumi and I am having trouble in using the existing keypair to the ec2 instance I have created. I've been searching and reading for hours now but still could not find the answer. I found this documentation, https://www.pulumi.com/docs/reference/pkg/aws/ec2/keypair/, lack of detail and example on how to use this. Example existing KeyPair on AWS Name: MyExampleKeyPair ID: key-0f0e4
Copy code
import pulumi
import pulumi_aws as aws 

size = 't2.micro'
ami = aws.get_ami(most_recent="true",
				  owners=["amazon"],
				  filters=[{"name":"name", "values":["amzn-ami-hvm-*"]}]
				)

group = aws.ec2.SecurityGroup('webserver-secgrp',
							  description="Enable HTTP access",
							  ingress=[
							  			{ 'protocol': 'tcp', 'from_port': 22, 'to_port': 22, 'cidr_blocks': ['0.0.0.0/0'] },
							  			{ 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] }
							  		  ]
								)

user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &
"""

server = aws.ec2.Instance('webserver-www',
						  instance_type=size,
						  vpc_security_group_ids = [group.id],
						  user_data=user_data,
						  ami=ami.id
						  )

pulumi.export('publicIp', server.public_ip)
pulumi.export('publicHostName', server.public_dns)
My question is how am I going to add that on the code above? I've been trying different code but still not successful in adding the existing keypair.
b
Hi @average-nest-71706 Welcome to Pulumi! Let me know what you've tried and I can help you triage why it's not working
There is currently no resource to look up an existing KeyPair
but an existing Key will work perfectly if you know the ID to use when attaching to the Instance
a
Hi @broad-dog-22463 What confuses me is the line of code here:
Copy code
@staticmethod
def get(resource_name: str, id: str, opts: Optional[ResourceOptions] = None, arn: Optional[str] = None, fingerprint: Optional[str] = None, key_name: Optional[str] = None, key_name_prefix: Optional[str] = None, key_pair_id: Optional[str] = None, public_key: Optional[str] = None, tags: Optional[Mapping[str, str]] = None) -> KeyPair
I'm not sure if it is supposed to be used like this.
Copy code
deployer = aws.ec2.get("FirstKeyPair", id: "key-0f0e4b....")
Or maybe my question is on how to use these
Copy code
@staticmethod
def get
on Pulumi documentation, I hope it will going to have some sort of example usage.
b
Hi @average-nest-71706 So that's if you want to look up an existing keypair - you need to do the following there aws.ec2.KeyPair.get() on that
a
Oh I see.. After that, I did pulumi preview and got this error. "Preview failed: resource 'key-0f0e4b...' does not exist" These keypair is generated from the ec2 instance I manually provisioned before. deployer = aws.ec2.KeyPair.get("FirstKeyPair", id="key-0f0e4bf5b08639dad") Do I still need to do pulumi import?
Copy code
pulumi import aws:ec2/keyPair:KeyPair deployer FirstKeyPair
Copy code
pulumi import aws:ec2/keyPair:KeyPair deployer FirstKeyPair
Previewing import (dev)

View Live: <https://app.pulumi.com/.../webserver/dev/previews/5da907e7-e1b2-49e3-bf7a-3fc87457d87b>

     Type                 Name           Plan       Info
     pulumi:pulumi:Stack  webserver-dev             1 error
 =   └─ aws:ec2:KeyPair   deployer       import     2 errors
 
Diagnostics:
  pulumi:pulumi:Stack (webserver-dev):
    error: preview failed
 
  aws:ec2:KeyPair (deployer):
    error: aws:ec2/keyPair:KeyPair resource 'deployer' has a problem: Required attribute is not set
    error: Preview failed: one or more inputs failed to validate
I have tried to do pulumi import and got this.
b
so it's a manually created keypair that you already know the details of - the benefit of bringing this under pulumi management would just be to reference an ID that you know about I would suggest you can just set the KeyPair as part of your instance without the need to look it up to unblock you The alternative here would be to create a new KeyPair that is under Pulumi's management and will be created as part of te Pulumi run
a
Isn't a KeyPair manually created?
Copy code
import pulumi
import pulumi_aws as aws

deployer = aws.ec2.KeyPair("deployer", public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 <mailto:email@example.com|email@example.com>")
Looking here, you must already have the public key.
b
it is not created unless you tell it to be
when you say manually created - I think what you mean is, that you can specify a location to a public key either with the key material or to a file and read that file and the Pulumi will create that Public Key for you in AWS
Pulumi will not autogenerate a key on your behalf - you must give it source material to reference
a
Ok so here's what I did. I used puttygen to generate a rsa-ssh then do:
Copy code
Enc_key = "ssh-rsa somrandomeautogeneratedkeyhereAAAAEBRGRWQrgc"
deployer = aws.ec2.KeyPair("deployer", public_key=Enc_Key)

server = aws.ec2.Instance('webserver-www',
						  instance_type=size,
						  vpc_security_group_ids = [group.id],
						  user_data=user_data,
						  ami=ami.id,
						  key_name=deployer
						  )
At first it give me an error because deployer value on key_name is not returning the key pair name. So I removed that and do "pulumi up", it was successful but ec2 instance generated still don't have a key pair assigned. I also checked the Key Pair on aws and it generated a key pair for me named "deployer-2f63432". What I did next is updated the aws.ec2.Instance and added key_name="deployer-2f63432" and there, I saw that my ec2 instance now have an assigned keypair.
I just realized that it is supposed to be key_name=deployer.id
b
that's the one!
a
Thank you @broad-dog-22463