0% found this document useful (0 votes)
51 views59 pages

Terraform

Visual Studio Code is a multi-platform source code editor that can be used for a variety of programming languages. It includes features like syntax highlighting, code completion, debugging support, and extensions. Users can customize preferences, themes, and install extensions. Terraform is an infrastructure as code tool to safely and efficiently build, change, and version infrastructure components like compute instances, storage, and networking. It can be used with providers like AWS to define and manage resources through configuration files. The HashiCorp Terraform Visual Studio Code extension allows using Terraform commands and workflows within VS Code.

Uploaded by

Santhosh M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views59 pages

Terraform

Visual Studio Code is a multi-platform source code editor that can be used for a variety of programming languages. It includes features like syntax highlighting, code completion, debugging support, and extensions. Users can customize preferences, themes, and install extensions. Terraform is an infrastructure as code tool to safely and efficiently build, change, and version infrastructure components like compute instances, storage, and networking. It can be used with providers like AWS to define and manage resources through configuration files. The HashiCorp Terraform Visual Studio Code extension allows using Terraform commands and workflows within VS Code.

Uploaded by

Santhosh M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Terraform

Visual Studio Code


Visual Studio Code is a source-code editor made
by Microsoft for Windows, Linux and macOS. Features
include support for debugging, syntax
highlighting, intelligent code completion, snippets, code
refactoring, and embedded Git. Users can change
the theme, keyboard shortcuts, preferences, and
install extensions that add additional functionality.

Features
Visual Studio Code is a source-code editor that can be
used with a variety of programming languages,
including Java, JavaScript, Go, Node.js, Python and C+
+. It is based on the Electron framework, which is used
to develop Node.js Web applications that run on
the Blink layout engine. Visual Studio Code employs the
same editor component (codenamed "Monaco") used
in Azure DevOps (formerly called Visual Studio Online
and Visual Studio Team Services).
Instead of a project system, it allows users to open one or
more directories, which can then be saved in workspaces
for future reuse. This allows it to operate as a language-
agnostic code editor for any language. It supports a
number of programming languages and a set of features
that differs per language. Unwanted files and folders can
be excluded from the project tree via the settings. Many
Visual Studio Code features are not exposed through
menus or the user interface but can be accessed via the
command palette.
Visual Studio Code can be extended
via extensions, available through a central repository.
This includes additions to the editor and language
support. A notable feature is the ability to create
extensions that add support for new languages, themes,
and debuggers, perform static code analysis, and
add code linters using the Language Server Protocol.
Visual Studio Code includes multiple extensions
for FTP, allowing the software to be used as a free
alternative for web development. Code can be synced
between the editor and the server, without downloading
any extra software.
Visual Studio Code allows users to set the code page in
which the active document is saved,
the newline character, and the programming language of
the active document. This allows it to be used on any
platform, in any locale, and for any given programming
language.
Language support
Out of the box, Visual Studio Code includes basic
support for most common programming languages. This
basic support includes syntax highlighting, bracket
matching, code folding, and configurable snippets.
Visual Studio Code also ships with IntelliSense for
JavaScript, TypeScript, JSON, CSS, and HTML, as well
as debugging support for Node.js. Support for additional
languages can be provided by freely available extensions
on the VS Code Marketplace.
Data collection
Visual Studio Code collects usage data and sends it to
Microsoft, although this can be disabled. In addition,
because of the open-source nature of the application, the
telemetry code is accessible to the public, who can see
exactly what is collected. According to Microsoft, the
data is shared with Microsoft-controlled affiliates and
subsidiaries, although law enforcement may request it as
part of a legal process.
Version control
Source control is a built-in feature of Visual Studio
Code. It has a dedicated tab inside of the menu bar where
you can access version control settings and view changes
made to the current project. To use the feature you must
link Visual Studio Code to any supported version control
system (Git, Apache Subversion, Perforce, etc.). This
allows you to create repositories as well as make push
and pull requests directly from the Visual Studio Code
program.
Introduction to Terraform
Terraform is an infrastructure as code (IaC) tool that
allows you to build, change, and version infrastructure
safely and efficiently. This includes low-level
components such as compute instances, storage, and
networking, as well as high-level components such as
DNS entries, SaaS features, etc. Terraform can manage
both existing service providers and custom in-house
solutions.
Command: init - Terraform
The terraform init command is used to initialize a
working directory containing Terraform configuration
files. This is the first command that should be run after
writing a new Terraform configuration or cloning an
existing one from version control. It is safe to run this
command multiple times.

Command: plan - Terraform


The terraform plan command evaluates a Terraform
configuration to determine the desired state of all the
resources it declares, then compares that desired state to
the real infrastructure objects being managed with the
current working directory and workspace.

Command: apply - Terraform


The terraform apply command executes the actions
proposed in a Terraform plan. ... Another way to use
terraform apply is to pass it the filename of a saved plan
file you created earlier with terraform plan -out=... , in
which case Terraform will apply the changes in the plan
without any confirmation prompt.

Install Terraform Visual Studio Code extension


1. Launch Visual Studio Code.
2. Click on the Extensions shortcut on the left navigation
panel.
3. Search terraform in extensions marketplace.
4. Select HashiCorp Terraform from search results.
5. Click the Install button.
Run the following commands to run Terraform
script in your system
1. terraform init
It is used to initialize the working directory.
It will install the required plugins for our code, e.g.,
AWS S3.
You will see something like after running terraform
init successfully-

2. terraform plan
We will use this command for script verification. It will
show if there is an error in our configuration.
The output of terraform plan looks like this if it runs
successfully-
3. terraform apply
Use terraform apply to create your S3 bucket.
It will ask you for confirmation before execution; enter
yes for confirmation.
Use terraform apply -auto-approve if you want to execute
it without asking for confirmation.
After successful execution, it will display the following
message-
You can verify your bucket in S3 services in your AWS
Account.
Terraform AWS S3
Steps to Create an S3 Bucket using Terraform
 Create S3 bucket module. Create a module that will
have a basic S3 file configuration.
 Define bucket. Open bucket.tf and define bucket in
that.
 Define variables. In var.tf, we will define variables for
the bucket.tf.
 Add Configuration.
 Add Access key, Secret key, and Region.
Screenshots
Terraform AWS EC2 Instance
Using Terraform to Create an EC2 Instance
 Create an EC2 Instance.
 Automatically look up the latest Windows Server
2019 AMI for the EC2 instance.
 Create and attach a additional drive.
 Create a Cloudwatch Alarm Metric to monitor CPU.
Screenshots
Terraform AWS IAM

1. Creating an AWS IAM role using Terraform:


This is where, the IAM role creation will be done. The
assume_role_policy parameter is a must to be given
within the resource block, and there are other optional
parameters as well such as name, path, description etc.

The terraform script:


The resource block above, constructs a
resource of the stated TYPE (i.e. the initial
parameter “aws_iam_role”) and NAME (i.e.
the second parameter “ec2_s3_access_role”).
The integration of the type and name must be
distinctive. Within the block (the { }) is the
configuration for the resource.

A resource component in terraform, constructs a


resource, of the given TYPE (first parameter)
and NAME (second parameter) when defining a
resource. As an example, if the script is:

resource "aws_iam_instance_profile" "test_profile" {


name = "test_profile"
roles = ["${aws_iam_role.ec2_s3_access_role.name}"]
}

So in the above block, aws_iam_instance_profile is


the TYPE and test_profile is the NAME. The
combination of the type and name must be unique.

assume_role_policy parameter in the above resource


block, allows an entity, permission to assume the role.

The assume role policy:

2. Creating an AWS IAM policy using Terraform:


This is where we need to define the required policy (i.e.
permissions) according to the necessities. For example,
allowing the IAM role to access all the S3 buckets within
the region. Providing the policy is a required parameter,
where as there are other parameters as well such as arn,
path, id etc.

The terraform script:

The policy parameter in the above block, requires an


IAM policy in a JSON format. What the following policy
does is that, it allows the IAM role to access all the S3
buckets and also to perform any kind of actions (i.e. list
buckets, put objects, delete objects etc.) on those buckets.

The IAM policy:

3. Attaching the policy to the role using Terraform:


This is where, we’ll be attaching the policy which we
wrote above, to the role we created in the first step.

The terraform script:

The aws_iam_policy_attachment in the above resource


block, is used to attach a Managed IAM Policy to user(s),
role(s), and/or group(s). But in our case, it was a role.
The value for the roles parameter has been accessed from
the resource block which we created in step 1.

Value of the role = $


{aws_iam_role.ec2_s3_access_role.name}

Explanation:

> aws_iam_role is the type of the resource block which


we created in step 1.
> ec2_s3_access_role is the name of the variable which
we defined.

> name is a property of that resource block.

The same thing applies to the value for policy_arn.

SCREENSHOTS
Terraform SQS
How to create an SQS queue on AWS using
Terraform
 Pre-requisites.
 What we will do.
 Write Terraform configuration files for SQS Queue.
 Create an SQS Queue using the Terraform
configuration files.
 Delete the created SQS Queue using Terraform.
SCREENSHOTS
Terraform SNS
How to create an SNS topic on AWS using Terraform
 Pre-requisites.
 What we will do.
 Write Terraform configuration files for SNS Topic.
 Create an SNS Topic using the Terraform
configuration files.
 Delete the created SNS topic using Terraform.
SCREENSHOTS
Terraform VPC
How do I create AWS VPC with terraform?
Running Terraform to Create the AWS VPC
 Open a terminal and navigate to the ~\terraform-vpc-
demo directory. cd ~\terraform-vpc-demo.
 Run the terraform init command in the same directory.

 Now, run the terraform plan command.

 Next, tell Terraform actually to provision the AWS

VPC and resources using terraform apply


SCREENSHOTS
Terraform Lambda
How do I create a Lambda function in AWS?
To create a Lambda function with the console
 Open the Functions page of the Lambda console.
 Choose Create function.
 Under Basic information, do the following: For
Function name, enter my-function . For Runtime,
confirm that Node. js 14. x is selected. Note that
Lambda provides runtimes for .
 Choose Create function.
SCREENSHOTS

You might also like