DEV Community

Zahraa Jawad
Zahraa Jawad

Posted on

Amazon Virtual Private Cloud (Amazon VPC) By Terraform using Visual Studio Code

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

 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

Image description

Image description

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:

Image description

Image description

  • 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

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

Image description

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

Image description

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

Provider Script:

Image description

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

Image description

The steps of creating Amazon VPC By terraform:

Image description

Image description

Image description

Image description

Image description

Image description

Before execution, configure to your account:

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

Image description

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

Image description

Image description

Image description

Image description

Image description

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

Image description

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

Top comments (0)