Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Terraform to Pulumi migration
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as fs from "fs";
const config = new pulumi.Config();
const var_availability_zones = config.require("availabilityZones");
const var_public_key = config.get("publicKey") || "";
const aws_security_group_default = new aws.ec2.SecurityGroup("default", {
namePrefix: "example_sg",
});
const aws_ami_ubuntu = pulumi.output(aws.getAmi({
filters:
{
name: "name",
values: ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"],
},
{
name: "virtualization-type",
values: ["hvm"],
},
],
mostRecent: true,
owners: ["099720109477"],
}));
const aws_elb_elb = new aws.elasticloadbalancing.LoadBalancer("elb", {
availabilityZones: var_availability_zones.split(","),
healthCheck: {
healthyThreshold: 2,
interval: 30,
target: "HTTP:80/",
timeout: 3,
unhealthyThreshold: 2,
},
listeners: [{
instancePort: 80,
instanceProtocol: "http",
lbPort: 80,
lbProtocol: "http",
}],
namePrefix: "webelb",
});
const aws_key_pair_default: aws.ec2.KeyPair[] = [];
for (let i = 0; i < ((var_public_key === "") ? 0 : 1); i++) {
aws_key_pair_default.push(new aws.ec2.KeyPair(`default-${i}`, {
keyNamePrefix: "default",
publicKey: var_public_key,
}));
}
const aws_instance_web_server_key: aws.ec2.Instance[] = [];
for (let i = 0; i < ((var_public_key === "") ? 0 : var_availability_zones.split(",").length); i++) {
aws_instance_web_server_key.push(new aws.ec2.Instance(`web-server-key-${i}`, {
ami: aws_ami_ubuntu.apply(__arg0 => __arg0.id),
availabilityZone: var_availability_zones.split(",")[i],
instanceType: "t2.micro",
keyName: aws_key_pair_default[0].keyName,
securityGroups: [aws_security_group_default.name],
userData: fs.readFileSync("userdata.sh", "utf-8"),
}));
}
const aws_elb_attachment_web_server_key: aws.elasticloadbalancing.Attachment[] = [];
for (let i = 0; i < ((var_public_key === "") ? 0 : var_availability_zones.split(",").length); i++) {
aws_elb_attachment_web_server_key.push(new aws.elasticloadbalancing.Attachment(`web-server-key-${i}`, {
elb: aws_elb_elb.id,
instance: pulumi.all(aws_instance_web_server_key.map(v => v.id)).apply(__arg0 => __arg0.map(v => v)[i]),
}));
}
const aws_instance_web_server_nokey: aws.ec2.Instance[] = [];
for (let i = 0; i < ((var_public_key === "") ? var_availability_zones.split(",").length : 0); i++) {
aws_instance_web_server_nokey.push(new aws.ec2.Instance(`web-server-nokey-${i}`, {
ami: aws_ami_ubuntu.apply(__arg0 => __arg0.id),
availabilityZone: var_availability_zones.split(",")[i],
instanceType: "t2.micro",
securityGroups: [aws_security_group_default.name],
userData: fs.readFileSync("userdata.sh", "utf-8"),
}));
}
const aws_elb_attachment_web_server_nokey: aws.elasticloadbalancing.Attachment[] = [];
for (let i = 0; i < ((var_public_key === "") ? var_availability_zones.split(",").length : 0); i++) {
aws_elb_attachment_web_server_nokey.push(new aws.elasticloadbalancing.Attachment(`web-server-nokey-${i}`, {
elb: aws_elb_elb.id,
instance: pulumi.all(aws_instance_web_server_nokey.map(v => v.id)).apply(__arg0 => __arg0.map(v => v)[i]),
}));
}
const aws_security_group_rule_allow_all_outbound = new aws.ec2.SecurityGroupRule("allow_all_outbound", {
cidrBlocks: ["0.0.0.0/0"],
fromPort: 0,
protocol: "-1",
securityGroupId: aws_security_group_default.id,
toPort: 0,
type: "egress",
});
const aws_security_group_rule_allow_http = new aws.ec2.SecurityGroupRule("allow_http", {
cidrBlocks: ["0.0.0.0/0"],
fromPort: 80,
protocol: "tcp",
securityGroupId: aws_security_group_default.id,
toPort: 80,
type: "ingress",
});
const aws_security_group_rule_allow_ssh: aws.ec2.SecurityGroupRule[] = [];
for (let i = 0; i < ((var_public_key === "") ? 0 : 1); i++) {
aws_security_group_rule_allow_ssh.push(new aws.ec2.SecurityGroupRule(`allow_ssh-${i}`, {
cidrBlocks: ["0.0.0.0/0"],
fromPort: 22,
protocol: "tcp",
securityGroupId: aws_security_group_default.id,
toPort: 22,
type: "ingress",
}));
}
export const publicIps = aws_instance_web_server_key.map(v => v.publicIp);
export const url = aws_elb_elb.dnsName.apply(__arg0 => `http://${__arg0}`);
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as fs from "fs";
const config = new pulumi.Config();
const availabilityZones = config.require("availabilityZones").split(",");
const publicKey = config.get("publicKey");
const ubuntuAmi = aws.getAmi({
filters: [
{ name: "name", values: ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] },
{ name: "virtualization-type", values: ["hvm"] },
],
mostRecent: true,
owners: ["099720109477"], // Canonical
});
const anyCidr = ["0.0.0.0/0"];
const ingressRules = [{ cidrBlocks: anyCidr, fromPort: 80, toPort: 80, protocol: "tcp" }];
if (publicKey) {
ingressRules.push({ cidrBlocks: anyCidr, fromPort: 22, toPort: 22, protocol: "tcp" });
}
const webServerSg = new aws.ec2.SecurityGroup("default", {
egress: [{ cidrBlocks: anyCidr, fromPort: 0, toPort: 0, protocol: "-1" }],
ingress: ingressRules,
});
let keyName: pulumi.Output<string> | undefined;
if (publicKey) {
keyName = new aws.ec2.KeyPair("default", { publicKey }).id;
}
const webServerInstances: aws.ec2.Instance[] = availabilityZones.map(az =>
new aws.ec2.Instance(`web-server-key-${az}`, {
ami: ubuntuAmi.then(ami => ami.id),
associatePublicIpAddress: publicKey !== "",
availabilityZone: az,
instanceType: "t2.micro",
securityGroups: [ webServerSg.name ],
userData: fs.readFileSync("userdata.sh", "utf-8"),
keyName: keyName,
}),
);
const webServerElb = new aws.elasticloadbalancing.LoadBalancer("elb", {
availabilityZones: availabilityZones,
healthCheck: {
healthyThreshold: 2,
interval: 30,
target: "HTTP:80/",
timeout: 3,
unhealthyThreshold: 2,
},
listeners: [{
instancePort: 80,
instanceProtocol: "http",
lbPort: 80,
lbProtocol: "http",
}],
instances: webServerInstances.map(instance => instance.id),
});
export const publicIps = webServerInstances.map(v => v.publicIp);
export const url = webServerElb.dnsName.apply(hostname => `http://${hostname}`);
variable "availability_zones" {}
variable "public_key" {
default = ""
}
resource "aws_elb" "elb" {
name_prefix = "webelb"
availability_zones = ["${split(",", var.availability_zones)}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "HTTP:80/"
interval = 30
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_security_group" "default" {
name_prefix = "example_sg"
}
resource "aws_security_group_rule" "allow_http" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.default.id}"
}
resource "aws_security_group_rule" "allow_all_outbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.default.id}"
}
resource "aws_security_group_rule" "allow_ssh" {
count = "${var.public_key == "" ? 0 : 1}"
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.default.id}"
}
resource "aws_key_pair" "default" {
count = "${var.public_key == "" ? 0 : 1}"
key_name_prefix = "default"
public_key = "${var.public_key}"
}
resource "aws_instance" "web-server-key" {
count = "${var.public_key == "" ? 0 : length(split(",", var.availability_zones))}"
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
availability_zone = "${element(split(",", var.availability_zones), count.index)}"
user_data = "${file("userdata.sh")}"
security_groups = ["${aws_security_group.default.name}"]
key_name = "${aws_key_pair.default.0.key_name}"
}
resource "aws_elb_attachment" "web-server-key" {
count = "${var.public_key == "" ? 0 : length(split(",", var.availability_zones))}"
elb = "${aws_elb.elb.id}"
instance = "${element(aws_instance.web-server-key.*.id, count.index)}"
}
resource "aws_instance" "web-server-nokey" {
count = "${var.public_key == "" ? length(split(",", var.availability_zones)) : 0}"
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
availability_zone = "${element(split(",", var.availability_zones), count.index)}"
user_data = "${file("userdata.sh")}"
security_groups = ["${aws_security_group.default.name}"]
}
resource "aws_elb_attachment" "web-server-nokey" {
count = "${var.public_key == "" ? length(split(",", var.availability_zones)) : 0}"
elb = "${aws_elb.elb.id}"
instance = "${element(aws_instance.web-server-nokey.*.id, count.index)}"
}
output "public_ips" {
value = "${aws_instance.web-server-key.*.public_ip}"
}
output "url" {
value = "http://${aws_elb.elb.dns_name}"
}
#!/bin/bash -v
apt-get update -y
apt-get install -y nginx > /tmp/nginx.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.