We are going to create the
example.tf configuration file containing the following code :
provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}
The profile named
default is the one we declared
while configuring AWS CLI. It's part of the best practices to save
AWS credentials (aka
IAM) outside of
Terraform code.
Let's create this now :
workDir="
$HOME/terraform"; mkdir -p "$workDir" && cd "$workDir"
cat << EOF >
example.tf
provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}
EOF
When starting working on a new configuration, or after checking out an existing one from version control, we must initialize things :
terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.31.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.31"
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.
- The aws provider plugin is downloaded and installed in a subdirectory of the current working directory ($HOME/terraform/.terraform/plugins/...), along with various other book-keeping files.
- The output specifies which version of the plugin was installed, and suggests specifying that version in configuration to ensure that running terraform init in future will install a compatible version (not necessary here since we're only experimenting)