Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//
// Note that the VPC has been tagged appropriately.
const defaultVpc = new aws.ec2.Vpc("default", {
cidrBlock: "10.0.0.0/16", // Just one CIDR block
enableDnsHostnames: true, // Definitely want DNS hostnames.
// The tag collection for this VPC.
tags: {
// Ensure that we tag this VPC with a Name.
Name: "test",
},
});
// Use some data sources.
const defaultSubnetIds = defaultVpc.id.apply(id => aws.ec2.getSubnetIds({
vpcId: id,
}, { async: true }));
const defaultAvailabilityZones = pulumi.output(aws.getAvailabilityZones({ async: true }));
const defaultAvailabilityZone: pulumi.Output[] = [];
for (let i = 0; i < defaultAvailabilityZones.apply(defaultAvailabilityZones => defaultAvailabilityZones.ids.length); i++) {
defaultAvailabilityZone.push(defaultAvailabilityZones.apply(defaultAvailabilityZones => aws.getAvailabilityZone({
zoneId: defaultAvailabilityZones.zoneIds[i],
}, { async: true })));
}
// The VPC details
const vpc = [{
// The ID
id: defaultVpc.id,
}];
// The region, again
const region = awsRegion; // why not
// Create a security group.
//
// This group should allow SSH and HTTP access.
}, inputs.baseTags);
const internetGateway = new aws.ec2.InternetGateway(`${baseName}-igw`, {
vpcId: vpc.id,
tags: internetGatewayTags,
}, vpcParent);
// Subnet Distributor
let distributor: SubnetDistributor;
if (typeof inputs.azCount === "number") {
distributor = SubnetDistributor.fixedCount(inputs.baseCidr, inputs.azCount);
} else {
distributor = await SubnetDistributor.perAz(inputs.baseCidr);
}
// Find AZ names
let azNames = (await aws.getAvailabilityZones({
state: "available",
})).names;
// Public Subnets
const publicSubnets = (await distributor.publicSubnets()).map((cidr, index) => {
const subnetTags = Object.assign({
Name: `${inputs.description} Public ${index + 1}`,
}, inputs.baseTags);
return new aws.ec2.Subnet(`${baseName}-public-${index + 1}`, {
vpcId: vpc.id,
cidrBlock: cidr,
mapPublicIpOnLaunch: false,
availabilityZone: azNames[index],
tags: subnetTags,
}, vpcParent);
});
Name: `${inputs.description} VPC IG`,
}, inputs.baseTags);
const internetGateway = new aws.ec2.InternetGateway(`${baseName}-igw`, {
vpcId: vpc.id,
tags: internetGatewayTags,
}, vpcParent);
// Subnet Distributor
let distributor;
if (typeof inputs.azCount === "number") {
distributor = subnetDistributor_1.SubnetDistributor.fixedCount(inputs.baseCidr, inputs.azCount);
}
else {
distributor = yield subnetDistributor_1.SubnetDistributor.perAz(inputs.baseCidr);
}
// Find AZ names
let azNames = (yield aws.getAvailabilityZones({
state: "available",
})).names;
// Public Subnets
const publicSubnets = (yield distributor.publicSubnets()).map((cidr, index) => {
const subnetTags = Object.assign({
Name: `${inputs.description} Public ${index + 1}`,
}, inputs.baseTags);
return new aws.ec2.Subnet(`${baseName}-public-${index + 1}`, {
vpcId: vpc.id,
cidrBlock: cidr,
mapPublicIpOnLaunch: false,
availabilityZone: azNames[index],
tags: subnetTags,
}, vpcParent);
});
instance.publicSubnetIds = publicSubnets.map(subnet => subnet.id);
export async function getAwsAz(index: number) {
if (!azs) {
azs = aws.getAvailabilityZones();
}
return (await azs).names[index];
}
const sg = new aws.ec2.SecurityGroup("web-secgrp", {
ingress: [
{ protocol: "icmp", fromPort: 8, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
],
});
const alb = new awsx.lb.ApplicationLoadBalancer("web-traffic", {
external: true,
securityGroups: [ sg.id ],
});
const listener = alb.createListener("web-listener", { port: 80 });
export const ips: any[] = [];
export const hostnames: any[] = [];
for (const az of aws.getAvailabilityZones().names) {
const server = new aws.ec2.Instance(`web-server-${az}`, {
instanceType: "t2.micro",
securityGroups: alb.securityGroups.map(sg => sg.securityGroup.name),
ami: ami,
availabilityZone: az,
userData: "#!/bin/bash\n"+
`echo 'Hello, World -- from ${az}!' > index.html\n` +
"nohup python -m SimpleHTTPServer 80 &",
tags: { "Name": "web-server" },
});
ips.push(server.publicIp);
hostnames.push(server.publicDns);
alb.attachTarget(`web-target-${az}`, server);
}
export function getAvailabilityZones(): Promise {
if (!zones) {
zones = aws.getAvailabilityZones().then(r => r.names);
}
return zones;
}
const ami = aws.getAmi({
filters: [{ name: "name", values: ["amzn-ami-hvm-*-x86_64-ebs"] }],
owners: [ "137112412989" ],
mostRecent: true,
}).then(ami => ami.id);
const sg = new aws.ec2.SecurityGroup("web-secgrp", {
ingress: [
{ protocol: "icmp", fromPort: 8, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
});
export const ips: any[] = [];
export const hostnames: any[] = [];
for (const az of aws.getAvailabilityZones().names) {
const server = new aws.ec2.Instance(`web-server-${az}`, {
instanceType: "t2.micro",
securityGroups: [ sg.name ],
ami: ami,
availabilityZone: az,
userData: "#!/bin/bash\n"+
`echo 'Hello, World -- from ${az}!' > index.html\n` +
"nohup python -m SimpleHTTPServer 80 &",
tags: { "Name": "web-server" },
});
ips.push(server.publicIp);
hostnames.push(server.publicDns);
}
async function main() {
const config = new Config();
const azCount = config.getNumber("azCount") || 2;
const baseTags = {
ManagedBy: "Pulumi",
PulumiStack: getStack(),
};
const availabilityZones = aws.getAvailabilityZones({
state: "available",
}, { async: true });
const appVpc = new Vpc("app-vpc", {
description: `${baseTags.ManagedBy} App VPC`,
baseTags: baseTags,
baseCidr: "172.28.0.0/16",
availabilityZoneNames: availabilityZones.names.slice(0, azCount),
enableFlowLogs: true,
endpoints: {
s3: true,
dynamodb: true,
},
});
export async function getAwsAz(index: number) {
if (!azs) {
azs = aws.getAvailabilityZones();
}
return (await azs).names[index];
}