I am trying to learn terraform usage with jenkins. I realized that I can not use 2 tf files in the same github repository.I have 2 tf files: Providers.tf and Provider2.tf
Provider.tf:
#OpenShot Terraform Project
provider "aws" {
region = "eu-west-2"
}
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket-leanscale-2"
acl = "private"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {a
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "HelloWorld-leanscale-2"
}
}
Provider2.tf:
#OpenShot Terraform Project
provider "aws" {
region = "eu-west-2"
}
resource "aws_s3_bucket" "b2" {
bucket = "my-tf-test-bucket-leanscale-3"
acl = "private"
tags = {
Name = "My bucket2"
Environment = "Dev"
}
}
data "aws_ami" "ubuntu2" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web2" {
ami = data.aws_ami.ubuntu2.id
instance_type = "t2.micro"
tags = {
Name = "HelloWorld-leanscale-3"
}
}
Jenkins Script :
pipeline {
agent any
tools {
terraform 'terraform-leanscale'
}
environment {
TF_WORKSPACE = 'default' //Sets the Terraform Workspace
TF_IN_AUTOMATION = 'true'
AWS_ACCESS_KEY_ID = "${params.AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${params.AWS_SECRET_ACCESS_KEY}"
}
stages {
stage('Git Checkout') {
steps {
git branch: 'staging', credentialsId: 'leanscale', url: 'https://github.com/yusufkaratoprak/leanscale_yusuf.git'
}
}
stage('terraform-init') {
steps {
sh label: '', script: 'terraform init'
}
}
stage('terraform-apply') {
steps {
sh label: '', script: 'terraform apply --auto-approve'
}
}
}
}
```C
[Pipeline] sh
+ terraform init
[31mThere 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.[0m[0m
[31m
[1m[31mError: [0m[0m[1mArgument or block definition required[0m
[0m on provider.tf line 24, in data "aws_ami" "ubuntu":
24: filter {[4ma[0m
[0m
An argument or block definition is required here. To set an argument, use the
equals sign "=" to introduce the argument value.
[0m[0m
[31m
[1m[31mError: [0m[0m[1mDuplicate provider configuration[0m
[0m on provider2.tf line 2:
2: [4mprovider "aws"[0m {
[0m
A default (non-aliased) provider configuration for "aws" was already given at
provider.tf:2,1-15. If multiple configurations are required, set the "alias"
argument for alternative configurations.
[0m[0m
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (terraform-apply)
Stage "terraform-apply" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
How can OI use multiple tf files in the same repository by Jenkins script?
Move this declaration in its own file instead of duplicating
provider "aws" {
region = "eu-west-2"
}
Terraform doesn't care if you use one or 100 files for the code, but definitions must be unique.