Repository: gruntwork-io/terragrunt (language: go)

Issue:
Bucket policy error on migration of existing terraform bucket to terragrunt
**Describe the bug**
During migration of existing Terraform project to Terragrunt, noticed bellow error:

```
DEBU[0000] *s3err.RequestFailure NoSuchBucketPolicy: The bucket policy does not exist
        status code: 404, request id: E8RC2BW7CJ0ETWEV, host id: RpTp1UWKs+lckP1AEpWGkXZdoP2NBmxwmgvtlgwx9cO+z0Sp2itoKTKOiJZ++C2Dq3pGoJBwB1g=
/home/circleci/project/remote/remote_state_s3.go:993 (0xf96511)
/home/circleci/project/remote/remote_state_s3.go:618 (0xf92f09)
/home/circleci/project/remote/remote_state_s3.go:474 (0xf9227f)
/home/circleci/project/remote/remote_state_s3.go:319 (0xf91705)
/home/circleci/project/remote/remote_state.go:257 (0xf8d20b)
/home/circleci/project/remote/remote_state_s3.go:302 (0xf913ab)
/home/circleci/project/remote/remote_state.go:80 (0xf8c423)
/home/circleci/project/cli/commands/terraform/action.go:432 (0x13dfbb7)
/home/circleci/project/cli/commands/terraform/action.go:261 (0x13de97e)
/home/circleci/project/cli/commands/terraform/action.go:563 (0x13e057b)
/home/circleci/project/cli/commands/terraform/action.go:505 (0x13e0205)
/home/circleci/project/cli/commands/terraform/action.go:265 (0x13de9bb)
/home/circleci/project/cli/commands/terraform/action.go:213 (0x13de2a8)
/home/circleci/project/cli/commands/terraform/action.go:87 (0x13ddd15)
/home/circleci/project/cli/commands/terraform/command.go:38 (0x196425b)
/home/circleci/project/pkg/cli/command.go:151 (0x11d82aa)
/home/circleci/project/pkg/cli/command.go:140 (0x11d8218)
/home/circleci/project/pkg/cli/app.go:127 (0x11d5771)
/home/circleci/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/command.go:274 (0x5a2438)
/home/circleci/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/app.go:332 (0x59edb7)
/home/circleci/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/app.go:309 (0x11d53a5)
/home/circleci/project/pkg/cli/app.go:131 (0x11d5381)
/home/circleci/project/main.go:17 (0x1967e85)
/usr/local/go/src/runtime/internal/atomic/types.go:194 (0x43c3bb)
/usr/local/go/src/runtime/asm_amd64.s:1650 (0x46c681)
ERRO[0000] NoSuchBucketPolicy: The bucket policy does not exist
        status code: 404, request id: E8RC2BW7CJ0ETWEV, host id: RpTp1UWKs+lckP1AEpWGkXZdoP2NBmxwmgvtlgwx9cO+z0Sp2itoKTKOiJZ++C2Dq3pGoJBwB1g=

```

**To Reproduce**
1. Create S3 bucket manually
1. Don't specify bucket policy
1. Migrate S3 bucket to Terragrunt configuration:
```hcl
remote_state {
  backend = "s3"
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket         = "xyz"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "us-east-2"
    encrypt        = true
  }
}
```

**Expected behavior**
Terragrunt to add missing configurations and don't crash

**Nice to have**
- [x] Terminal output
- [ ] Screenshots

**Versions**
- Terragrunt version: 0.54.12
- Terraform version: 1.6.6
- Environment details (Ubuntu 20.04, Windows 10, etc.): Ubuntu 22.04

**Additional context**

N/A


Discussion / hints:

Fix patch:
diff --git a/remote/remote_state_s3.go b/remote/remote_state_s3.go
index 5eb78d3ec1..978df6f218 100644
--- a/remote/remote_state_s3.go
+++ b/remote/remote_state_s3.go
@@ -989,6 +989,12 @@ func checkIfBucketRootAccess(s3Client *s3.S3, config *RemoteStateConfigS3, terra
 		Bucket: aws.String(config.Bucket),
 	})
 	if err != nil {
+		// NoSuchBucketPolicy error is considered as no policy.
+		if awsErr, ok := err.(awserr.Error); ok {
+			if awsErr.Code() == "NoSuchBucketPolicy" {
+				return false, nil
+			}
+		}
 		terragruntOptions.Logger.Debugf("Could not get policy for bucket %s", config.Bucket)
 		return false, errors.WithStackTrace(err)
 	}


Test patch:
diff --git a/test/integration_test.go b/test/integration_test.go
index 5cacc70aa8..c6885de188 100644
--- a/test/integration_test.go
+++ b/test/integration_test.go
@@ -6452,6 +6452,35 @@ func TestTerragruntAssumeRole(t *testing.T) {
 	assert.Contains(t, content, "session_name = \"session_name_example\"")
 }
 
+func TestTerragruntUpdatePolicy(t *testing.T) {
+	t.Parallel()
+
+	tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_PATH)
+	rootPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_PATH)
+	cleanupTerraformFolder(t, rootPath)
+
+	s3BucketName := fmt.Sprintf("terragrunt-test-bucket-%s", strings.ToLower(uniqueId()))
+	lockTableName := fmt.Sprintf("terragrunt-test-locks-%s", strings.ToLower(uniqueId()))
+
+	err := createS3BucketE(t, TERRAFORM_REMOTE_STATE_S3_REGION, s3BucketName)
+	assert.NoError(t, err)
+
+	defer deleteS3Bucket(t, TERRAFORM_REMOTE_STATE_S3_REGION, s3BucketName)
+	defer cleanupTableForTest(t, lockTableName, TERRAFORM_REMOTE_STATE_S3_REGION)
+
+	tmpTerragruntConfigPath := createTmpTerragruntConfig(t, rootPath, s3BucketName, lockTableName, config.DefaultTerragruntConfigPath)
+
+	// check that there is no policy on created bucket
+	_, err = bucketPolicy(t, TERRAFORM_REMOTE_STATE_S3_REGION, s3BucketName)
+	assert.Error(t, err)
+
+	runTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-config %s --terragrunt-working-dir %s", tmpTerragruntConfigPath, rootPath))
+
+	// check that policy is created
+	_, err = bucketPolicy(t, TERRAFORM_REMOTE_STATE_S3_REGION, s3BucketName)
+	assert.NoError(t, err)
+}
+
 func validateOutput(t *testing.T, outputs map[string]TerraformOutput, key string, value interface{}) {
 	t.Helper()
 	output, hasPlatform := outputs[key]