Skip to content

Commit

Permalink
Fix error when re-running account setup (#365)
Browse files Browse the repository at this point in the history
## Context

Currently, when re-running `make infra-set-up-account` for an account
that is already set up, the script will error when trying to import the
bucket in to the terraform state, since it already exists in the state
file. The error shown is:

> Error: Resource already managed by Terraform
>
> Terraform is already managing a remote object for
module.backend.aws_s3_bucket.tf_state. To import to this address you
> must first remove the existing object from the state.

This change updates the script to first check if the bucket already exist,
and if so, don't create it
  • Loading branch information
lorenyu authored Aug 2, 2023
1 parent 86e6bd1 commit ea20b99
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions bin/set-up-current-account.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ terraform init \
-backend-config="key=$TF_STATE_KEY" \
-backend-config="region=$REGION"

# Import the S3 bucket that was created in the previous step so we don't recreate it
terraform import module.backend.aws_s3_bucket.tf_state $TF_STATE_BUCKET_NAME
# Import the bucket that we created in the previous step so we don't recreate it
# But first check if the bucket already exists in the state file. If we are
# re-running account setup and the bucket already exists then skip the import step
if ! terraform state list module.backend.aws_s3_bucket.tf_state; then
terraform import module.backend.aws_s3_bucket.tf_state $TF_STATE_BUCKET_NAME
fi

terraform apply \
-input=false \
Expand Down
1 change: 1 addition & 0 deletions otherfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def
1 change: 1 addition & 0 deletions somefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc

0 comments on commit ea20b99

Please sign in to comment.