1. Answers
  2. Can Pulumi SSH Into EC2 To Add A Public Key?

Can Pulumi SSH Into EC2 to Add a Public Key?

Introduction

In this solution, we will use Pulumi to provision an EC2 instance on AWS and then SSH into the instance to add a public key. Pulumi is an Infrastructure as Code (IaC) tool that allows you to define and manage cloud resources using familiar programming languages. The key services involved in this solution are AWS EC2 for creating the virtual machine and AWS IAM for managing access.

Step-by-Step Explanation

Step 1: Set Up Pulumi Project

First, we need to set up a new Pulumi project. This involves installing the Pulumi CLI, creating a new project, and configuring the AWS provider.

Step 2: Provision EC2 Instance

Next, we will define the EC2 instance resource in our Pulumi program. This includes specifying the instance type, AMI, key pair, and security group.

Step 3: SSH into EC2 Instance

After the EC2 instance is created, we will use the @pulumi/command package to SSH into the instance and add the public key to the ~/.ssh/authorized_keys file.

Key Points

  • Pulumi allows you to manage cloud resources using familiar programming languages.
  • AWS EC2 is used to create the virtual machine.
  • The @pulumi/command package is used to execute commands on the EC2 instance.
  • Proper IAM roles and security groups are essential for secure access.

Conclusion

In this solution, we demonstrated how to use Pulumi to provision an EC2 instance on AWS and SSH into the instance to add a public key. By leveraging Pulumi and AWS services, we can automate the process of managing cloud infrastructure and ensure secure access to our resources.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as command from "@pulumi/command";

// Create a new IAM role
const role = new aws.iam.Role("myRole", {
    assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
        Service: "ec2.amazonaws.com",
    }),
});

// Create a new EC2 key pair
const keyPair = new aws.ec2.KeyPair("myKeyPair", {
    publicKey: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC..."
});

// Create a new security group
const securityGroup = new aws.ec2.SecurityGroup("mySecurityGroup", {
    ingress: [
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create a new EC2 instance
const instance = new aws.ec2.Instance("myInstance", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    keyName: keyPair.keyName,
    securityGroups: [securityGroup.name],
    iamInstanceProfile: role.name,
});

// SSH into the instance and add the public key to authorized_keys
const sshCommand = new command.remote.Command("sshCommand", {
    connection: {
        host: instance.publicIp,
        user: "ec2-user",
        privateKey: "<private-key-content>",
    },
    create: "echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...' >> ~/.ssh/authorized_keys",
});

export const instancePublicIp = instance.publicIp;

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up