Launching EC2 using Terraform | Installing Grafana and promotues using Terraform.
Launching EC2 instance using Terraform:
The set of files used to describe infrastructure in Terraform is known as a Terraform configuration. You will write your first configuration to define a single AWS EC2 instance.
Each Terraform configuration must be in its own working directory. Create a directory for your configuration.
Change into the directory.
Create a file to define your infrastructure.
Open main.tf
in your text editor, paste in the configuration below, and save the file.
Tip: The AMI ID used in this configuration is specific to the us-west-2
region. If you would like to use a different region, see the Troubleshooting section for guidance.
This is a complete configuration that you can deploy with Terraform. The following sections review each block of this configuration in more detail.
»Terraform Block
The terraform {}
block contains Terraform settings, including the required providers Terraform will use to provision your infrastructure. For each provider, the source
attribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by default. In this example configuration, the aws
provider's source is defined as hashicorp/aws
, which is shorthand for registry.terraform.io/hashicorp/aws
.
You can also set a version constraint for each provider defined in the required_providers
block. The version
attribute is optional, but we recommend using it to constrain the provider version so that Terraform does not install a version of the provider that does not work with your configuration. If you do not specify a provider version, Terraform will automatically download the most recent version during initialization.
To learn more, reference the provider source documentation.
»Providers
The provider
block configures the specified provider, in this case aws
. A provider is a plugin that Terraform uses to create and manage your resources.
The profile
attribute in the aws
provider block refers Terraform to the AWS credentials stored in your AWS configuration file, which you created when you configured the AWS CLI. Never hard-code credentials or other secrets in your Terraform configuration files. Like other types of code, you may share and manage your Terraform configuration files using source control, so hard-coding secret values can expose them to attackers.
You can use multiple provider blocks in your Terraform configuration to manage resources from different providers. You can even use different providers together. For example, you could pass the IP address of your AWS EC2 instance to a monitoring resource from DataDog.
»Resources
Use resource
blocks to define components of your infrastructure. A resource might be a physical or virtual component such as an EC2 instance, or it can be a logical resource such as a Heroku application.
Resource blocks have two strings before the block: the resource type and the resource name. In this example, the resource type is aws_instance
and the name is app_server
. The prefix of the type maps to the name of the provider. In the example configuration, Terraform manages the aws_instance
resource with the aws
provider. Together, the resource type and resource name form a unique ID for the resource. For example, the ID for your EC2 instance is aws_instance.app_server
.
Resource blocks contain arguments which you use to configure the resource. Arguments can include things like machine sizes, disk image names, or VPC IDs. Our providers reference documents the required and optional arguments for each resource. For your EC2 instance, the example configuration sets the AMI ID to an Ubuntu image, and the instance type to t2.micro
, which qualifies for AWS' free tier. It also sets a tag to give the instance a name.
»Initialize the directory
When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with terraform init
.
Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the aws
provider.
Initialize the directory.
Terraform downloads the aws
provider and installs it in a hidden subdirectory of your current working directory, named .terraform
. The terraform init
command prints out which version of the provider was installed. Terraform also creates a lock file named .terraform.lock.hcl
which specifies the exact provider versions used, so that you can control when you want to update the providers used for your project.
»Format and validate the configuration
We recommend using consistent formatting in all of your configuration files. The terraform fmt
command automatically updates configurations in the current directory for readability and consistency.
Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won't return any file names.
You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate
command.
Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.
»Create infrastructure
Apply the configuration now with the terraform apply
command. Terraform will print output similar to what is shown below. We have truncated some of the output to save space.
Tip: If your configuration fails to apply, you may have customized your region or removed your default VPC. Refer to the troubleshooting section at the bottom of this tutorial for help.
Before it applies any changes, Terraform prints out the execution plan which describes the actions Terraform will take in order to change your infrastructure to match the configuration.
The output format is similar to the diff format generated by tools such as Git. The output has a +
next to aws_instance.app_server
, meaning that Terraform will create this resource. Beneath that, it shows the attributes that will be set. When the value displayed is (known after apply)
, it means that the value will not be known until the resource is created. For example, AWS assigns Amazon Resource Names (ARNs) to instances upon creation, so Terraform cannot know the value of the arn
attribute until you apply the change and the AWS provider returns that value from the AWS API.
Terraform will now pause and wait for your approval before proceeding. If anything in the plan seems incorrect or dangerous, it is safe to abort here with no changes made to your infrastructure.
In this case the plan is acceptable, so type yes
at the confirmation prompt to proceed. Executing the plan will take a few minutes since Terraform waits for the EC2 instance to become available.
You have now created infrastructure using Terraform! Visit the EC2 console and find your new EC2 instance.
Note: Per the aws
provider block, your instance was created in the us-west-2
region. Ensure that your AWS Console is set to this region.
»Inspect state
When you applied your configuration, Terraform wrote data into a file called terraform.tfstate
. Terraform stores the IDs and properties of the resources it manages in this file, so that it can update or destroy those resources going forward.
The Terraform state file is the only way Terraform can track which resources it manages, and often contains sensitive information, so you must store your state file securely and restrict access to only trusted team members who need to manage your infrastructure. In production, we recommend storing your state remotely with Terraform Cloud or Terraform Enterprise. Terraform also supports several other remote backends you can use to store and manage your state.
Inspect the current state using terraform show
.
When Terraform created this EC2 instance, it also gathered the resource's metadata from the from the AWS provider and wrote the metadata to the state file. Later in this collection, you will modify your configuration to reference these values to configure other resources and output values.
»Manually Managing State
Terraform has a built-in command called terraform state
for advanced state management. Use the list
subcommand to list of the resources in your project's state.
»Troubleshooting
If terraform validate
was successful and your apply still failed, you may be encountering one of these common errors.
If you use a region other than
us-west-2
, you will also need to change yourami
, since AMI IDs are region-specific. Choose an AMI ID specific to your region by following these instructions, and modifymain.tf
with this ID. Then re-runterraform apply
.If you do not have a default VPC in your AWS account in the correct region, navigate to the AWS VPC Dashboard in the web UI, create a new VPC in your region, and associate a subnet and security group to that VPC. Then add the security group ID (
vpc_security_group_ids
) and subnet ID (subnet_id
) arguments to youraws_instance
resource, and replace the values with the ones from your new security group and subnet.Save the changes to
main.tf
, and re-runterraform apply
.Remember to add these lines to your configuration for the rest of the tutorials in this collection. For more information, review this document from AWS on working with VPCs.
#!/bin/bash
###################### Installing java #################################
sudo apt install software-properties-common apt-transport-https -y
sudo apt install openjdk-8-jdk
sudo export RELEASE="2.2.1"
sudo useradd --no-create-home --shell /bin/false prometheus
sudo useradd --no-create-home --shell /bin/false node_exporter
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus
cd /opt/
wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
tar -xvf prometheus-2.26.0.linux-amd64.tar.gz
cd prometheus-2.26.0.linux-amd64
sudo cp /opt/prometheus-2.26.0.linux-amd64/prometheus /usr/local/bin/
sudo cp /opt/prometheus-2.26.0.linux-amd64/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool
sudo cp -r /opt/prometheus-2.26.0.linux-amd64/consoles /etc/prometheus
sudo cp -r /opt/prometheus-2.26.0.linux-amd64/console_libraries /etc/prometheus
sudo cp -r /opt/prometheus-2.26.0.linux-amd64/prometheus.yml /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
sudo chown -R prometheus:prometheus /etc/prometheus/prometheus.yml
prometheus --version
promtool --version
sudo touch /etc/systemd/system/prometheus.service
sudo tee -a /etc/systemd/system/prometheus.service << END
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
END
sudo systemctl start prometheus
sudo systemctl enable prometheus
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana -y
sudo systemctl start grafana-server
Create a terraform file on the name of resources.tf and past.
provider "aws" {
region = "${var.region}"
access_key = "${var.access_key_id}"
secret_key = "${var.secret_key_id}"
}
data "template_file" "init" {
template = "${file("installation.sh")}"
}
resource "aws_instance" "test" {
ami = "${var.ami_id}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${var.subnet}"
user_data = "${data.template_file.init.rendered}"
vpc_security_group_ids = ["${var.security_group_id}"]
tags = {
Name = "${var.instance_name}"
}
}
Create a terraform file on the name of var.tfvars and past.
region = "ap-south-1"
access_key_id = "xxxxxxxxxxxxxxxxxxx"
secret_key_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ami_id = "ami-04a0d9**********"
instance_type = "t3.small"
subnet = "subnet-b4*****"
security_group_id = "sg-041d19xxxxxxx"
instance_name = "grafana-test"
key_name = "give the key_pair"
Note: Give the acces id, secret key and instance id on which instance, sub net and give the name for the instance and name of key pair mention for the instance.
Create a terraform file on the name of variables.tf and past.
variable "region"{}
variable "ami_id"{}
variable "key_name"{}
variable "access_key_id"{}
variable "secret_key_id"{}
variable "instance_type"{}
variable "subnet"{}
variable "security_group_id"{}
variable "instance_name"{}
- initialize the directory with
terraform init
. - To check the configuration is correct or not use
terraform plan
command. - Apply the configuration now with the
terraform apply
command. - In this case the plan is acceptable, so type
yes
at the confirmation prompt to proceed. - Inspect the current state using
terraform show
.
Comments
Post a Comment