terraform with DynamoDB backend: 400 Bad Request when creating new workspace

Viewed 610

I have terraform stack which keeps locks in DynamoDB:

terraform {
  backend "s3" {
    bucket  = "bucketname"
    key     = "my_key"
    encrypt = "true"
    role_arn = "arn:aws:iam::11111111:role/my_role"
    dynamodb_table = "tf-remote-state-lock"
  }
}

When I run terraform workspace new test it fails with (quite misleading) error:

failed to lock s3 state: 2 errors occurred:
* ResourceNotFoundException: Requested resource not found
* ResourceNotFoundException: Requested resource not found

If I turn on TF_LOG=DEBUG then I see 400 Bad Request (more details on pastebin)

What am I doing wrong and how to fix it?

2 Answers

Solution: terraform workspace whatsoever should rum only AFTER terraform init. If you have TF_WORKSPACE set up, you may have error during tf init saying that the workspace does not exist yet, so you may have temptation to rum tf ws new before tf init. Don't do it, just set up TF_WORKSPACE only after tf init.

Probably you did not create your AWS resources. The documentation says the following about the s3 bucket:

This assumes we have a bucket created called mybucket.

I think the second message comes from the DynamoDb table not created or not configured correctly. The documentation says the following about using the DynamoDb for locking and consistentcy:

dynamodb_table - (Optional) Name of DynamoDB Table to use for state locking and consistency. The table must have a primary key named LockID with type of string. If not configured, state locking will be disabled.

Related