I use S3 as my backend, in my terraform codebase, please be easy with me because I'm starting to learn terratest.
So, I use below code base for my terratest
func TestApiGateway(t *testing.T) {
t.Parallel()
awsRegion := "us-east-1"
uniqueId := random.UniqueId()
// Create an S3 bucket where we can store state
bucketName := fmt.Sprintf("test-terraform-backend-example-%s", strings.ToLower(uniqueId))
defer cleanupS3Bucket(t, awsRegion, bucketName)
aws.CreateS3Bucket(t, awsRegion, bucketName)
key := fmt.Sprintf("%s/terraform.tfstate", uniqueId)
secretKey := "GXLbU7+igRxRHubTmJ45F"
accessKey := "I6Q2SCYX"
token := "IQoJb3JpZ2luX2VjEAkaCXVz"
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../../",
// Environment variables to set when running Terraform
EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
"AWS_ACCESS_KEY_ID": accessKey,
"AWS_SECRET_ACCESS_KEY": secretKey,
"AWS_SESSION_TOKEN": token,
},
Vars: map[string]interface{}{
"token": token,
"region": awsRegion,
"access_key": accessKey,
"secret_key": secretKey,
},
BackendConfig: map[string]interface{}{
"bucket": bucketName,
"key": key,
"region": awsRegion,
},
})
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
stageUrl := terraform.Output(t, terraformOptions, "deployment_invoke_url")
time.Sleep(30 * time.Second)
statusCode := DoGetRequest(t, stageUrl)
assert.Equal(t, 200, statusCode)
}
func DoGetRequest(t *testing.T, api string) int {
resp, err := http.Get(api)
if err != nil {
log.Fatalln(err)
}
//We Read the response status on the line below.
return resp.StatusCode
}
As per https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/options.go
EnvVars map[string]string // Environment variables to set when running Terraform
EnvVars sets up the environment variables prior running the terraform. But I'm getting below error:
Error finding AWS credentials. Did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables or configure an AWS profile? Underlying error: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
my backend.tf lookslike this
terraform {
backend "s3" {}
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.47.0"
}
}
}
provider "aws" {
region = var.aws_region
access_key = var.access_key
secret_key = var.secret_key
token = var.token
}
So as a workaround I use the export command,
export AWS_SECRET_ACCESS_KEY="actual_secret_access_key"
export AWS_ACCESS_KEY_ID="actual_access_key"
export AWS_SESSION_TOKEN="actual_token"
then I'm getting
Error: Backend initialization required, please run "terraform init"
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ Reason: Initial configuration of the requested backend "s3"
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ The "backend" is the interface that Terraform uses to store state,
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ perform operations, etc. If this message is showing up, it means that the
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ Terraform configuration you're using is using a custom configuration for
TestApiGateway 2023-01-08T18:03:46+01:00 logger.go:66: │ the Terraform backend.