Amazon Virtual Private Cloud (Amazon VPC) By Terraform using Visual Studio Code
Tags: aws, vscode, terraform, webdev
Published: 2022-12-12 18:15:15

**Outline**  Basic concepts
         What is Terraform?
         Install Terraform
         AWS Provider
         Amazon VPC
         Amazon VPC help to:
         Create Amazon VPC
         The configuration we need before creating an amazon vpc
         Amazon VPC Documentation
         The steps of creating Amazon VPC By terraform:

**Basic concepts** 

** What is Terraform?**

Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently. HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like
DNS entries and SaaS features.


** Install Terraform**

   To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

The following link include the installation of terraform:

https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli



** AWS Provider**

Use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS. You must configure the provider with the proper credentials before you can use it.


**Authentication and Configuration:**

Configuration for the AWS Provider can be derived from several sources, which are applied
in the following order:
• Parameters in the provider configuration
• Environment variables
• Shared credentials files
• Shared configuration files
• Container credentials
• Instance profile credentials and region



** Amazon VPC:**

Amazon Virtual Private Cloud (Amazon VPC) enables you to launch AWS
resources into a virtual network that you've defined. This virtual network closely resembles a traditional network that you'd operate in your own data center, with the benefits of using the scalable infrastructure of AWS.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uny2iyaktwi2q9rwthf3.png)



** Amazon VPC help to:**

- Secure and monitor connections, screen traffic, and limit 
  instance access within the virtual network.
- Spend less time setting up, managing, and checking your virtual 
  network.
- Customize the virtual network by choosing your own IP address 
  range, creating subnets, and configuring routing tables.


** Creation Amazon VPC**

You can create and manage your VPCs using any of the following
interfaces:

• **AWS Management Console** — Provides a web interface that you 
    can use to access your VPCs.
• **AWS Command Line Interface (AWS CLI)** — Provides commands for 
    a broad set of AWS services, including Amazon VPC, and is 
    supported on Windows, Mac, and Linux.
• **AWS SDKs — Provides language**-specific APIs and takes care of 
    many of the connection details, such as calculating signatures, 
    handling request retries, and error handling.
• **Query API** - Provides low-level API actions that you call 
    using HTTPS requests. Using the Query API is the most direct 
    way to access Amazon VPC, but it requires that your application 
    handle low-level details such as generating the hash to sign 
    the request, and error handling.



**The configuration we need before creating an amazon vpc:**

**• Access key and Secrete access key**

We need the access key and secret access key to perform the configuration for the aws management console.

You can get access key and secret access key by the steps:
- Sign in to the AWS Management Console and open the IAM console
  at https://console.aws.amazon.com/iam
- In the navigation pane, choose Users and then choose any user you 
  need for configuration in your account.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4dk2mzor4bk44oydbnd.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5my2wzhdbo7390eb5l1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbnv0ypwgjscu5rzr91a.png)

**Warning:** Never post your secret access key on public platforms, such as GitHub. This can compromise your account security.


**Or** you can make a new user in your account and have an access key and secret access key for this user by the following steps:

- Sign in to the AWS Management Console and open the IAM console
  at https://console.aws.amazon.com/iam
- In the navigation pane, choose **Users** and then choose **Add users**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/foakqakckhkfj822puaj.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6bjre1fmpkxvcpc6o79.png)


- On the **Set permissions** page, specify how you want to assign 
  permissions to this set of new users. Choose one of the following 
  three options:

o Add user to group.
o Copy permissions from existing user.
o Attach existing policies directly.

- Then choose Next: **Tags**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4znmmm37xb6j43xkku1.png)

- Add tag which is optional.
- Then choose **Next: Review** to see all of the choices you made 
  up to this point.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxl2g6h766cwqsrfmq4n.png)

- Note here the access key and secret access( press on show to view the Secret access key).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aaoqyiwvrokwu7j3kh02.png)


**Amazon VPC Documentation:**

By VPC Documentation in aws, we copy the script of creating vpc and provider into the Visual Studio Code or (git) program through the following steps:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5mekp9pwvt6oinhure8.png)

**Provider Script:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b09zy2pr8usbvtzktbne.png)

**Note:**
You can install the Vscode by the link https://code.visualstudio.com/
and choose the appropriate operating system for your device.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx9ncrd6ivxpfkr0pijp.png)


**The steps of creating Amazon VPC By terraform:**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd1cw6txionueem6wv5w.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ohmjhz06uluoerwll1r.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8fgg8ir8y8o7omjlmxu.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dldd76j1q6mnum8tubsc.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdeopximd67zjy8nhvj6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lsw156h0tyuoictvqcl.png)


**Before execution, configure to your account:**

To configure with an AWS account, execute the command: Aws configure and press Enter.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kykrxiwdsw8p25k56r3y.png)


- The first command: terraform init, then press Enter to execute.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg7m4e2m7ynqbf6hy2py.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btpt619ejfv7kn36oiem.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r041m8rsv00fu2kbktsf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50sdpuqowghp8zlql6ln.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2t46bcs86v6xt4nhfruj.png)

To make sure that the vpc is deleted :
Go to your AWS account and then to your vpc

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fi41mclytp3ymwp4o3f4.png)




**References:**
1- https://developer.hashicorp.com/terraform/intro
2-https://registry.terraform.io/providers/hashicorp/aws/latest/docs
3- https://aws.amazon.com/vpc/