Listado de la etiqueta: Create

AWS provides a supuesto private cloud (VPC) service for creating a logically isolated supuesto network in the cloud. Here, we can launch EC2 and RDS instances and create security groups and other resources. Like many other tasks, we can also create a VPC using Terraform.

What We Will Cover

This guide will show how to create an AWS VPC (Posible Private Cloud) using Terraform.

What You Will Need

  1. AWS account
  2. Access to the internet
  3. Basics of Terraform

Creating AWS VPC Using Terraform

Now that we have installed Terraform on our locorregional machine, we can continue our task of working with VPC. Here, we have outlined the VPC setup for our case:

We have one private and one public subnet with their corresponding route table. The public subnet also has a NAT gateway attached to it. The Terraform configuration for different components is stored in different files as:

  1. variables.tf: Definition of variables used in the files
  2. vpc.tf: For VPC resource
  3. gateway.tf: For Gateway resources
  4. subnets.tf: For defining public and private subnets
  5. route-table.tf: For public and private route table resources
  6. main.tf

As mentioned earlier, Terraform uses several configuration files for provisioning resources, and each of these files must reside in their respective working folder/directory. Let us create a directory for this purpose:

Step 1. Create a folder that will hold your configuration files, and then navigate to this folder:

1 $ mkdir linuxhint-terraform && cd linuxhint-terraform

Step 2. Let us create our first configuration file, “variables.tf”, that will contain information about our AWS region and the type of instance we want to use:

Now, put the following text inside it, and save the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
variable «aws_region» {
description = «The AWS region to create the VPC in.»
default   = «us-east-1»
}

variable «vpc-cidr» {
cidr_block = «172.168.0.0/16»
}

variable «pubsubcidr» {
cidr_block = «172.168.0.0/24»
}

variable «prisubcidr» {
cidr_block = «172.168.1.0/24»
}

Step 3. Create vpc.tf:

Now, put the following text inside it, and save the file:

1
2
3
4
5
resource «aws_vpc» «my-vpc» {

cidr_block = var.vpc-cidr

}

Step 4. Create gateway.tf file and define internet gateway and NAT gateway here:

Now, put the following text inside it, and save the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Create Internet Gateway resource and attach it to the VPC

resource «aws_internet_gateway» «IGW» {

vpc_id =  aws_vpc.my-vpc.id

}

# Create EIP for the IGW

resource «aws_eip» «myEIP» {
vpc   = true
}

# Create NAT Gateway resource and attach it to the VPC
resource «aws_nat_gateway» «NAT-GW» {
allocation_id = aws_eip.myEIP.id
subnet_id = aws_subnet.mypublicsubnet.id
}

Step 5. Create subnets.tf for the private and public subnets inside the VPC:

Now, put the following text inside it, and save the file:

1
2
3
4
5
6
7
8
9
resource «aws_subnet» «myprivatesubnet» {
vpc_id =  aws_vpc.my-vpc.id
cidr_block = var.prisubcidr
}

resource «aws_subnet» «mypublicsubnet» {
vpc_id =  aws_vpc.my-vpc.id
cidr_block = var.pubsubcidr
}

Step 6. Create route-table.tf for private and public subnets:

Now, put the following text inside it, and save the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Creating RT for Private Subnet

resource «aws_route_table» «privRT» {
vpc_id = aws_vpc.my-vpc.id
route {
cidr_block = «0.0.0.0/0»
nat_gateway_id = aws_nat_gateway.NAT-GW.id
}
}

# Creating RT for Public Subnet
resource «aws_route_table» «publRT» {
vpc_id =  aws_vpc.my-vpc.id
route {
cidr_block = «0.0.0.0/0»
gateway_id = aws_internet_gateway.IGW.id
}
}
#Associating the Public RT with the Public Subnets
resource «aws_route_table_association» «PubRTAss» {
subnet_id = aws_subnet.mypublicsubnet.id
route_table_id = aws_route_table.publRT.id
}
#Associating the Private RT with the Private Subnets
resource «aws_route_table_association» «PriRTAss» {
subnet_id = aws_subnet.myprivatesubnet.id
route_table_id = aws_route_table.privRT.id
}

Step 7. Make a “main.tf” file that will contain the definition for our infrastructure:

Now, put the following configuration inside it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
terraform {
required_providers {
aws = {
source  = «hashicorp/aws»

version = «~> 3.27»
}
}

required_version = «>= 0.14.9»

}

provider «aws» {
region                = var.aws_region
shared_credentials_file = «/home/User_Name/.aws/credentials»
profile               = «profile1»
}

Modify the “User_Name” in the above code with the username in your case. Let us see a bit about the parameters used in the previous files:

  • shared_credentials_file: It is the path of the file containing the credentials of the AWS users.
  • profile: It specifies the user’s profile to be used for working with AWS.
  • aws_vpc: Resource for building a VPC.
  • cidr_block: Provides an IPv4 CIDR block for the VPC.
  • aws_internet_gateway: Resource for creating an internet gateway for the VPC.
  • aws_eip: Resource for producing an Elastic IP (EIP).
  • aws_nat_gateway: Resource for creating a NAT gateway for the VPC.
  • Allocation_id: Attribute for allocation id of the above-generated EIP.
  • subnet_id: Attribute for subnet id of the subnet where NAT gateway is deployed.
  • aws_subnet: Resource for creating a VPC subnet.
  • aws_route_table: Resource for creating a VPC route table.
  • route: Argument that contains a list of route objects.
  • nat_gateway_id: Argument denoting the ID of the VPC NAT gateway.
  • gateway_id: Optional argument for VPC internet gateway.
  • aws_route_table_association: Resource for creating an association between route table (public or private) and 1) internet gateway and 2) supuesto private gateway.
  • route_table_id: The route table ID with which we are associating the subnet.

Initializing the Terraform Directory

To download and install the provider we defined in our configuration and other files, we need to initialize the directory containing this file:

Building the Infrastructure

To apply the changes we planned above, run the following command:

Enter “yes” on the terminal when prompted.

Verifying the Procedure

Now, let us check if the desired VPC is created or not. Head to the VPC console, and check for the available VPCs:

We can see that our VPC is created successfully.

After you have done performing this task, delete the resources to avoid unnecessary charges:

Enter “yes” to apply the action.

Conclusion

In this guide, we have learned about creating a VPC on AWS using Terraform. The next thing that you can do is try to provision an RDS or EC2 instance using Terraform.

Source link


We will be discussing different methods to create a text file through a C# program in this article. The C# programming language allows us to create, edit and update text files using special classes like the Stream Writer, Stream Reader, and File Stream present in the System.IO namespace. These classes are specially made to perform all types of operations on a file that we could use for creating or editing during a C# program. All the input and output operations for a text file in the C# programming language are stored in the System.IO namespace.

We will now discuss several C# programming language methods and implement them in the Ubuntu 20.04 environment.

Method 01: Using the Stream Writer and Stream Reader Classes in Ubuntu 20.04 to Create and Read Text Files

In this method, we will be using the Stream Writer and Stream Reader class to create a text file while adding some text to them. The Stream writer class is a part of the System.IO namespace and is used for input functions in a text file, while the Stream Reader class, which is also present in the same namespace, is used for reading the text file in the C# programming language. Both these classes will help us in achieving our goal in this method.

Text Description automatically generated

The above C# program starts with the initialization of a string variable named “file Name.” We gave this variable the text file name that we will be creating and reading on our device. Then we will be initiating the creation of the object of the Stream Writer class, which will have the File.CreateText Function, and this function will have the string variable “file Name” which has the name of the text file. Within the parameters of the stream writer class’s object, we have used the object to call the Write Line method to add some lines to the new text file created.

After this, we will be initiating an object for the Stream Reader class, which will have the Open Text function with the File name string variable; this function will give us access to the text file. Now we will initiate a string variable and put it in a while loop which will have the stream reader object assigning all the text of the file to the string variable, and it will all be printed using the Console Write Line function.

Text Description automatically generated

After compiling and executing the previous code, we will get the above output. In this snippet, we can see the output of the while loop in the Stream Reader function, which reads the text file and prints its content. We will look at the text file that we created and verify that the text was added successfully.

Graphical user interface, website Description automatically generated

Method 02: Using File Stream and Stream Reader Class to Create and Read a Text File in Ubuntu 20.04

In this technique, we will create a text file and add some text to it using the File Stream and Stream Reader classes. For text file input methods, the File Stream class from the System.IO namespace will be utilized, while the Stream Reader class from the same namespace will be used to read the text file in this method.

Text Description automatically generated

The C# code above starts with initializing a string variable named “f.” This variable is named after the text file we will be creating. Then we will start the Stream Writer class object, which will have a “File.CreateText Function” with a string variable containing the file name. We have used the object to use the Write Line function to add some lines to the new text file generated using the stream writer class’s object’s arguments.

After that, we will create an object for the Stream Reader class using the “Open Text” method and the File name string variable, which will allow us to read the text file. Now we will create a string variable and place it in a while loop, with the stream reader object assigning all of the file’s text to the string variable and printing it all using the “Console Write Line” method.

Text Description automatically generated

We will obtain the above output after compiling and running the prior code. The result of the while loop of the Stream Reader function, which reads the text file and displays its content, can be seen in this snippet. We will now examine the text file we prepared.

Graphical user interface, application, website Description automatically generated

The text file has the line that we added to it using the Stream Writer Class object, as we can see in the following example.

Method 03: Using File Security and File Stream Class to Create a Text File With Secure Access in Ubuntu 20.04

In this method, we will be using the File Security class to restrict the access of a text file, and only specified users will be allowed to open and edit the file. The File Security class and the File Stream Class are both found in the System.IO namespace and will be utilized to create a text file on our system in this function.

A screenshot of a computer Description automatically generated with medium confidence

In this C# program, we will initialize a string variable with the name of the text file stored in it. Then we will create an object of the File Security class and use it for the “Add Access Rule function”, which is the further inheritance for the File System Access Rule; this will allocate the new text file its access rights. After this, we will create an object of the File Stream class to create a text file, and in the parameters, we will pass the object of the File Security class along with file size and other details. Then with the help of the Byte array, we will add some text to the file as well.

Text Description automatically generated

In this output, we can see that the text file was created successfully as the file contents were displayed in the terminal.

Method 04: Using File.Write All Text to Create and Add Text to a Text file in Ubuntu 20.04

In this method, we will be using the functions of the System.Text namespace to create a text file. This namespace in the C# Programming language has all the functionalities related to textual operations. In this method, we will use the Write All Text function of the System.Text namespace to create and add text to a new text file.

Text Description automatically generated

In this C# program, we will initialize two string variables, one with the file name and the other with the text that we want to add to the file. Then we will call the Write All Text function with both these string variables as the parameter.

After executing the above program, we will get the following output:

A screenshot of a computer Description automatically generated with low confidence

The terminal and the text file are evidence that the Write All Text function successfully created a text file with some text in it.

A picture containing timeline Description automatically generated

Conclusion

In this article, we discussed several C# programming language methods to create a text file on your device. The use of special classes built for editing and creating text files present in the System.IO namespace, which are some special operations(input and output) libraries related to text files in the C# programming languages, were also discussed in this article. We focused on the function of stream classes to create a file which is the Create() method and was common in several classes. We implemented this method and other text file creating methods in the Ubuntu 20.04 environment to better understand how these different classes and functions work together in the C# programming language.



Source link


Lists are similar to dynamically allocated arrays, declared in other languages. Lists do not always have to be homogeneous, making Python’s most powerful feature. Integers, Strings, and Objects can all be found in a single list. Lists are changeable, which means they can be changed after they are created.

In Python, lists are ordered and counted. Every member inside the list has its separate position in the list, allowing duplication of the list’s elements while maintaining the credibility of each member. Lists help store and iterate through an iteration of data.

How to Create it?

This artifact will teach you to create and initialize a Python list of lists. A list of lists is a nested list with one or more lists. There are numerous methods for creating a list of lists. A different method will be used to construct a list of lists. Then, we will explore how to access list items after we’ve created a list of lists. Let’s look at a few examples.

Example 1: Using the append() Function to Create a List of Lists in Python in Ubuntu 20.04

The List append() function in Python is used to append and add items to the end of a List. In this illustration, we are just adding a list to a list as an item using the append() method.

Let’s see the implemented python code in the following image. In our first step, we created two list arrays represented with “list_x” and “list_y.” The lists here are initialized with different integer values.

After that, we have created a new list as “list_z,” which is initially empty. We are going to add the above lists to that list. Then, we have called the append function for the “list_z,” to which we want to append the above-initialized lists. The append function takes the list_x and the list_y as an argument individually. This append method will add the whole list in the list_z along with the elements of the list. The print function is called for printing the lists of newly generated “list_z.”

list_x = [2, 4, 6, 8]

list_y = [3, 5, 7, 9]

list_z = []

list_z.append(list_x)

list_z.append(list_y)

print(list_z)

The lists are appended to the one list as shown on the output terminal screen.

Example 2: Using the List Initializer to Create a List of Lists in Python in Ubuntu 20.04

An alternative way to make a list in Python, use the list initializer syntax. We can use this approach to make a list of lists by providing lists to the list initializer as elements.

At the beginning of the python code below, we have defined two lists separately. The lists are represented as “list1” and “list2”. These lists are stored with some integral values, as shown. Then, we have the list initializer method for which we have declared a new list as “list3”. We have initialized the “list3” with the “list1” and “list2”.

Here, We treat lists as items by using a list initializer. By using lists as items, we can make a list of lists. It’s the most straightforward method for making a list of lists. The list3 will be printed, which has both the above-specified lists.

list1 = [21, 22, 23, 24]

list2 = [25, 25, 27, 28]

list3=[list1, list2]

print(list3)

The output here shows the list of lists in the prompt shell as follows.

Example 3: Using the for-loop

We will create a more comprehensive list of lists by explicitly utilizing the append() technique with the for loop.

In the following Python script, we have established an empty list. The list is given the name “mylist.” Then, we have a for loop for creating the list of lists. We have defined the range of the lists in the list by using the range method and passed the value “3” as an argument. The for loop has the variable “m,” which will iterate over the range defined for the list.

After that, we have called the append method, which will append the three lists in the list. Now, the for loop is invoked again for adding the elements in the lists in the range of “6”. The print function will print the three lists in the list and the elements in the lists.

mylist = []

for m in range(3):

mylist.append([])

for n in range(6):

mylist[m] .append(n)

print(mylist)

The output is as follows:

Example 4: Using the List Comprehension to Create a List of Lists in Python in Ubuntu 20.04

In Python, list comprehension is a simple but elegant approach to generating lists. We generate lists using for loops and logic enclosed in square brackets using this strategy.

First of all, we have defined a list that now has string elements. Then, we created another list with the name “colors.” The list “colors” is an empty list for the time being. After that, we called the list “colors” and applied the for loop cycle to it. The for loop iterates over each item in the list and adds the elements in the list by creating a new variable, “elements.” The list “colors” of the lists will be printed at the end.

list = [‘pink’,‘red’,‘white’,‘blue’,‘green’]

colors = []

colors = [[elements] for elements in list]

print(colors)

We can also create a list of lists in python through this approach. This one is the shortest method among all.

Example 5: Accessing the Elements From the List of Lists in Python

By utilizing an index, we may access elements. The list index begins with 0 and ends with n-1, whereby n is the list length.

The following python program has a list initialized with some string values. The new empty list is also created as Animals. Then the for loop is applied to the list “Animal.” We are adding the list in the list “Animals” by using a for loop. In the end, we are displaying the list and accessing the list’s elements.

list = [‘cat’,‘dog’,‘hen’,‘parrot’, ‘panda’]

Animals = []

Animals = [[items] for items in list]

print(Animals)

print(Animals[1])

print(Animals[3])

print(Animals[4])

The list of lists is created and also accessed the list elements by the index location. The output is as follows.

Conclusion

Summing up! About creating a Python list of lists. A list of lists is a list where every value is a separate list. We have deeply gone through creating the list of lists in python. We have four different approaches that will create a list of lists in python easily and efficiently. Then, we have also accessed the list of list elements in python by the index position of the elements in the list. We hope you found it informative.



Source link