1. Answers
  2. SSH into an Ubuntu Server on AWS EC2

How do I SSH into an Ubuntu server on AWS EC2?

In this guide, we will set up an Ubuntu server on AWS EC2 and configure SSH access to it using Pulumi. We will create an EC2 instance, generate an SSH key pair, and associate the public key with the instance to enable secure SSH access.

Key Points

  • Create an EC2 instance running Ubuntu.
  • Generate and use an SSH key pair for secure access.
  • Configure security group to allow SSH traffic.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Generate an SSH key pair
const sshKey = new aws.ec2.KeyPair("ssh-key", {
    publicKey: "<YOUR_PUBLIC_SSH_KEY>",
});

// Create a security group to allow SSH access
const securityGroup = new aws.ec2.SecurityGroup("ssh-security-group", {
    ingress: [
        {
            protocol: "tcp",
            fromPort: 22,
            toPort: 22,
            cidrBlocks: ["0.0.0.0/0"], // Allow SSH from anywhere
        },
    ],
});

// Launch an Ubuntu EC2 instance
const server = new aws.ec2.Instance("ubuntu-server", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Ubuntu Server 20.04 LTS AMI ID
    keyName: sshKey.keyName,
    securityGroups: [securityGroup.name],
    tags: {
        Name: "ubuntu-server",
    },
});

// Export the public IP and DNS of the instance
export const publicIp = server.publicIp;
export const publicDns = server.publicDns;

Summary

In this guide, we created an Ubuntu server on AWS EC2 and set up SSH access using Pulumi. We generated an SSH key pair, configured a security group to allow SSH traffic, and launched an EC2 instance with the specified SSH key. Finally, we exported the public IP and DNS of the instance for easy access.

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