Terraform - Ec2 instance
Terraform is configured using a high-level, expressive configuration syntax to describe the resources that make up your infrastructure.
First create the main.tf file
Then we see the following
main.tf
workbook:terraform workbookcode$ pwd
/Users/workbookcode/repos/terraform
workbook-ltm1:terraform workbookcode$ subl .
workbook-ltm1:terraform workbookcode$ pwd
/Users/workbookcode/repos/terraform
workbook-ltm1:terraform workbookcode$ terraform init
There are some problems with the configuration, described below.
The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
Error: Argument or block definition required
on main.tf line 13, in resource "aws_instance" "web":
An argument or block definition is required here.
workbook-ltm1:terraform workbookcode$ terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (terraform-providers/aws) 2.13.0...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 2.13"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
#-----------------------------------------------------------------------------------------------------------------
In the example above, I am creating a machine on the region “us-east-1” using the profile “terraform”.
My machine size is t2.micro and it is using the AMI ami-0e55e373 , which is a Ubuntu 17.04 image available for the region “us-east-1”.
Note : Ubuntu 17.04 image doesn’t have the same AMI id in two different regions.
One issue with the main.tf was that, the region had a typo in it
Instead of us-east-1 . it was . eu-east-1 . and terraform was erroring out
Now as we move on, we keep on adding the variables that we can use in the terraform
variable "region" {
default = "us-east-1"
}
variable "shared_credentials_file" {
default = "/Users/asdf/.aws/credentials"
}
variable "profile" {
default = "terraform"
}
provider "aws" {
region = "us-east-1"
shared_credentials_file = "/Users/asdf/.aws/credentials"
profile = "terraform"
}
resource "aws_instance" "artifactory_test" {
ami = "ami-04d587fb777042a31"
instance_type = "t2.micro"
tags = {
Name = "artifactory_test"
}
}
I’ve run into that same typo issue before with regions—it’s such a small mistake, but it can block everything. The way you broke it down makes troubleshooting clearer. When you spun up the EC2 instance, did you also set up security groups in the same config, or do you prefer to manage them separately?
ReplyDelete