Created
June 26, 2019 11:14
-
-
Save fukasawah/bcdf110e87e1cdfef3006e8e9ed2e0f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage) bash tfimport.sh RESOURCE_TYPE RESOURCE_NAME RESOURCE_ADDR | |
# eg.) bash tfimport.sh "aws_s3_bucket_notification" "foo" my-s3-bucket-name | |
set -ue | |
TEMP_TF_FILE="temp.$$.tf" | |
RESOURCE_TF_FILE="resources.tf" | |
RESOURCE_TYPE="$1" | |
RESOURCE_NAME="$2" | |
RESOURCE_ADDR="$3" | |
function teardown(){ | |
rm "$TEMP_TF_FILE" || true | |
exit | |
} | |
trap 'teardown' EXIT | |
cat << EOF > "$TEMP_TF_FILE" | |
resource "$RESOURCE_TYPE" "$RESOURCE_NAME" {} | |
EOF | |
terraform import "$RESOURCE_TYPE.$RESOURCE_NAME" "$RESOURCE_ADDR" && | |
terraform state show -no-color "$RESOURCE_TYPE.$RESOURCE_NAME" >> "$RESOURCE_TF_FILE" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment