https://pulumi.com logo
#getting-started
Title
# getting-started
k

kind-carpenter-52856

07/16/2021, 8:56 AM
hi all, Just join the slack forum for pulumi, I would like to ask Is there any example code to add keypair to instance ec2 aws (i hope example in golang :)) Thanks a lot
b

brave-planet-10645

07/16/2021, 9:58 AM
There's an example in our docs which explains how to create a keypair: https://www.pulumi.com/docs/reference/pkg/aws/ec2/keypair/#example-usage Then when you create the EC2 instance you can just pass the name into the
KeyName
input https://www.pulumi.com/docs/reference/pkg/aws/ec2/instance/#keyname_go
k

kind-carpenter-52856

07/16/2021, 1:27 PM
hi @brave-planet-10645, thx for reply, yes i already read the documentation, my issue is attaching keypair to ec2, here is my code : https://pastebin.com/5Bw8ZnyY If you have a time to take a look there is section “// Create a simple web server using the startup script for the instance.” I want to put “KeyName” to attach keypair to EC2, but if i try to add KeyName, it wouldn’t accept string. Any Advice?
b

brave-planet-10645

07/16/2021, 1:27 PM
You need to create the keypair before the instance
so you'd have something like this:
Copy code
keypair, err = ec2.NewKeyPair(ctx, "pulumi-deployer", &ec2.KeyPairArgs{
			PublicKey: pulumi.String("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 <mailto:email@example.com|email@example.com>"),
		})

srv, err := ec2.NewInstance(ctx, "web-server-www", &ec2.InstanceArgs{
			Tags:                pulumi.StringMap{"Name": pulumi.String("web-server-www")},
			InstanceType:        pulumi.String("t2.micro"), // t2.micro is available in the AWS free tier.
            KeyName: keypair.KeyName,
			VpcSecurityGroupIds: pulumi.StringArray{group.ID()},
			Ami:                 pulumi.String("ami-0d058fe428540cd89"),
			UserData: pulumi.String(`#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`),
		})
k

kind-carpenter-52856

07/16/2021, 1:35 PM
i got this error here is my code keypair, err = ec2.NewKeyPair(ctx, “pulumi-deployer”, &ec2.KeyPairArgs{ PublicKey: pulumi.String(“ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com”), }) if err != nil { return err } // Create a simple web server using the startup script for the instance. srv, err := ec2.NewInstance(ctx, “web-server-www”, &ec2.InstanceArgs{ Tags: pulumi.StringMap{“Name”: pulumi.String(“web-server-www”)}, InstanceType: pulumi.String(“t2.micro”), // t2.micro is available in the AWS free tier. VpcSecurityGroupIds: pulumi.StringArray{group.ID()}, Ami: pulumi.String(“ami-0d058fe428540cd89”), UserData: pulumi.String(
Copy code
#!/bin/bash
echo “Hello, World!” > index.html
nohup python -m SimpleHTTPServer 80 &
), })
but it’s work if i change “keypair” to “_”
b

brave-planet-10645

07/16/2021, 2:14 PM
That's because you're not referencing the keypair when you're creating the instance
look at my code sample at where you've got the
KeyPair
line in the EC2 instance creation
k

kind-carpenter-52856

07/16/2021, 3:08 PM
hi @brave-planet-10645 i just copy paste your code, still not work. I there something i missed? https://pastebin.com/XeM2ikWR
OMG, it’s work, it should “:=” instead of “=”
@brave-planet-10645 hi Piers, I think it’s work, thank you for your time and answer, always success for pulumi
b

brave-planet-10645

07/16/2021, 3:16 PM
😄
3 Views