How do I set up an AWS EC2 instance with TypeScript?
In this guide, we will set up an AWS EC2 instance using Pulumi and TypeScript. Pulumi is an infrastructure as code tool that allows you to define and manage cloud resources using programming languages. We will use the @pulumi/aws
package to provision an EC2 instance.
Key Points:
- We will create a new EC2 instance using a specified Amazon Machine Image (AMI) and instance type.
- We will define necessary security groups to allow SSH access.
- The code provided will be written in TypeScript.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a security group that allows SSH access
const securityGroup = new aws.ec2.SecurityGroup("web-secgrp", {
description: "Enable SSH access",
ingress: [
{
protocol: "tcp",
fromPort: 22,
toPort: 22,
cidrBlocks: ["0.0.0.0/0"], // Allow SSH from anywhere
},
],
});
// Define the EC2 instance
const instance = new aws.ec2.Instance("web-server", {
instanceType: "t2.micro", // Instance type
ami: "ami-0c55b159cbfafe1f0", // AMI ID for Amazon Linux 2 in us-west-2 region
securityGroups: [securityGroup.name], // Attach security group
tags: {
Name: "web-server",
},
});
// Export the instance's public IP address
export const publicIp = instance.publicIp;
export const publicDns = instance.publicDns;
Summary
In this guide, we created an AWS EC2 instance using Pulumi and TypeScript. We defined a security group to allow SSH access and provisioned an EC2 instance with a specified AMI and instance type. The public IP and DNS of the instance were exported for easy access. This setup can be further customized based on your requirements.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.