LoginSignup
0
0

More than 1 year has passed since last update.

Pulumi(typescript) でセキュリティグループとEC2を作成する

Posted at

VPC一覧表示、Subnet一覧表示、Secuirty Group 作成、EC2作成

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";


function consoleIds(ids:string[]){
	ids.forEach(id => {
			console.log(` ID: ${id}`);	
		}
	);
}

async function  consoleVpc(vpcid:string){
	const vpc= await aws.ec2.getVpc({id:vpcid});
	console.log(` ID: ${vpc.id}`);
	console.log(` CIDR: ${vpc.cidrBlock}`);
	console.log(` Name: ${vpc.tags.Name}`);
}

async function consoleSubnet(subnetId:string){
	const subnet=await aws.ec2.getSubnet({id:subnetId});

	console.log("Existing Subnet:");
	console.log(` subnetID: ${subnet.id}`);
	console.log(` CIDR: ${subnet.cidrBlock}`);
	console.log(` Name: ${subnet.tags.Name}`);
	console.log(` VPC: ${subnet.vpcId}`);	

	// Get the VPC
	console.log("VPC INFO:");
	const vpc=consoleVpc(subnet.vpcId);
}

function consoleSubnets(ids:string[]){
	ids.forEach(subnetId => {
			//console.log(` ID: ${id}`);
			consoleSubnet(subnetId);
		}
	);
}

function consoleVpcs(ids:string[]){
	ids.forEach(vpcid => {
			//console.log(` ID: ${id}`);
			consoleVpc(vpcid);
		}
	);
}

const getVpcs = async function(){
	const vpcs=await aws.ec2.getVpcs({});
	console.log("Existing VPCs:");
	consoleVpcs(vpcs.ids);
	
	console.log(vpcs.tags);
}

const getSubnets = async function(){
	const subnets=await aws.ec2.getSubnets({});
	console.log("Existing Subnets:");
	consoleSubnets(subnets.ids);
	console.log(subnets.tags);
}

//
// Create a security group
//
const testSecGroup = new aws.ec2.SecurityGroup("testSecGroup", {
    vpcId: "<vpc-id>",
    ingress: [
        {
            protocol: "tcp",
            fromPort: 22,
            toPort: 22,
            cidrBlocks: ["0.0.0.0/0"],
        },
        {
            protocol: "tcp",
            fromPort: 3000,
            toPort: 3000,
            cidrBlocks: ["0.0.0.0/0"],
        },
    ],
	egress: [
		{
			protocol: "-1",
			fromPort: 0,
			toPort: 0,
			cidrBlocks: ["0.0.0.0/0"],	
		}
	],
    tags: {
        Name: "makuhari,test",
    },
});

//
// Create keypair
//
const myKeyPair = new aws.ec2.KeyPair("testkeypair", {
    publicKey: "<public-key>",
});

//
// Create a new EC2 instance
//
const myInstance = new aws.ec2.Instance("test-instance", {
    instanceType: "t2.micro",:
    ami: "ami-078296f82eb463377",
    vpcSecurityGroupIds: [testSecGroup.id],
    subnetId: "<subnetId>",
    keyName: myKeyPair.id,
	associatePublicIpAddress: true,
});

//getVpcs();
//getSubnets();
//myKeyPair();




0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0